Add initial alembic migration before turning in

This commit is contained in:
Darryl Nixon 2023-06-02 19:40:26 -07:00
parent 425780f8a4
commit ce30df0520
5 changed files with 51 additions and 18 deletions

View file

@ -6,7 +6,6 @@ repos:
- id: end-of-file-fixer - id: end-of-file-fixer
- id: check-yaml - id: check-yaml
- id: check-added-large-files - id: check-added-large-files
exclude: 'ghostforge/static/js/es6/faker-8.0.2.mjs'
- repo: https://github.com/asottile/reorder_python_imports - repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0 rev: v3.9.0
hooks: hooks:
@ -27,3 +26,4 @@ repos:
rev: 6.0.0 rev: 6.0.0
hooks: hooks:
- id: flake8 - id: flake8
exclude: ^migrations/versions/

View file

@ -58,7 +58,10 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako # are written from script.py.mako
# output_encoding = utf-8 # 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]
# post_write_hooks defines scripts or Python functions that are run # post_write_hooks defines scripts or Python functions that are run

View file

@ -1,29 +1,17 @@
import asyncio import asyncio
import os
from logging.config import fileConfig from logging.config import fileConfig
from os import getenv as env
from alembic import context from alembic import context
from sqlalchemy import pool from sqlalchemy import pool
from sqlalchemy.engine import Connection from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config from sqlalchemy.ext.asyncio import async_engine_from_config
from sqlmodel import SQLModel
from ghostforge.db import Base
# from ghostforge.models import User
# this is the Alembic Config object, which provides # this is the Alembic Config object, which provides
# access to the values within the .ini file in use. # access to the values within the .ini file in use.
config = context.config 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. # Interpret the config file for Python logging.
# This line sets up loggers basically. # This line sets up loggers basically.
if config.config_file_name is not None: if config.config_file_name is not None:
@ -33,7 +21,7 @@ if config.config_file_name is not None:
# for 'autogenerate' support # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # 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, # other values from the config, defined by the needs of env.py,
# can be acquired: # 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( connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}), cfg,
prefix="sqlalchemy.", prefix="sqlalchemy.",
poolclass=pool.NullPool, poolclass=pool.NullPool,
) )

View file

@ -7,6 +7,7 @@ Create Date: ${create_date}
""" """
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
import sqlmodel
${imports if imports else ""} ${imports if imports else ""}
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.

View file

@ -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 ###