15.2.1. camcops_server.alembic.env

camcops_server/alembic/env.py


Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.


This file configures and runs Alembic.

It is loaded directly by Alembic, via a pseudo-“main” environment.

camcops_server.alembic.env.custom_compare_type(context: alembic.runtime.migration.MigrationContext, inspected_column: sqlalchemy.sql.schema.Column, metadata_column: sqlalchemy.sql.schema.Column, inspected_type: sqlalchemy.sql.type_api.TypeEngine, metadata_type: sqlalchemy.sql.type_api.TypeEngine) Optional[bool][source]

Perform type comparison?

Parameters
  • context – frontend to database

  • inspected_column – column from the database

  • metadata_column – column from the SQLAlchemy metadata

  • inspected_type – column type reflected from the database

  • metadata_type – column type from the SQLAlchemy metadata

Returns

False if the metadata type is the same as the inspected type None to allow the default implementation to compare these

A return value of True would mean the two types do not match and should result in a type change operation

Specifically, it detects:

  • MySQL TINYINT(1) is equivalent to SQLAlchemy Boolean(), because TINYINT(1) is the correct instantiation of Boolean().

  • LONGTEXT(collation='utf8mb4_unicode_ci') is the MySQL database version of UnicodeText(length=4294967295)

camcops_server.alembic.env.debug_op_object(op: Union[List, alembic.operations.ops.OpContainer, Tuple], level: int = 0) str[source]

Describes a OpContainer.

camcops_server.alembic.env.process_revision_directives(context_: alembic.runtime.migration.MigrationContext, revision: Tuple[str], directives: List[alembic.operations.ops.MigrationScript]) None[source]

Process autogenerated migration scripts and fix these problems.

camcops_server.alembic.env.run_alembic() None[source]

Run migrations via Alembic.

camcops_server.alembic.env.run_migrations_offline(config: alembic.config.Config, target_metadata: sqlalchemy.sql.schema.MetaData) None[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.

camcops_server.alembic.env.run_migrations_online(config: alembic.config.Config, target_metadata: sqlalchemy.sql.schema.MetaData) None[source]

Run migrations in ‘online’ mode.

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