websauna.system.devop.alembic module

Support for Alembic SQL migrations.

  1. Each Python package needs to get its own alembic_history table

  2. In “objects to consider” function we resolve the object’s package and compare whether it is the package for which we try to create migrations for

  3. All migration scripts live inside the package, in alembic/ folder next to setup.py

websauna.system.devop.alembic.consider_class_for_migration(klass, allowed_packages)[source]
Return type


websauna.system.devop.alembic.consider_module_for_migration(mod, allowed_packages)[source]
  • mod (str) – Dotted name to a module

  • allowed_packages (List) – List of allowed packages

Return type




Return class reference mapped to table.


tablename – String with name of table.

Return type



Class reference or None.

websauna.system.devop.alembic.get_migration_table_name(allowed_packages, current_package_name)[source]

Convert Python package name to migration table name.

Return type



Get the SQLAlchemy MetaData instance which contains declarative tables only from a certain Python packagek.

We get all tables which have been registered against the current Base model. Then we grab Base.metadata and drop out all tables which we think are not part of our migration run.

websauna.system.devop.alembic.logger = None

Defined later because of initialization order


Return list of package names we want to consider from Alembic extra arguments.


Alembic env.py script entry point for Websauna application.

Initialize the application, load models and pass control to Alembic migration handler.


package (str) – String of the Python package name whose model the migration concerns. E.g. the name of the current Websauna package.

websauna.system.devop.alembic.run_migrations_offline(url, target_metadata, version_table, include_object)[source]

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.

websauna.system.devop.alembic.run_migrations_online(engine, target_metadata, version_table, include_object)[source]

Run migrations in ‘online’ mode.

In this scenario we need to create an Engine and associate a connection with the context.