diff --git a/alembic.ini b/alembic.ini index 2f32637..c52f478 100644 --- a/alembic.ini +++ b/alembic.ini @@ -1,18 +1,29 @@ -# A generic, single database configuration. +# a multi-database configuration. [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 script_location = migrations -# template used to generate migration files -# file_template = %%(rev)s_%%(slug)s +# template used to generate migration file names; The default value is %%(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. # defaults to the current working directory. -prepend_sys_path = . +prepend_sys_path = app -# timezone to use when rendering the date -# within the migration file as well as the filename. +# timezone to use when rendering the date within the migration file +# 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() # leave blank for localtime # timezone = @@ -30,30 +41,36 @@ prepend_sys_path = . # versions/ directory # sourceless = false -# version location specification; this defaults +# version location specification; This defaults # to migrations/versions. When using multiple version -# directories, initial revisions must be specified with --version-path -# version_locations = %(here)s/bar %(here)s/bat migrations/versions +# directories, initial revisions must be specified with --version-path. +# 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 # are written from script.py.mako # 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 defines scripts or Python functions that are run # on newly generated revision scripts. See the documentation for further # detail and examples # format using "black" - use the console_scripts runner, against the "black" entrypoint -# hooks=black -# black.type=console_scripts -# black.entrypoint=black -# black.options=-l 79 +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME # Logging configuration [loggers] diff --git a/migrations/README b/migrations/README index 98e4f9c..df8d004 100644 --- a/migrations/README +++ b/migrations/README @@ -1 +1 @@ -Generic single-database configuration. \ No newline at end of file +Alembic configuration for Alchemical diff --git a/migrations/env.py b/migrations/env.py index 4790b3c..027fd36 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,84 +1,27 @@ -import sys -import os - -from logging.config import fileConfig - -from sqlalchemy import engine_from_config -from sqlalchemy import pool - +from importlib import import_module from alembic import context +from alchemical.alembic.env import run_migrations # 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. -fileConfig(config.config_file_name) - -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -# https://stackoverflow.com/questions/32032940/how-to-import-the-own-model-into-myproject-alembic-env-py -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"}, +# import the application's Alchemical instance +try: + import_mod, db_name = config.get_main_option('alchemical_db', '').split( + ':') + db = getattr(import_module(import_mod), db_name) +except (ModuleNotFoundError, AttributeError): + raise ValueError( + 'Could not import the Alchemical database instance. ' + 'Ensure that the alchemical_db setting in alembic.ini is correct.' ) - with context.begin_transaction(): - context.run_migrations() - - -def run_migrations_online(): - """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() +# run the migration engine +# 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 +run_migrations(db, { + 'render_as_batch': True, + 'compare_type': True, +}) diff --git a/migrations/versions/2caa3d37f211_add_tracks_intro_column.py b/migrations/versions/2caa3d37f211_add_tracks_intro_column.py new file mode 100644 index 0000000..c3a68da --- /dev/null +++ b/migrations/versions/2caa3d37f211_add_tracks_intro_column.py @@ -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 ### +