def run_migrations_online(): """Run migrations in 'online' mode. In this scenario we need to create an Engine and associate a connection with the context. """ section = config.get_section(config.config_ini_section) section['sqlalchemy.url'] = expand_sql_url(section['sqlalchemy.url']) engine = engine_from_config( section, prefix='sqlalchemy.', poolclass=pool.NullPool ) connection = engine.connect() context.configure( connection=connection, target_metadata=target_metadata ) try: with context.begin_transaction(): context.run_migrations() finally: connection.close()
def __init__(self, configPath): super(Config, self).__init__(configPath) if not path.exists(configPath): raise RuntimeError(strings['no_config']) parser = configparser.ConfigParser() with open(configPath) as configFile: parser.readfp(configFile) isLoggerSection = lambda section: not (section.startswith( 'logger') or section.startswith('handler') or section.startswith( 'formatter')) sections = filter(isLoggerSection, parser.sections()) for section in sections: setattr(self, section, Section()) mySection = getattr(self, section) for option in parser.options(section): attr_name = option.replace('.', '_') value = parser.get(section, option) setattr(mySection, attr_name, value) if hasattr(self, 'alembic') and hasattr(self.alembic, 'script_location'): self.alembic.script_location = expand_env_var( 'SCRIPT_ROOT', self.alembic.script_location) self.set_main_option('script_location', self.alembic.script_location) if hasattr(self, 'alembic') and hasattr(self.alembic, 'sqlalchemy_url'): self.alembic.sqlalchemy_url = expand_sql_url( self.alembic.sqlalchemy_url) self.set_main_option('sqlalchemy.url', self.alembic.sqlalchemy_url)
def __init__(self, configPath): super(Config, self).__init__(configPath) if not path.exists(configPath): raise RuntimeError(strings['no_config']) parser = configparser.ConfigParser() with open(configPath) as configFile: parser.readfp(configFile) isLoggerSection = lambda section: not (section.startswith('logger') or section.startswith('handler') or section.startswith('formatter')) sections = filter(isLoggerSection, parser.sections()) for section in sections: setattr(self, section, Section()) mySection = getattr(self, section) for option in parser.options(section): attr_name = option.replace('.', '_') value = parser.get(section, option) setattr(mySection, attr_name, value) if hasattr(self, 'alembic') and hasattr(self.alembic, 'script_location'): self.alembic.script_location = expand_env_var('SCRIPT_ROOT', self.alembic.script_location) self.set_main_option('script_location', self.alembic.script_location) if hasattr(self, 'alembic') and hasattr(self.alembic, 'sqlalchemy_url'): self.alembic.sqlalchemy_url = expand_sql_url(self.alembic.sqlalchemy_url) self.set_main_option('sqlalchemy.url', self.alembic.sqlalchemy_url)
def session(self): if hasattr(self, "_session"): return self._session # This also has the side-effect of initializing the database and logging alembic.command.upgrade(self.config, "head") self.engine = sa.create_engine(expand_sql_url(self.config.alembic.sqlalchemy_url), encoding="utf-8", echo=False) SQLSession.configure(bind=self.engine) self._session = SQLSession() return self._session
def run_migrations_offline(): """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. """ url = expand_sql_url(config.get_main_option("sqlalchemy.url")) context.configure(url=url) with context.begin_transaction(): context.run_migrations()
def run_migrations_offline(): """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. """ url = expand_sql_url(config.get_main_option("sqlalchemy.url")) print(url) context.configure(url=url) with context.begin_transaction(): context.run_migrations()
def run_migrations_online(): """Run migrations in 'online' mode. In this scenario we need to create an Engine and associate a connection with the context. """ section = config.get_section(config.config_ini_section) section['sqlalchemy.url'] = expand_sql_url(section['sqlalchemy.url']) engine = engine_from_config(section, prefix='sqlalchemy.', poolclass=pool.NullPool) connection = engine.connect() context.configure(connection=connection, target_metadata=target_metadata) try: with context.begin_transaction(): context.run_migrations() finally: connection.close()