Migrate Alembic to Alchemical format
This commit is contained in:
parent
fb5376cdf0
commit
0c03db14d4
53
alembic.ini
53
alembic.ini
@ -1,18 +1,29 @@
|
|||||||
# A generic, single database configuration.
|
# a multi-database configuration.
|
||||||
|
|
||||||
[alembic]
|
[alembic]
|
||||||
|
# this must be configured to point to the Alchemical database instance
|
||||||
|
# there are two components separated by a colon:
|
||||||
|
# the left part is the import path to the module containing the database instance
|
||||||
|
# the right part is the name of the database instance, typically 'db'
|
||||||
|
alchemical_db = models:db
|
||||||
|
|
||||||
# path to migration scripts
|
# path to migration scripts
|
||||||
script_location = migrations
|
script_location = migrations
|
||||||
|
|
||||||
# template used to generate migration files
|
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||||
# file_template = %%(rev)s_%%(slug)s
|
# Uncomment the line below if you want the files to be prepended with date and time
|
||||||
|
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||||
|
# for all available tokens
|
||||||
|
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||||
|
|
||||||
# sys.path path, will be prepended to sys.path if present.
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
# defaults to the current working directory.
|
# defaults to the current working directory.
|
||||||
prepend_sys_path = .
|
prepend_sys_path = app
|
||||||
|
|
||||||
# timezone to use when rendering the date
|
# timezone to use when rendering the date within the migration file
|
||||||
# within the migration file as well as the filename.
|
# as well as the filename.
|
||||||
|
# If specified, requires the python-dateutil library that can be
|
||||||
|
# installed by adding `alembic[tz]` to the pip requirements
|
||||||
# string value is passed to dateutil.tz.gettz()
|
# string value is passed to dateutil.tz.gettz()
|
||||||
# leave blank for localtime
|
# leave blank for localtime
|
||||||
# timezone =
|
# timezone =
|
||||||
@ -30,30 +41,36 @@ prepend_sys_path = .
|
|||||||
# versions/ directory
|
# versions/ directory
|
||||||
# sourceless = false
|
# sourceless = false
|
||||||
|
|
||||||
# version location specification; this defaults
|
# version location specification; This defaults
|
||||||
# to migrations/versions. When using multiple version
|
# to migrations/versions. When using multiple version
|
||||||
# directories, initial revisions must be specified with --version-path
|
# directories, initial revisions must be specified with --version-path.
|
||||||
# version_locations = %(here)s/bar %(here)s/bat migrations/versions
|
# The path separator used here should be the separator specified by "version_path_separator" below.
|
||||||
|
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
|
||||||
|
|
||||||
|
# version path separator; As mentioned above, this is the character used to split
|
||||||
|
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
|
||||||
|
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
|
||||||
|
# Valid values for version_path_separator are:
|
||||||
|
#
|
||||||
|
# version_path_separator = :
|
||||||
|
# version_path_separator = ;
|
||||||
|
# version_path_separator = space
|
||||||
|
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
|
||||||
|
|
||||||
# the output encoding used when revision files
|
# the output encoding used when revision files
|
||||||
# are written from script.py.mako
|
# are written from script.py.mako
|
||||||
# output_encoding = utf-8
|
# output_encoding = utf-8
|
||||||
|
|
||||||
sqlalchemy.url = SET
|
|
||||||
# sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod
|
|
||||||
# sqlalchemy.url = mysql+mysqldb://dev_musicmuster:dev_musicmuster@localhost/dev_musicmuster
|
|
||||||
# sqlalchemy.url = mysql+mysqldb://dev_musicmuster:dev_musicmuster@localhost/dev_musicmuster_carts
|
|
||||||
|
|
||||||
[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
|
||||||
# on newly generated revision scripts. See the documentation for further
|
# on newly generated revision scripts. See the documentation for further
|
||||||
# detail and examples
|
# detail and examples
|
||||||
|
|
||||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
# hooks=black
|
# hooks = black
|
||||||
# black.type=console_scripts
|
# black.type = console_scripts
|
||||||
# black.entrypoint=black
|
# black.entrypoint = black
|
||||||
# black.options=-l 79
|
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
# Logging configuration
|
# Logging configuration
|
||||||
[loggers]
|
[loggers]
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
Generic single-database configuration.
|
Alembic configuration for Alchemical
|
||||||
|
|||||||
@ -1,84 +1,27 @@
|
|||||||
import sys
|
from importlib import import_module
|
||||||
import os
|
|
||||||
|
|
||||||
from logging.config import fileConfig
|
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config
|
|
||||||
from sqlalchemy import pool
|
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
|
from alchemical.alembic.env import run_migrations
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
# Interpret the config file for Python logging.
|
# import the application's Alchemical instance
|
||||||
# This line sets up loggers basically.
|
try:
|
||||||
fileConfig(config.config_file_name)
|
import_mod, db_name = config.get_main_option('alchemical_db', '').split(
|
||||||
|
':')
|
||||||
# add your model's MetaData object here
|
db = getattr(import_module(import_mod), db_name)
|
||||||
# for 'autogenerate' support
|
except (ModuleNotFoundError, AttributeError):
|
||||||
# from myapp import mymodel
|
raise ValueError(
|
||||||
# target_metadata = mymodel.Base.metadata
|
'Could not import the Alchemical database instance. '
|
||||||
# https://stackoverflow.com/questions/32032940/how-to-import-the-own-model-into-myproject-alembic-env-py
|
'Ensure that the alchemical_db setting in alembic.ini is correct.'
|
||||||
path = os.path.dirname(os.path.dirname(__file__))
|
|
||||||
sys.path.insert(0, path)
|
|
||||||
sys.path.insert(0, os.path.join(path, "app"))
|
|
||||||
from app.models import Base
|
|
||||||
target_metadata = Base.metadata
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
|
|
||||||
def run_migrations_offline():
|
|
||||||
"""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():
|
# run the migration engine
|
||||||
context.run_migrations()
|
# The dictionary provided as second argument includes options to pass to the
|
||||||
|
# Alembic context. For details on what other options are available, see
|
||||||
|
# https://alembic.sqlalchemy.org/en/latest/autogenerate.html
|
||||||
def run_migrations_online():
|
run_migrations(db, {
|
||||||
"""Run migrations in 'online' mode.
|
'render_as_batch': True,
|
||||||
|
'compare_type': True,
|
||||||
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()
|
|
||||||
|
|||||||
110
migrations/versions/2caa3d37f211_add_tracks_intro_column.py
Normal file
110
migrations/versions/2caa3d37f211_add_tracks_intro_column.py
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
"""add Tracks.intro column
|
||||||
|
|
||||||
|
Revision ID: 2caa3d37f211
|
||||||
|
Revises: 5bb2c572e1e5
|
||||||
|
Create Date: 2024-05-07 20:06:00.845979
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import mysql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '2caa3d37f211'
|
||||||
|
down_revision = '5bb2c572e1e5'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade(engine_name: str) -> None:
|
||||||
|
globals()["upgrade_%s" % engine_name]()
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade(engine_name: str) -> None:
|
||||||
|
globals()["downgrade_%s" % engine_name]()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade_() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('carts', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('name',
|
||||||
|
existing_type=mysql.VARCHAR(length=256),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('notecolours', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('substring',
|
||||||
|
existing_type=mysql.VARCHAR(length=256),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('colour',
|
||||||
|
existing_type=mysql.VARCHAR(length=21),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('enabled',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('is_regex',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('is_casesensitive',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('playdates', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('lastplayed',
|
||||||
|
existing_type=mysql.DATETIME(),
|
||||||
|
nullable=False)
|
||||||
|
batch_op.alter_column('track_id',
|
||||||
|
existing_type=mysql.INTEGER(display_width=11),
|
||||||
|
nullable=False)
|
||||||
|
|
||||||
|
with op.batch_alter_table('playlists', schema=None) as batch_op:
|
||||||
|
batch_op.drop_index('tab')
|
||||||
|
|
||||||
|
with op.batch_alter_table('tracks', schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column('intro', sa.Integer(), nullable=True))
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade_() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('tracks', schema=None) as batch_op:
|
||||||
|
batch_op.drop_column('intro')
|
||||||
|
|
||||||
|
with op.batch_alter_table('playlists', schema=None) as batch_op:
|
||||||
|
batch_op.create_index('tab', ['tab'], unique=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('playdates', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('track_id',
|
||||||
|
existing_type=mysql.INTEGER(display_width=11),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('lastplayed',
|
||||||
|
existing_type=mysql.DATETIME(),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('notecolours', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('is_casesensitive',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('is_regex',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('enabled',
|
||||||
|
existing_type=mysql.TINYINT(display_width=1),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('colour',
|
||||||
|
existing_type=mysql.VARCHAR(length=21),
|
||||||
|
nullable=True)
|
||||||
|
batch_op.alter_column('substring',
|
||||||
|
existing_type=mysql.VARCHAR(length=256),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
with op.batch_alter_table('carts', schema=None) as batch_op:
|
||||||
|
batch_op.alter_column('name',
|
||||||
|
existing_type=mysql.VARCHAR(length=256),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user