import logging import click from service.conf import settings from service.db import SchemaPath from service.tasks.data_model import get_all_schemas_of_path from service.utils import dbmate logger = logging.getLogger(__name__) @click.group("db") def cli(): """ Perform database commands. """ @cli.command("migrate") @click.option( "-s", "--schema-path", required=True, default=SchemaPath.PUBLIC, type=click.Choice(choices=SchemaPath.choices()), ) def migrate(schema_path: str): """ Apply schema type migrations. """ disable_ssl = settings.MIGRATIONS_DISABLE_SSL if schema_path in {SchemaPath.PUBLIC, SchemaPath.COMMONS}: dbmate.migrate(schema_path, dump_schema=True, disable_ssl=disable_ssl) elif schema_path == SchemaPath.WORKSPACE: # Running migrations for "workspace" schema (always empty) and dumping it dbmate.migrate(schema_path, dump_schema=True, disable_ssl=disable_ssl) # Running migrations for all user workspace schemas (no need to dump them) workspace_schemas = get_all_schemas_of_path(schema_path) for workspace_schema in workspace_schemas: dbmate.migrate( workspace_schema, schema_path=SchemaPath.WORKSPACE, dump_schema=False, disable_ssl=disable_ssl, ) elif schema_path == SchemaPath.COMPANY: # company contains all tables from workspace + a few company-specific ones # so we run all workspace migrations for company schemas as well dbmate.migrate( schema_name=schema_path, schema_path=SchemaPath.WORKSPACE, disable_ssl=disable_ssl, dump_schema=False, ) dbmate.migrate( schema_name=schema_path, schema_path=SchemaPath.COMPANY, disable_ssl=disable_ssl, dump_schema=True, ) company_schemas = get_all_schemas_of_path(schema_path) for company_schema in company_schemas: dbmate.migrate( company_schema, schema_path=SchemaPath.WORKSPACE, dump_schema=False, disable_ssl=disable_ssl, ) dbmate.migrate( company_schema, schema_path=SchemaPath.COMPANY, dump_schema=False, disable_ssl=disable_ssl, ) elif schema_path == SchemaPath.ALLIANCE: # alliance schema contains same tables as workspace schema # (but may include additional tables in future) # so we run all workspace migrations for alliance schemas as well # before running alliance-specific ones from 'alliance' folder dbmate.migrate( schema_name=schema_path, schema_path=SchemaPath.WORKSPACE, disable_ssl=disable_ssl, dump_schema=False, ) dbmate.migrate( schema_name=schema_path, schema_path=SchemaPath.ALLIANCE, disable_ssl=disable_ssl, dump_schema=True, ) alliance_schemas = get_all_schemas_of_path(schema_path) for alliance_schema in alliance_schemas: dbmate.migrate( alliance_schema, schema_path=SchemaPath.WORKSPACE, dump_schema=False, disable_ssl=disable_ssl, ) dbmate.migrate( alliance_schema, schema_path=SchemaPath.ALLIANCE, dump_schema=False, disable_ssl=disable_ssl, ) @cli.command("migrate-all") @click.pass_context def migrate_all(ctx): for schema_path in SchemaPath.choices(): ctx.invoke(migrate, schema_path=schema_path) @cli.command("new") @click.option( "-m", "--message", required=True, ) @click.option( "-s", "--schema-path", required=True, default=SchemaPath.PUBLIC, type=click.Choice(choices=SchemaPath.choices()), ) def new(message: str, schema_path: str): """ Apply schema type migrations. """ dbmate.new(message, schema_name=schema_path) @cli.command("wait") def wait(): dbmate.wait(disable_ssl=settings.MIGRATIONS_DISABLE_SSL) click.echo("Connected!")