From ce30df0520cd3a0b7976aa19154e06bf71635323 Mon Sep 17 00:00:00 2001 From: Darryl Nixon Date: Fri, 2 Jun 2023 19:40:26 -0700 Subject: [PATCH] Add initial alembic migration before turning in --- .pre-commit-config.yaml | 2 +- alembic.ini | 5 +++- migrations/env.py | 32 ++++++++++++------------ migrations/script.py.mako | 1 + migrations/versions/e12aedb1fb49_init.py | 29 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 migrations/versions/e12aedb1fb49_init.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0081b85..fad465a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,6 @@ repos: - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - exclude: 'ghostforge/static/js/es6/faker-8.0.2.mjs' - repo: https://github.com/asottile/reorder_python_imports rev: v3.9.0 hooks: @@ -27,3 +26,4 @@ repos: rev: 6.0.0 hooks: - id: flake8 + exclude: ^migrations/versions/ diff --git a/alembic.ini b/alembic.ini index 5684cf6..ce27957 100644 --- a/alembic.ini +++ b/alembic.ini @@ -58,7 +58,10 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = postgresql+asyncpg://%(POSTGRES_USER)s:%(POSTGRES_PASSWORD)s@%(POSTGRES_CONTAINER)s:5432/%(POSTGRES_DB)s +# This variable is replaced at runtime in env.py with environment variables +# since they can't be nicely replaced/substituted within a configuration file +sqlalchemy.url = placeholder + [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/migrations/env.py b/migrations/env.py index 4bf02cc..4b590e3 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,29 +1,17 @@ import asyncio -import os from logging.config import fileConfig +from os import getenv as env from alembic import context from sqlalchemy import pool from sqlalchemy.engine import Connection from sqlalchemy.ext.asyncio import async_engine_from_config - -from ghostforge.db import Base - -# from ghostforge.models import User +from sqlmodel import SQLModel # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config -section = config.config_ini_section - -# this is ghostforge-specific, and loads the POSTGRES_* -# environment variables from .env into the alembic context -# for use in dynamically building the postgres URL string. -for var in os.environ: - if var.startswith("POSTGRES_"): - config.set_section_option(section, var, os.environ.get(var)) - # Interpret the config file for Python logging. # This line sets up loggers basically. if config.config_file_name is not None: @@ -33,7 +21,7 @@ if config.config_file_name is not None: # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata -target_metadata = Base.metadata +target_metadata = SQLModel.metadata # other values from the config, defined by the needs of env.py, # can be acquired: @@ -78,8 +66,20 @@ async def run_async_migrations() -> None: """ + cfg = config.get_section(config.config_ini_section, {}) + cfg["sqlalchemy.url"] = ( + "postgresql+asyncpg://" + + env("POSTGRES_USER") + + ":" + + env("POSTGRES_PASSWORD") + + "@" + + env("POSTGRES_CONTAINER") + + ":5432/" + + env("POSTGRES_DB") + ) + connectable = async_engine_from_config( - config.get_section(config.config_ini_section, {}), + cfg, prefix="sqlalchemy.", poolclass=pool.NullPool, ) diff --git a/migrations/script.py.mako b/migrations/script.py.mako index 55df286..3124b62 100644 --- a/migrations/script.py.mako +++ b/migrations/script.py.mako @@ -7,6 +7,7 @@ Create Date: ${create_date} """ from alembic import op import sqlalchemy as sa +import sqlmodel ${imports if imports else ""} # revision identifiers, used by Alembic. diff --git a/migrations/versions/e12aedb1fb49_init.py b/migrations/versions/e12aedb1fb49_init.py new file mode 100644 index 0000000..aa4298a --- /dev/null +++ b/migrations/versions/e12aedb1fb49_init.py @@ -0,0 +1,29 @@ +"""init + +Revision ID: e12aedb1fb49 +Revises: +Create Date: 2023-06-02 19:37:26.134573 + +""" +import sqlalchemy as sa +import sqlmodel +from alembic import op + + +# revision identifiers, used by Alembic. +revision = "e12aedb1fb49" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ###