generated from dopt-python/py311
add database migration tooling with alembic
This commit is contained in:
1
alembic/README
Normal file
1
alembic/README
Normal file
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
80
alembic/env.py
Normal file
80
alembic/env.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from logging.config import fileConfig
|
||||
from pathlib import Path
|
||||
|
||||
import dotenv
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from wce_crm import constants, db
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
target_metadata = db.MD_MAIN
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
config.set_main_option("sqlalchemy.url", f"sqlite:///{constants.Config.DB_PATH_MAIN}")
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
28
alembic/script.py.mako
Normal file
28
alembic/script.py.mako
Normal file
@@ -0,0 +1,28 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,91 @@
|
||||
"""added new column and renamed table for initial recording
|
||||
|
||||
Revision ID: 5f2af5179c47
|
||||
Revises:
|
||||
Create Date: 2026-06-17 16:06:46.421562
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "5f2af5179c47"
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.rename_table("grunderfassung_unternehmen", "grunderfassung")
|
||||
op.add_column(
|
||||
"grunderfassung",
|
||||
sa.Column("Metadaten_wiedereintrittsdatum", sa.Date, nullable=True, default=None),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"grunderfassung_unternehmen",
|
||||
sa.Column("erfassung_id", sa.INTEGER(), nullable=False),
|
||||
sa.Column("Metadaten_erstellung", sa.DATETIME(), nullable=True),
|
||||
sa.Column("Metadaten_aktualisierung", sa.DATETIME(), nullable=True),
|
||||
sa.Column("Metadaten_nutzer", sa.VARCHAR(length=20), nullable=True),
|
||||
sa.Column("Arbeitserfahrung", sa.TEXT(), nullable=True),
|
||||
sa.Column("Grunderfassung_fallnummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Grunderfassung_notiz", sa.TEXT(), nullable=True),
|
||||
sa.Column("HoehereBildung", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_adresse", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_anrede_anschrift", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_email", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_festnetznummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_funktion_beziehung", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_mobilfunknummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_name", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_name_partner", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_titel", sa.TEXT(), nullable=True),
|
||||
sa.Column("Kontaktperson__KP_vorname", sa.TEXT(), nullable=True),
|
||||
sa.Column("Partnersuche__kanal_aufmerksamkeit", sa.TEXT(), nullable=True),
|
||||
sa.Column("Partnersuche__person_suche", sa.INTEGER(), nullable=True),
|
||||
sa.Column("Partnersuche__un_suche", sa.INTEGER(), nullable=True),
|
||||
sa.Column("Projektrelevanz__relevanz", sa.TEXT(), nullable=True),
|
||||
sa.Column("Projektrelevanz__foerderperiode", sa.TEXT(), nullable=True),
|
||||
sa.Column("Schulbildung", sa.TEXT(), nullable=True),
|
||||
sa.Column("Sprachkenntnisse", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__PLZ", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__anrede_anschrift", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__anzahl_kinder__alter", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__anzahl_kinder__anzahl", sa.INTEGER(), nullable=True),
|
||||
sa.Column("Stammdaten__aufenthaltsort", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__bundesland", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__land", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__email", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__familienstand", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__festnetznummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__geburtsdatum", sa.DATE(), nullable=True),
|
||||
sa.Column("Stammdaten__hausnummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__herkunftsland", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__mobilfunknummer", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__name", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__ort", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__rueckkehrer", sa.BOOLEAN(), nullable=True),
|
||||
sa.Column("Stammdaten__staatsangehoerigkeit", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__strasse", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__titel", sa.TEXT(), nullable=True),
|
||||
sa.Column("Stammdaten__vorname", sa.TEXT(), nullable=True),
|
||||
sa.Column("WeitereInfos__WI_arbeitsstatus", sa.TEXT(), nullable=True),
|
||||
sa.Column("WeitereInfos__WI_aufenthaltstitel", sa.TEXT(), nullable=True),
|
||||
sa.Column("WeitereInfos__WI_deutsch_sprache", sa.TEXT(), nullable=True),
|
||||
sa.Column("WeitereInfos__WI_gueltigkeit_aufenthaltstitel", sa.DATE(), nullable=True),
|
||||
sa.Column("WeitereInfos__WI_meldung_institution", sa.TEXT(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("erfassung_id"),
|
||||
)
|
||||
op.drop_table("grunderfassung")
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user