From bb92f15636b0e2d5b31f80b4f01d91863a3a2cfd Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 19 Dec 2021 22:11:57 +0100 Subject: [PATCH] feat(db): Add database migration support, implements #19 Migrations allow us to keep track of database changes and upgrading databases if needed. --- flaschengeist/app.py | 3 +- flaschengeist/database.py | 24 +++++++++++++ migrations/alembic.ini | 52 ++++++++++++++++++++++++++++ migrations/env.py | 73 +++++++++++++++++++++++++++++++++++++++ migrations/script.py.mako | 25 ++++++++++++++ setup.cfg | 6 ++-- 6 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako diff --git a/flaschengeist/app.py b/flaschengeist/app.py index f2f1664..5c3a9d7 100644 --- a/flaschengeist/app.py +++ b/flaschengeist/app.py @@ -98,10 +98,11 @@ def create_app(test_config=None): CORS(app) with app.app_context(): - from flaschengeist.database import db + from flaschengeist.database import db, migrate configure_app(app, test_config) db.init_app(app) + migrate.init_app(app, db, compare_type=True) __load_plugins(app) @app.route("/", methods=["GET"]) diff --git a/flaschengeist/database.py b/flaschengeist/database.py index ebda993..4da914a 100644 --- a/flaschengeist/database.py +++ b/flaschengeist/database.py @@ -1,3 +1,6 @@ +import os +from flask import current_app +from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy from sqlalchemy import MetaData @@ -14,6 +17,27 @@ metadata = MetaData( db = SQLAlchemy(metadata=metadata) +migrate = Migrate() + + +@migrate.configure +def configure_alembic(config): + # Load migration paths from plugins + migrations = [str(p.migrations_path) for p in current_app.config["FG_PLUGINS"].values() if p and p.migrations_path] + if len(migrations) > 0: + # Get configured paths + paths = config.get_main_option("version_locations") + # Get configured path seperator + sep = config.get_main_option("version_path_separator", "os") + if paths: + # Insert configured paths at the front, before plugin migrations + migrations.insert(0, config.get_main_option("version_locations")) + sep = os.pathsep if sep == "os" else " " if sep == "space" else sep + # write back seperator (we changed it if neither seperator nor locations were specified) + config.set_main_option("version_path_separator", sep) + config.set_main_option("version_locations", sep.join(migrations)) + print(config.get_main_option("version_locations")) + return config def case_sensitive(s): diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 0000000..e1023e2 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,52 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +version_path_separator = os +version_locations = %(here)s/versions + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic,flask_migrate + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 0000000..fd8edac --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,73 @@ +import logging +from logging.config import fileConfig +from flask import current_app +from alembic import context + +# 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) +logger = logging.getLogger("alembic.env") + +config.set_main_option("sqlalchemy.url", str(current_app.extensions["migrate"].db.get_engine().url).replace("%", "%%")) +target_metadata = current_app.extensions["migrate"].db.metadata + + +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) + + 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. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, "autogenerate", False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info("No changes in schema detected.") + + connectable = current_app.extensions["migrate"].db.get_engine() + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions["migrate"].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 0000000..a6f4fdf --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,25 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +import flaschengeist +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/setup.cfg b/setup.cfg index c18feda..770ece4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,16 +22,16 @@ include_package_data = True python_requires = >=3.9 packages = find: install_requires = - Flask >= 2.0 + Flask>=2.0 Pillow>=8.4.0 flask_cors + flask_migrate>=3.1.0 flask_sqlalchemy>=2.5 # Importlib requirement can be dropped when python requirement is >= 3.10 importlib_metadata>=4.3 sqlalchemy>=1.4.26 toml - werkzeug - + werkzeug >= 2.0 [options.extras_require] argon = argon2-cffi