From 99695dc24d89578905fc700931a35fd77a133544 Mon Sep 17 00:00:00 2001 From: Vlad Pronsky Date: Tue, 6 Jun 2023 02:32:14 +0300 Subject: [PATCH] fix sqlite 3.34 --- Dockerfile-test | 26 ++++++++++++++++++++++++++ Makefile | 5 ++++- twscrape/account.py | 1 + twscrape/accounts_pool.py | 26 ++++++++++++++++++++------ twscrape/cli.py | 4 ++-- twscrape/db.py | 4 ++++ 6 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 Dockerfile-test diff --git a/Dockerfile-test b/Dockerfile-test new file mode 100644 index 0000000..4fce831 --- /dev/null +++ b/Dockerfile-test @@ -0,0 +1,26 @@ +FROM python:3.10-alpine +ARG SQLITE_Y=2021 +ARG SQLITE_V=3340100 + +RUN pip install --upgrade pip +RUN python -c "import sqlite3;print(sqlite3.sqlite_version)" + +# https://www.sqlite.org/chronology.html +RUN apk add build-base +RUN wget https://sqlite.org/${SQLITE_Y}/sqlite-autoconf-${SQLITE_V}.tar.gz -O sqlite.tar.gz \ + && tar xvfz sqlite.tar.gz \ + && cd sqlite-autoconf-${SQLITE_V} \ + && ./configure --prefix=/usr/local --build=aarch64-unknown-linux-gnu \ + && make \ + && make install \ + && cd .. \ + && rm -rf sqlite* + +RUN sqlite3 --version +RUN python -c "import sqlite3;print(sqlite3.sqlite_version)" + +WORKDIR /app +COPY pyproject.toml readme.md /app/ +RUN pip install -e .[dev] +COPY . /app +RUN pytest tests/ diff --git a/Makefile b/Makefile index 10e6ef8..d1c95fb 100644 --- a/Makefile +++ b/Makefile @@ -37,4 +37,7 @@ act: changelog: @git pull origin --tags > /dev/null - @git log $(shell git describe --tags --abbrev=0 HEAD)^..HEAD --pretty=format:'- %s' \ No newline at end of file + @git log $(shell git describe --tags --abbrev=0 HEAD)^..HEAD --pretty=format:'- %s' + +test34: + docker build -f Dockerfile-test . diff --git a/twscrape/account.py b/twscrape/account.py index 1f44d7a..b2a0bfe 100644 --- a/twscrape/account.py +++ b/twscrape/account.py @@ -25,6 +25,7 @@ class Account(JSONTrait): proxy: str | None = None error_msg: str | None = None last_used: datetime | None = None + _tx: str | None = None @staticmethod def from_rs(rs: sqlite3.Row): diff --git a/twscrape/accounts_pool.py b/twscrape/accounts_pool.py index 7e4c729..c62eeac 100644 --- a/twscrape/accounts_pool.py +++ b/twscrape/accounts_pool.py @@ -1,5 +1,7 @@ # ruff: noqa: E501 import asyncio +import sqlite3 +import uuid from datetime import datetime, timezone from fake_useragent import UserAgent @@ -148,13 +150,25 @@ class AccountsPool: LIMIT 1 """ - q2 = f""" - UPDATE accounts SET locks = json_set(locks, '$.{queue}', datetime('now', '+15 minutes')) - WHERE username = ({q1}) - RETURNING * - """ + if int(sqlite3.sqlite_version_info[1]) >= 35: + qs = f""" + UPDATE accounts SET locks = json_set(locks, '$.{queue}', datetime('now', '+15 minutes')) + WHERE username = ({q1}) + RETURNING * + """ + rs = await fetchone(self._db_file, qs) + else: + tx = uuid.uuid4().hex + qs = f""" + UPDATE accounts + SET locks = json_set(locks, '$.{queue}', datetime('now', '+15 minutes')), _tx = '{tx}' + WHERE username = ({q1}) + """ + await execute(self._db_file, qs) + + qs = f"SELECT * FROM accounts WHERE _tx = '{tx}'" + rs = await fetchone(self._db_file, qs) - rs = await fetchone(self._db_file, q2) return Account.from_rs(rs) if rs else None async def get_for_queue_or_wait(self, queue: str) -> Account: diff --git a/twscrape/cli.py b/twscrape/cli.py index 40b1b95..ea6be77 100644 --- a/twscrape/cli.py +++ b/twscrape/cli.py @@ -33,8 +33,8 @@ async def main(args): if args.command == "version": print(f"twscrape: {version('twscrape')}") - print(f"SQlite client: {sqlite3.version}") - print(f"SQlite runtime: {sqlite3.sqlite_version} ({await get_sqlite_version()})") + print(f"SQLite client: {sqlite3.version}") + print(f"SQLite runtime: {sqlite3.sqlite_version} ({await get_sqlite_version()})") return logger.debug(f"Using database: {args.db}") diff --git a/twscrape/db.py b/twscrape/db.py index 1279170..5d7da3c 100644 --- a/twscrape/db.py +++ b/twscrape/db.py @@ -69,9 +69,13 @@ async def migrate(db: aiosqlite.Connection): await db.execute("ALTER TABLE accounts ADD COLUMN stats TEXT DEFAULT '{}' NOT NULL") await db.execute("ALTER TABLE accounts ADD COLUMN last_used TEXT DEFAULT NULL") + async def v3(): + await db.execute("ALTER TABLE accounts ADD COLUMN _tx TEXT DEFAULT NULL") + migrations = { 1: v1, 2: v2, + 3: v3, } logger.debug(f"Current migration v{uv} (latest v{len(migrations)})")