From 1700ac6fbb9599d2c1448c4934a088ddc1db2c44 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Thu, 22 Jun 2023 16:38:09 +0100 Subject: [PATCH 1/2] add 'aiosqlite' for async SQLite Signed-off-by: Grant Ramsay --- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index cf7684c..eb82b01 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,5 +1,20 @@ # This file is automatically @generated by Poetry 1.5.0 and should not be changed by hand. +[[package]] +name = "aiosqlite" +version = "0.19.0" +description = "asyncio bridge to the standard sqlite3 module" +optional = false +python-versions = ">=3.7" +files = [ + {file = "aiosqlite-0.19.0-py3-none-any.whl", hash = "sha256:edba222e03453e094a3ce605db1b970c4b3376264e56f32e2a4959f948d66a96"}, + {file = "aiosqlite-0.19.0.tar.gz", hash = "sha256:95ee77b91c8d2808bd08a59fbebf66270e9090c3d92ffbf260dc0db0b979577d"}, +] + +[package.extras] +dev = ["aiounittest (==1.4.1)", "attribution (==1.6.2)", "black (==23.3.0)", "coverage[toml] (==7.2.3)", "flake8 (==5.0.4)", "flake8-bugbear (==23.3.12)", "flit (==3.7.1)", "mypy (==1.2.0)", "ufmt (==2.1.0)", "usort (==1.0.6)"] +docs = ["sphinx (==6.1.3)", "sphinx-mdinclude (==0.5.3)"] + [[package]] name = "anyio" version = "3.7.0" @@ -1833,4 +1848,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<4.0" -content-hash = "3f5d72591a4ca7bc67d386bc23f2d4770d46421bba4103032cf5bef844077b24" +content-hash = "1d69c021f8f97d962e07d66a10e5f4929029df316d5a5abc202bc47fb687953b" diff --git a/pyproject.toml b/pyproject.toml index 535d59b..6f98571 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ fastapi = "^0.97.0" asyncpg = "^0.27.0" sqlalchemy = {extras = ["asyncio"], version = "^2.0.16"} uvicorn = {extras = ["standard"], version = "^0.22.0"} +aiosqlite = "^0.19.0" [tool.poetry.group.dev.dependencies] # linting and formatting tools From 33fd7feeed104274f2718edaf76f4fb1de2c31e9 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Thu, 22 Jun 2023 16:59:14 +0100 Subject: [PATCH 2/2] add info on using sqlite3 instead of postgres Signed-off-by: Grant Ramsay --- .gitignore | 1 + README.md | 12 ++++++++++++ db.py | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/.gitignore b/.gitignore index 9c44576..ee4e36e 100644 --- a/.gitignore +++ b/.gitignore @@ -207,3 +207,4 @@ cython_debug/ # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,linux,python # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) +test.db diff --git a/README.md b/README.md index 5d9f986..13fe5af 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ - [Installation](#installation) - [Usage](#usage) - [Local Postgres server using Docker](#local-postgres-server-using-docker) + - [Use SQLite instead of PostgreSQL](#use-sqlite-instead-of-postgresql) - [License](#license) ## Introduction @@ -84,6 +85,17 @@ docker exec -it postgres psql -U postgres This will allow you to edit or delete the database or records. +### Use SQLite instead of PostgreSQL + +For testing purposes, you can also use SQLite instead of PostgreSQL. To do so, +open the [dp.py](db.py) file and comment out the PostgreSQL database in the +`DATABASE_URL` environment variable and uncomment the SQLite database. + +```python +# DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres" +DATABASE_URL = "sqlite+aiosqlite:///./test.db" +``` + ## License This project is licensed under the terms of the MIT license. diff --git a/db.py b/db.py index bc0c46a..0a3dc82 100644 --- a/db.py +++ b/db.py @@ -3,6 +3,10 @@ from sqlalchemy.orm import declarative_base DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost/postgres" +# DATABASE_URL = "sqlite+aiosqlite:///./test.db" +# Note that (as far as I can tell from the docs and searching) there is no need +# to add 'check_same_thread=False' to the sqlite connection string, as +# SQLAlchemy version 1.4+ will automatically add it for you when using SQLite. engine = create_async_engine(DATABASE_URL, echo=False) Base = declarative_base()