Skip to content

Commit

Permalink
Added versionTableSchema to DBMigration configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
puehringer committed Mar 23, 2020
1 parent 8b3f898 commit 4e2ee7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
"prebuild:python": "node -e \"process.exit(process.env.PHOVEA_SKIP_TESTS === undefined?1:0)\" || npm run test:python",
"prebuild:web": "node -e \"process.exit(process.env.PHOVEA_SKIP_TESTS === undefined?1:0)\" || npm run test:web",
"db-migration:base": "docker-compose run api python phovea_server/__main__.py --env dev db-migration",
"db-migration": "npm run db-migration:base -- exec",
"db-migration:list": "npm run db-migration:base -- list"
"db-migration": "npm run db-migration:base exec --",
"db-migration:list": "npm run db-migration:base list --"
},
"dependencies": {
"@types/d3": "~3.5.36",
Expand Down
9 changes: 7 additions & 2 deletions tdp_core/dbmigration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class DBMigration(object):
DBMigration object stores the required arguments to execute commands using Alembic.
"""

def __init__(self, id: str, db_url: str, script_location: str, auto_upgrade: bool=False):
def __init__(self, id: str, db_url: str, script_location: str, *, auto_upgrade: bool=False, version_table_schema: str=None):
"""
Initializes a new migration object and optionally carries out an upgrade.
:param str id: ID of the migration object
:param str db_url: DB connection url
:param str script_location: Location of the base directory (containing env.py and the versions directory)
:param bool auto_upgrade: True if the migration should automatically upgrade the database to head
:param str version_table_schema: Schema of the alembic version table
"""
if not id or not db_url or not script_location:
raise ValueError('Empty id or db_url or script_location')
Expand All @@ -37,6 +38,7 @@ def __init__(self, id: str, db_url: str, script_location: str, auto_upgrade: boo
self.db_url: str = db_url
self.script_location: str = script_location
self.auto_upgrade: bool = auto_upgrade
self.version_table_schema: str = version_table_schema
self.custom_commands: Dict[str, str] = dict()

# Because we can't easily pass "-1" as npm argument, we add a custom command for that without the space
Expand Down Expand Up @@ -109,6 +111,8 @@ def execute(self, arguments: List[str] = []) -> bool:
alembic_cfg.set_main_option('script_location', self.script_location)
alembic_cfg.set_main_option('sqlalchemy.url', self.db_url)
alembic_cfg.set_main_option('migration_id', self.id)
if self.version_table_schema:
alembic_cfg.set_main_option('version_table_schema', self.version_table_schema)

# Run the command
cmd_parser.run_cmd(alembic_cfg, options)
Expand Down Expand Up @@ -149,6 +153,7 @@ def __init__(self, plugins: List[AExtensionDesc] = []):
db_key = config.get('dbKey') or (p.dbKey if hasattr(p, 'dbKey') else None)
db_url = config.get('dbUrl') or (p.dbUrl if hasattr(p, 'dbUrl') else None)
script_location = config.get('scriptLocation') or (p.scriptLocation if hasattr(p, 'scriptLocation') else None)
version_table_schema = config.get('versionTableSchema') or (p.versionTableSchema if hasattr(p, 'versionTableSchema') else None)
auto_upgrade = config.get('autoUpgrade') if type(config.get('autoUpgrade')) == bool else \
(p.autoUpgrade if hasattr(p, 'autoUpgrade') and type(p.autoUpgrade) == bool else False)

Expand All @@ -175,7 +180,7 @@ def __init__(self, plugins: List[AExtensionDesc] = []):
db_url = str(engines.engine(db_key).url)

# Create new migration
migration = DBMigration(id, db_url, script_location, auto_upgrade)
migration = DBMigration(id, db_url, script_location, auto_upgrade=auto_upgrade, version_table_schema=version_table_schema)

# Store migration
self._migrations[migration.id] = migration
Expand Down
20 changes: 18 additions & 2 deletions tdp_core/dbmigration_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
if not migration_id:
raise ValueError('No migration_id in main configuration')

# Additional configuration to be passed to context.configure
additional_configuration = {}
# Add the version_table_schema parameter if it exists
version_table_schema = config.get_main_option('version_table_schema')
if version_table_schema:
additional_configuration['version_table_schema'] = version_table_schema


def run_migrations_offline():
"""Run migrations in 'offline' mode.
Expand All @@ -42,7 +49,12 @@ def run_migrations_offline():
url = config.get_main_option("sqlalchemy.url")
# include_schemas because https://stackoverflow.com/questions/26275041/alembic-sqlalchemy-does-not-detect-existing-tables
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True, include_schemas=True, version_table='{}_alembic_version'.format(migration_id)
url=url,
target_metadata=target_metadata,
literal_binds=True,
include_schemas=True,
version_table='{}_alembic_version'.format(migration_id),
**additional_configuration
)

with context.begin_transaction():
Expand All @@ -64,7 +76,11 @@ def run_migrations_online():

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata, include_schemas=True, version_table='{}_alembic_version'.format(migration_id)
connection=connection,
target_metadata=target_metadata,
include_schemas=True,
version_table='{}_alembic_version'.format(migration_id),
**additional_configuration
)

with context.begin_transaction():
Expand Down

0 comments on commit 4e2ee7a

Please sign in to comment.