def upgrade(pkg, sql=False): """Upgrade to a later version.""" from alembic.config import Config from alembic.environment import EnvironmentContext if ':' in pkg: pkg, rev = pkg.split(':',1) else: rev = 'head' script = ScriptDirectory(pkg) env = EnvironmentContext(Config(''), script) conn = ptah.get_base().metadata.bind.connect() def upgrade(revision, context): return script._upgrade_revs(rev, revision) env.configure( connection = conn, fn = upgrade, as_sql = sql, starting_rev = None, destination_rev = rev, ) mc = env._migration_context env._migration_context = MigrationContext(pkg, conn.dialect, conn, mc.opts) with env: try: with env.begin_transaction(): env.run_migrations() finally: conn.close()
def test_args_propagate(self): config = _no_sql_testing_config() script = ScriptDirectory.from_config(config) template_args = {"x": "x1", "y": "y1", "z": "z1"} env = EnvironmentContext(config, script, template_args=template_args) env.configure(dialect_name="sqlite", template_args={"y": "y2", "q": "q1"}) eq_(template_args, {"x": "x1", "y": "y2", "z": "z1", "q": "q1"})
def create_migration_ctx(**kwargs): """Create an alembic migration context.""" env = EnvironmentContext(Config(), None) env.configure(connection=db.engine.connect(), sqlalchemy_module_prefix='db.', **kwargs) return env.get_context()
def upgrade(pkg, sql=False): """Upgrade to a later version.""" from alembic.config import Config from alembic.environment import EnvironmentContext if ':' in pkg: pkg, rev = pkg.split(':', 1) else: rev = 'head' script = ScriptDirectory(pkg) env = EnvironmentContext(Config(''), script) conn = ptah.get_base().metadata.bind.connect() def upgrade(revision, context): return script._upgrade_revs(rev, revision) env.configure( connection=conn, fn=upgrade, as_sql=sql, starting_rev=None, destination_rev=rev, ) mc = env._migration_context env._migration_context = MigrationContext(pkg, conn.dialect, conn, mc.opts) with env: try: with env.begin_transaction(): env.run_migrations() finally: conn.close()
def test_args_propagate(self): config = _no_sql_testing_config() script = ScriptDirectory.from_config(config) template_args = {"x": "x1", "y": "y1", "z": "z1"} env = EnvironmentContext(config, script, template_args=template_args) env.configure( dialect_name="sqlite", template_args={"y": "y2", "q": "q1"} ) eq_(template_args, {"x": "x1", "y": "y2", "z": "z1", "q": "q1"})
def create_migration_ctx(**kwargs): """Create an alembic migration context.""" env = EnvironmentContext(Config(), None) env.configure( connection=db.engine.connect(), sqlalchemy_module_prefix='db.', **kwargs ) return env.get_context()
def test_sys_path_prepend(self, config_value, expected): self.cfg.set_main_option("prepend_sys_path", config_value) script = ScriptDirectory.from_config(self.cfg) env = EnvironmentContext(self.cfg, script) target = os.path.abspath(_get_staging_directory()) def assert_(heads, context): eq_( [os.path.abspath(p) for p in sys.path[0:len(expected)]], [os.path.abspath(p) for p in expected], ) return [] p = [p for p in sys.path if os.path.abspath(p) != target] with mock.patch.object(sys, "path", p): env.configure(url="sqlite://", fn=assert_) with env: script.run_env()
def current(pkg): """Display the current revision.""" def display_version(rev, context): rev = script._get_rev(rev) if rev is None: print("Package '{0}' rev: None".format(pkg)) else: print("Package '{0}' rev: {1}{2} {3}".format( pkg, rev.revision, '(head)' if rev.is_head else "", rev.doc)) return [] conn = ptah.get_base().metadata.bind.connect() script = ptah.migrate.ScriptDirectory(pkg) env = EnvironmentContext(Config(''), script) env.configure(connection=conn, fn=display_version) mc = env._migration_context env._migration_context = MigrationContext(pkg, conn.dialect, conn, mc.opts) with env.begin_transaction(): env.run_migrations()
def current(pkg): """Display the current revision.""" def display_version(rev, context): rev = script._get_rev(rev) if rev is None: print("Package '{0}' rev: None".format(pkg)) else: print("Package '{0}' rev: {1}{2} {3}".format(pkg, rev.revision, "(head)" if rev.is_head else "", rev.doc)) return [] conn = ptah.get_base().metadata.bind.connect() script = ptah.migrate.ScriptDirectory(pkg) env = EnvironmentContext(Config(""), script) env.configure(connection=conn, fn=display_version) mc = env._migration_context env._migration_context = MigrationContext(pkg, conn.dialect, conn, mc.opts) with env.begin_transaction(): env.run_migrations()
op_name = op_[0] if op_name.endswith('_table'): op_obj = op_[1] op_obj_name = op_obj.name elif op_name.endswith('_column'): op_obj = op_[3] op_obj_name = op_[2] + '.' + op_obj.name else: op_obj = op_[1] op_obj_name = op_obj.name if op_obj_name is None: op_obj_name = op_obj.table.name + '.' \ + '_'.join(c.name for c in op_obj.columns) else: op_name = op_[0][0] op_obj_name = op_[0][2] + '.' + op_[0][3] return not op_obj_name in IGNORE_OPS.get(op_name, []) # Set up a migration context. alembic_cfg.set_main_option('script_location', 'thelma:db/schema/migrations') script = ScriptDirectory.from_config(alembic_cfg) env_ctxt = EnvironmentContext(alembic_cfg, script) engine = create_engine(alembic_cfg) env_ctxt.configure(engine.connect()) # , include_object=include_object) mig_ctxt = env_ctxt.get_context() ops = compare_metadata(mig_ctxt, metadata) diff = [op for op in ops if include_op(op)] pprint.pprint(diff, indent=2, width=20)
if isinstance(op_, tuple): op_name = op_[0] if op_name.endswith('_table'): op_obj = op_[1] op_obj_name = op_obj.name elif op_name.endswith('_column'): op_obj = op_[3] op_obj_name = op_[2] + '.' + op_obj.name else: op_obj = op_[1] op_obj_name = op_obj.name if op_obj_name is None: op_obj_name = op_obj.table.name + '.' \ + '_'.join(c.name for c in op_obj.columns) else: op_name = op_[0][0] op_obj_name = op_[0][2] + '.' + op_[0][3] return not op_obj_name in IGNORE_OPS.get(op_name, []) # Set up a migration context. alembic_cfg.set_main_option('script_location', 'thelma:db/schema/migrations') script = ScriptDirectory.from_config(alembic_cfg) env_ctxt = EnvironmentContext(alembic_cfg, script) engine = create_engine(alembic_cfg) env_ctxt.configure(engine.connect()) # , include_object=include_object) mig_ctxt = env_ctxt.get_context() ops = compare_metadata(mig_ctxt, metadata) diff = [op for op in ops if include_op(op)] pprint.pprint(diff, indent=2, width=20)