feat: use Alembic to initialize the database

This completely removes the previous DB patching mechanism.
When this is first run for an existing installation of Unwind, depending
on its version it might lead to problems because the database's schema
won't match the code.
To avoid that issue, when upgrading Unwind to this version make sure to
STOP the old application, install this new version but DON'T start it,
instead use `alembic upgrade head` to run the outstanding patches, and
only then start the application.
This commit is contained in:
ducklet 2024-05-19 02:25:36 +02:00
parent 5e4e70c9dc
commit 1ea09c1a45
20 changed files with 72 additions and 560 deletions

View file

@ -91,13 +91,18 @@ async def run_async_migrations() -> None:
await connectable.dispose()
def run_migrations_online_async() -> None:
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
# Support having a (sync) connection passed in from another script.
if (conn := config.attributes.get("connection")) and isinstance(
conn, sa.Connection
):
do_run_migrations(conn)
else:
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online_async()
run_migrations_online()

View file

@ -0,0 +1,38 @@
"""remove db_patches table
We replace our old patch process with Alembic's.
Revision ID: 8b06e4916840
Revises: f17c7ca9afa4
Create Date: 2024-05-19 00:11:06.730421+00:00
"""
from typing import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "8b06e4916840"
down_revision: str | None = "f17c7ca9afa4"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("db_patches")
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"db_patches",
sa.Column("id", sa.INTEGER(), nullable=False),
sa.Column("current", sa.VARCHAR(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###