Add production support for SQLite
CI / Build and push docker image (push) Successful in 3m9s
CI / Build and push OCSP image (push) Successful in 33s

This commit is contained in:
2026-10-28 01:23:28 +00:00
parent 3918a2ac71
commit 9da68a9625
11 changed files with 486 additions and 20 deletions
+12 -7
View File
@@ -142,13 +142,18 @@ def _database_url_from_parts(
name: str,
options: str,
) -> str:
netloc = quote(user, safe="")
if password:
netloc += ":" + quote(password, safe="")
netloc += "@" + host
if port:
netloc += ":" + port
url = f"{engine}://{netloc}/{name}"
if engine == "sqlite":
# DATABASE_NAME is a file path for SQLite (or :memory: for tests), so
# network credentials and host/port settings do not apply.
url = f"sqlite://{name}"
else:
netloc = quote(user, safe="")
if password:
netloc += ":" + quote(password, safe="")
netloc += "@" + host
if port:
netloc += ":" + port
url = f"{engine}://{netloc}/{name}"
options = options.lstrip("?")
if options:
url += "?" + options
@@ -23,10 +23,10 @@ It deliberately avoids the global-fallback singleton
per process and would therefore break across event loops.
Schema management is split by backend: in-memory sqlite databases (the
test suite) get ``generate_schemas`` on every fresh context; Postgres
schemas are owned by aerich migrations and must be applied externally
(``aerich upgrade``, run by the ``db-migrate`` compose service) before
the app serves requests.
test suite) get ``generate_schemas`` on every fresh context; Postgres and
file-backed SQLite schemas are owned by aerich migrations and must be
applied externally (``aerich upgrade``, run by the ``db-migrate`` compose
service) before the app serves requests.
"""
from __future__ import annotations
@@ -104,12 +104,17 @@ class TortoiseMixin(KayaMixin):
modules={"models": self._models_modules},
)
log.info("database context initialized (%s)", _safe_url(self._database_url))
# Schema creation is only done for sqlite (in-memory test
# databases). Postgres schemas are managed by aerich migrations
# Only ephemeral test databases are created implicitly. Postgres
# and persistent SQLite schemas are managed by aerich migrations
# (applied by the db-migrate compose service / `aerich upgrade`).
if self._database_url.startswith("sqlite"):
connection = ctx.connections.get("default")
is_memory_sqlite = (
connection.capabilities.dialect == "sqlite"
and getattr(connection, "filename", None) == ":memory:"
)
if is_memory_sqlite:
await ctx.generate_schemas()
log.info("sqlite schemas generated")
log.info("in-memory sqlite schemas generated")
return ctx
async def ensure_context(self) -> None: