diff options
Diffstat (limited to 'alembic')
| -rw-r--r-- | alembic/env.py | 93 | ||||
| -rw-r--r-- | alembic/script.py.mako | 28 |
2 files changed, 121 insertions, 0 deletions
diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..ff0b27f --- /dev/null +++ b/alembic/env.py | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | import asyncio | ||
| 2 | from logging.config import fileConfig | ||
| 3 | |||
| 4 | from sqlalchemy import pool | ||
| 5 | from sqlalchemy.engine import Connection | ||
| 6 | from sqlalchemy.ext.asyncio import async_engine_from_config | ||
| 7 | |||
| 8 | import models | ||
| 9 | from alembic import context | ||
| 10 | from settings import database_url | ||
| 11 | |||
| 12 | # this is the Alembic Config object, which provides | ||
| 13 | # access to the values within the .ini file in use. | ||
| 14 | config = context.config | ||
| 15 | config.set_main_option("sqlalchemy.url", database_url) | ||
| 16 | |||
| 17 | # Interpret the config file for Python logging. | ||
| 18 | # This line sets up loggers basically. | ||
| 19 | if config.config_file_name is not None: | ||
| 20 | fileConfig(config.config_file_name) | ||
| 21 | |||
| 22 | # add your model's MetaData object here | ||
| 23 | # for 'autogenerate' support | ||
| 24 | # from myapp import mymodel | ||
| 25 | # target_metadata = mymodel.Base.metadata | ||
| 26 | target_metadata = models.BaseTable.metadata | ||
| 27 | |||
| 28 | # other values from the config, defined by the needs of env.py, | ||
| 29 | # can be acquired: | ||
| 30 | # my_important_option = config.get_main_option("my_important_option") | ||
| 31 | # ... etc. | ||
| 32 | |||
| 33 | |||
| 34 | def run_migrations_offline() -> None: | ||
| 35 | """Run migrations in 'offline' mode. | ||
| 36 | |||
| 37 | This configures the context with just a URL | ||
| 38 | and not an Engine, though an Engine is acceptable | ||
| 39 | here as well. By skipping the Engine creation | ||
| 40 | we don't even need a DBAPI to be available. | ||
| 41 | |||
| 42 | Calls to context.execute() here emit the given string to the | ||
| 43 | script output. | ||
| 44 | |||
| 45 | """ | ||
| 46 | url = config.get_main_option("sqlalchemy.url") | ||
| 47 | context.configure( | ||
| 48 | url=url, | ||
| 49 | target_metadata=target_metadata, | ||
| 50 | literal_binds=True, | ||
| 51 | dialect_opts={"paramstyle": "named"}, | ||
| 52 | render_as_batch=True, | ||
| 53 | ) | ||
| 54 | |||
| 55 | with context.begin_transaction(): | ||
| 56 | context.run_migrations() | ||
| 57 | |||
| 58 | |||
| 59 | def do_run_migrations(connection: Connection) -> None: | ||
| 60 | context.configure(connection=connection, target_metadata=target_metadata) | ||
| 61 | |||
| 62 | with context.begin_transaction(): | ||
| 63 | context.run_migrations() | ||
| 64 | |||
| 65 | |||
| 66 | async def run_async_migrations() -> None: | ||
| 67 | """In this scenario we need to create an Engine | ||
| 68 | and associate a connection with the context. | ||
| 69 | |||
| 70 | """ | ||
| 71 | |||
| 72 | connectable = async_engine_from_config( | ||
| 73 | config.get_section(config.config_ini_section, {}), | ||
| 74 | prefix="sqlalchemy.", | ||
| 75 | poolclass=pool.NullPool, | ||
| 76 | ) | ||
| 77 | |||
| 78 | async with connectable.connect() as connection: | ||
| 79 | await connection.run_sync(do_run_migrations) | ||
| 80 | |||
| 81 | await connectable.dispose() | ||
| 82 | |||
| 83 | |||
| 84 | def run_migrations_online() -> None: | ||
| 85 | """Run migrations in 'online' mode.""" | ||
| 86 | |||
| 87 | asyncio.run(run_async_migrations()) | ||
| 88 | |||
| 89 | |||
| 90 | if context.is_offline_mode(): | ||
| 91 | run_migrations_offline() | ||
| 92 | else: | ||
| 93 | run_migrations_online() | ||
diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..1101630 --- /dev/null +++ b/alembic/script.py.mako | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | """${message} | ||
| 2 | |||
| 3 | Revision ID: ${up_revision} | ||
| 4 | Revises: ${down_revision | comma,n} | ||
| 5 | Create Date: ${create_date} | ||
| 6 | |||
| 7 | """ | ||
| 8 | from typing import Sequence, Union | ||
| 9 | |||
| 10 | from alembic import op | ||
| 11 | import sqlalchemy as sa | ||
| 12 | ${imports if imports else ""} | ||
| 13 | |||
| 14 | # revision identifiers, used by Alembic. | ||
| 15 | revision: str = ${repr(up_revision)} | ||
| 16 | down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)} | ||
| 17 | branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} | ||
| 18 | depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} | ||
| 19 | |||
| 20 | |||
| 21 | def upgrade() -> None: | ||
| 22 | """Upgrade schema.""" | ||
| 23 | ${upgrades if upgrades else "pass"} | ||
| 24 | |||
| 25 | |||
| 26 | def downgrade() -> None: | ||
| 27 | """Downgrade schema.""" | ||
| 28 | ${downgrades if downgrades else "pass"} | ||
