def stamp_schema_version(): """Write the schema version stamp to the database, in case it is missing.""" with engine.begin() as connection: alembic_cfg = Config() alembic_cfg.set_main_option("script_location", "backend:alembic") path = Path(backend.__file__).parent / 'alembic.ini' alembic_cfg.config_file_name = str(path) alembic_cfg.attributes['connection'] = connection command.stamp(alembic_cfg, "head")
def run_migrations(migrations_dir: str, dsn: str) -> None: if "{DATABASE_HOST}" in dsn: print("DB is misconfigured. Not running migrations.") return print(f"Running migrations from {migrations_dir}") alembic_cfg = Config() alembic_cfg.config_file_name = f"{os.getcwd()}/alembic.ini" alembic_cfg.set_main_option('sqlalchemy.url', dsn) command.upgrade(alembic_cfg, 'head')
def makemigrations(commit_name): project_root = os.path.abspath(os.curdir) alembic_folder_dst = project_root + os.sep + "alembic" alembic_init_dst = project_root + os.sep + "alembic.ini" alembic_cfg = Config() alembic_cfg.config_file_name = alembic_init_dst if not (os.path.exists(alembic_folder_dst) and os.path.exists(alembic_init_dst)): alembic_init(directory=alembic_folder_dst, config=alembic_cfg) src = ICECREAM_PATH + "/migration_tool/env.py" copy(src, alembic_folder_dst) command.revision(alembic_cfg, commit_name, autogenerate=True)
def create_config(app): """Programmatically create Alembic config""" cfg = Config(stdout=StringIO()) cfg.get_template_directory = get_template_directory migrations = app.get("migrations_dir") or os.path.join( app["cwd"], "migrations") cfg.set_main_option("script_location", migrations) cfg.config_file_name = os.path.join(migrations, "alembic.ini") db = app["db"] cfg.set_section_option("default", "sqlalchemy.url", str(db.sync_engine.url)) # put database in main options cfg.set_main_option("databases", "default") # create empty logging section to avoid raising errors in env.py cfg.set_section_option("logging", "path", "") cfg.metadata = dict(default=db.metadata) return cfg
def get_alembic_conf(): alembic_cfg = Config() alembic_cfg.set_main_option("script_location", "migrations") alembic_cfg.set_main_option("sqlalchemy.url", DATABASE_URL) alembic_cfg.config_file_name = os.path.join("migrations", 'alembic.ini') if os.path.isdir('migrations') is False: click.echo(click.style("Initiating alembic...", fg='bright_blue')) alembic.init(alembic_cfg, 'migrations') with open('migrations/env.py', 'r+') as f: content = f.read() content = content.replace( 'target_metadata = None', f'from {MAIN_MODULE_NAME} import db\ntarget_metadata = db.metadata' ) f.seek(0) f.write(content) return alembic_cfg
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
def _create_config(self): """Programmatically create Alembic config. To determine databases, DATASTORE from project's config file is used. To customize Alembic use MIGRATIONS in you config file. Example:: MIGRATIONS = { 'alembic': { 'script_location': '<path>', 'databases': '<db_name1>,<db_name2>', }, '<db_name1>': { 'sqlalchemy.url': 'driver://*****:*****@localhost/dbname', }, '<bd_name2>': { 'sqlalchemy.url': 'driver://*****:*****@localhost/dbname', }, 'logging': { 'path': '<path_to_logging_config>', } } For more information about possible options, please visit Alembic documentation: https://alembic.readthedocs.org/en/latest/index.html """ app = self.app cfg = Config() cfg.get_template_directory = lux_template_directory migrations = os.path.join(app.meta.path, 'migrations') cfg.set_main_option('script_location', migrations) cfg.config_file_name = os.path.join(migrations, 'alembic.ini') odm = app.odm() databases = [] # set section for each found database for name, engine in odm.keys_engines(): if not name: name = 'default' databases.append(name) # url = str(engine.url).replace('+green', '') url = str(engine.url) cfg.set_section_option(name, 'sqlalchemy.url', url) # put databases in main options cfg.set_main_option("databases", ','.join(databases)) # create empty logging section to avoid raising errors in env.py cfg.set_section_option('logging', 'path', '') # obtain the metadata required for `auto` command metadata = {} for key, db_engine in odm.keys_engines(): if not key: key = 'default' metadata[key] = meta = MetaData() for table, engine in odm.binds.items(): if engine == db_engine: table.tometadata(meta) cfg.metadata = metadata config = app.config.get('MIGRATIONS') if config: for section in config.keys(): for key, value in config[section].items(): if section == 'alembic': cfg.set_main_option(key, value) else: cfg.set_section_option(section, key, value) return cfg