def revision_database(message, autogenerate, revision_type, sql, head, splice, branch_label, version_path, rev_id): """Create new database revision.""" import types from alembic import command as alembic_command from alembic.config import Config as AlembicConfig alembic_cfg = AlembicConfig(config.ALEMBIC_INI_PATH) if revision_type: if revision_type == "core": path = config.ALEMBIC_CORE_REVISION_PATH elif revision_type == "tenant": path = config.ALEMBIC_TENANT_REVISION_PATH alembic_cfg.set_main_option("script_location", path) alembic_cfg.cmd_opts = types.SimpleNamespace(cmd="revision") alembic_command.revision( alembic_cfg, message, autogenerate=autogenerate, sql=sql, head=head, splice=splice, branch_label=branch_label, version_path=version_path, rev_id=rev_id, ) else: for path in [ config.ALEMBIC_CORE_REVISION_PATH, config.ALEMBIC_TENANT_REVISION_PATH, ]: alembic_cfg.set_main_option("script_location", path) alembic_cfg.cmd_opts = types.SimpleNamespace(cmd="revision") alembic_command.revision( alembic_cfg, message, autogenerate=autogenerate, sql=sql, head=head, splice=splice, branch_label=branch_label, version_path=version_path, rev_id=rev_id, )
def get_config(): path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "migrations") config = AlembicConfig(os.path.join(path, "alembic.ini")) config.set_main_option("script_location", path) config.cmd_opts = argparse.Namespace() setattr(config.cmd_opts, "x", None) return config
def execute_alembic(action, target): alembic_config = Config('alembic.ini') # main section options are set when main section is read main_section = alembic_config.config_ini_section alembic_config.get_section(main_section) alembic_config.cmd_opts = Namespace( x=["main_config=config_development.jsonc"]) if action == 'upgrade': command.upgrade(alembic_config, target) elif action == 'downgrade': command.downgrade(alembic_config, target)
def alembic_set_stamp_head(config_migration_path): # set the paths values this_file_directory = os.path.dirname(os.path.abspath(inspect.stack()[0][1])) alembic_directory = os.path.join(this_file_directory, 'alembic') ini_path = config_migration_path # create Alembic config and feed it with paths config = Config(ini_path) config.set_main_option('script_location', alembic_directory) config.cmd_opts = argparse.Namespace() # arguments stub config.attributes['configure_logger'] = False # prepare and run the command revision = 'head' # upgrade command command.upgrade(config, revision)
def setup_alembic_config(url): "setting up generic config for alembic migrations" directory = 'ether_sql/migrations' config = Config(os.path.join(directory, 'alembic.ini')) config.set_main_option('script_location', directory) config.cmd_opts = argparse.Namespace() # arguments stub x_arg = 'url=' + url if not hasattr(config.cmd_opts, 'x'): if x_arg is not None: setattr(config.cmd_opts, 'x', []) if isinstance(x_arg, list) or isinstance(x_arg, tuple): for x in x_arg: config.cmd_opts.x.append(x) else: config.cmd_opts.x.append(x_arg) else: setattr(config.cmd_opts, 'x', None) return config
def alembic_config(config_filename: str) -> Config: """Return altered Alembic config. First, read project's alembic configuration (alembic.ini). Second, alter project's existing config by passing Ahjo's credential configuration file as an 'x' argument and setting 'config_file_name' to point to Ahjo's logging configuration file. This way Alembic will use Ahjo's loggers and project's configurations when running Alembic operations. """ config = Config('alembic.ini') main_section = config.config_ini_section # main section options are set when main section is read config.get_section(main_section) config.cmd_opts = Namespace(x=["main_config=" + config_filename]) config.config_file_name = path.join(AHJO_PATH, 'resources/logger.ini') return config