def setup_class(cls): cls.env = env = staging_env() cls.a = env.generate_revision(util.rev_id(), '->a') cls.b = env.generate_revision(util.rev_id(), 'a->b') cls.c = env.generate_revision(util.rev_id(), 'b->c') cls.d = env.generate_revision(util.rev_id(), 'c->d') cls.e = env.generate_revision(util.rev_id(), 'd->e')
def process_revision_directives(context, rev, generate_revisions): generate_revisions[:] = [ ops.MigrationScript( util.rev_id(), ops.UpgradeOps(), ops.DowngradeOps(), version_path=os.path.join( _get_staging_directory(), "model1"), head="model1@head" ), ops.MigrationScript( util.rev_id(), ops.UpgradeOps(), ops.DowngradeOps(), version_path=os.path.join( _get_staging_directory(), "model2"), head="model2@head" ), ops.MigrationScript( util.rev_id(), ops.UpgradeOps(), ops.DowngradeOps(), version_path=os.path.join( _get_staging_directory(), "model3"), head="model3@head" ), ]
def setUp(self): self.bind = _sqlite_file_db() self.env = staging_env() self.cfg = _sqlite_testing_config() self.a = a = util.rev_id() self.b = b = util.rev_id() script = ScriptDirectory.from_config(self.cfg) script.generate_revision(a, None, refresh=True) write_script( script, a, """ revision = '%s' down_revision = None """ % a, ) script.generate_revision(b, None, refresh=True) write_script( script, b, """ revision = '%s' down_revision = '%s' """ % (b, a), )
def setup_class(cls): cls.env = env = staging_env() cls.a = env.generate_revision(util.rev_id(), "->a") cls.b = env.generate_revision(util.rev_id(), "a->b") cls.c = env.generate_revision(util.rev_id(), "b->c") cls.d = env.generate_revision(util.rev_id(), "c->d") cls.e = env.generate_revision(util.rev_id(), "d->e")
def process_revision_directives(context, rev, generate_revisions): existing_upgrades = generate_revisions[0].upgrade_ops existing_downgrades = generate_revisions[0].downgrade_ops # model1 will run the upgrades, e.g. create the table, # model2 will run the downgrades as upgrades, e.g. drop # the table again generate_revisions[:] = [ ops.MigrationScript( util.rev_id(), existing_upgrades, ops.DowngradeOps(), version_path=os.path.join( _get_staging_directory(), "model1"), head="model1@head" ), ops.MigrationScript( util.rev_id(), existing_downgrades, ops.DowngradeOps(), version_path=os.path.join( _get_staging_directory(), "model2"), head="model2@head" ) ]
def setUp(self): self.env = staging_env() self.cfg = _multi_dir_testing_config() self.cfg.set_main_option("revision_environment", "true") script = ScriptDirectory.from_config(self.cfg) # MARKMARK self.model1 = util.rev_id() self.model2 = util.rev_id() self.model3 = util.rev_id() for model, name in [ (self.model1, "model1"), (self.model2, "model2"), (self.model3, "model3"), ]: script.generate_revision( model, name, refresh=True, version_path=os.path.join(_get_staging_directory(), name), head="base") write_script(script, model, """\ "%s" revision = '%s' down_revision = None branch_labels = ['%s'] from alembic import op def upgrade(): pass def downgrade(): pass """ % (name, model, name))
def _opened_transaction_fixture(self): self.env = staging_env() self.cfg = _sqlite_testing_config() script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() b = util.rev_id() c = util.rev_id() script.generate_revision(a, "revision a", refresh=True) write_script(script, a, """ "rev a" revision = '%s' down_revision = None def upgrade(): pass def downgrade(): pass """ % (a, )) script.generate_revision(b, "revision b", refresh=True) write_script(script, b, """ "rev b" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): conn = op.get_bind() trans = conn.begin() def downgrade(): pass """ % (b, a)) script.generate_revision(c, "revision c", refresh=True) write_script(script, c, """ "rev c" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): pass def downgrade(): pass """ % (c, b)) return a, b, c
def setup(): global env env = staging_env() global a, b, c, d, e a = env.generate_revision(util.rev_id(), '->a', refresh=True) b = env.generate_revision(util.rev_id(), 'a->b', refresh=True) c = env.generate_revision(util.rev_id(), 'b->c', refresh=True) d = env.generate_revision(util.rev_id(), 'c->d', refresh=True) e = env.generate_revision(util.rev_id(), 'd->e', refresh=True)
def setup(): global env env = staging_env() global a, b, c, d, e a = env.generate_revision(util.rev_id(), None, refresh=True) b = env.generate_revision(util.rev_id(), None, refresh=True) c = env.generate_revision(util.rev_id(), None, refresh=True) d = env.generate_revision(util.rev_id(), None, refresh=True) e = env.generate_revision(util.rev_id(), None, refresh=True)
def _test_001_revisions(self): self.a = a = util.rev_id() self.b = b = util.rev_id() self.c = c = util.rev_id() script = ScriptDirectory.from_config(self.cfg) script.generate_revision(a, None, refresh=True) write_script(script, a, """ revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """ % a, sourceless=self.sourceless) script.generate_revision(b, None, refresh=True) write_script(script, b, """ revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE TABLE bar(id integer)") def downgrade(): op.execute("DROP TABLE bar") """ % (b, a), sourceless=self.sourceless) script.generate_revision(c, None, refresh=True) write_script(script, c, """ revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE TABLE bat(id integer)") def downgrade(): op.execute("DROP TABLE bat") """ % (c, b), sourceless=self.sourceless)
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision(util.rev_id(), '->a1') cls.b1 = env.generate_revision(util.rev_id(), 'a1->b1') cls.a2 = env.generate_revision( util.rev_id(), '->a2', head=(), refresh=True) cls.b2 = env.generate_revision( util.rev_id(), 'a2->b2', head=cls.a2.revision)
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision( util.rev_id(), "->a1", branch_labels=["lib1"] ) cls.b1 = env.generate_revision(util.rev_id(), "a1->b1") cls.c1 = env.generate_revision( util.rev_id(), "b1->c1", branch_labels=["c1lib"] ) cls.a2 = env.generate_revision(util.rev_id(), "->a2", head=()) cls.b2 = env.generate_revision( util.rev_id(), "a2->b2", head=cls.a2.revision ) cls.c2 = env.generate_revision( util.rev_id(), "b2->c2", head=cls.b2.revision, depends_on=["c1lib"] ) cls.d1 = env.generate_revision( util.rev_id(), "c1->d1", head=cls.c1.revision ) cls.e1 = env.generate_revision( util.rev_id(), "d1->e1", head=cls.d1.revision ) cls.f1 = env.generate_revision( util.rev_id(), "e1->f1", head=cls.e1.revision )
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision( util.rev_id(), '->a1', branch_labels=['lib1']) cls.b1 = env.generate_revision(util.rev_id(), 'a1->b1') cls.c1 = env.generate_revision( util.rev_id(), 'b1->c1', branch_labels=['c1lib']) cls.a2 = env.generate_revision(util.rev_id(), '->a2', head=()) cls.b2 = env.generate_revision( util.rev_id(), 'a2->b2', head=cls.a2.revision) cls.c2 = env.generate_revision( util.rev_id(), 'b2->c2', head=cls.b2.revision, depends_on=['c1lib']) cls.d1 = env.generate_revision( util.rev_id(), 'c1->d1', head=cls.c1.revision) cls.e1 = env.generate_revision( util.rev_id(), 'd1->e1', head=cls.d1.revision) cls.f1 = env.generate_revision( util.rev_id(), 'e1->f1', head=cls.e1.revision)
def three_rev_fixture(cfg): a = util.rev_id() b = util.rev_id() c = util.rev_id() script = ScriptDirectory.from_config(cfg) script.generate_revision(a, "revision a", refresh=True) write_script(script, a, """\ "Rev A" revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE STEP 1") def downgrade(): op.execute("DROP STEP 1") """ % a) script.generate_revision(b, "revision b", refresh=True) write_script(script, b, u("""# coding: utf-8 "Rev B, méil" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE STEP 2") def downgrade(): op.execute("DROP STEP 2") """) % (b, a), encoding="utf-8") script.generate_revision(c, "revision c", refresh=True) write_script(script, c, """\ "Rev C" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE STEP 3") def downgrade(): op.execute("DROP STEP 3") """ % (c, b)) return a, b, c
def setup_class(cls): cls.env = env = staging_env() cls.a = env.generate_revision(util.rev_id(), "->a") cls.b = env.generate_revision(util.rev_id(), "a->b") cls.c1 = env.generate_revision(util.rev_id(), "b->c1") cls.d1 = env.generate_revision(util.rev_id(), "c1->d1") cls.c2 = env.generate_revision( util.rev_id(), "b->c2", branch_labels="c2branch", head=cls.b.revision, splice=True, ) cls.d2 = env.generate_revision( util.rev_id(), "c2->d2", head=cls.c2.revision ) cls.e = env.generate_revision( util.rev_id(), "merge d1 and d2", head=(cls.d1.revision, cls.d2.revision), ) cls.f = env.generate_revision(util.rev_id(), "e->f")
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision(util.rev_id(), "->a1") cls.b1 = env.generate_revision(util.rev_id(), "a1->b1") cls.c1 = env.generate_revision(util.rev_id(), "b1->c1") cls.a2 = env.generate_revision( util.rev_id(), "->a2", head=(), refresh=True ) cls.b2 = env.generate_revision( util.rev_id(), "a2->b2", head=cls.a2.revision ) cls.c2 = env.generate_revision( util.rev_id(), "b2->c2", head=cls.b2.revision ) # mergepoint between c1, c2 # d1 dependent on c2 cls.d1 = env.generate_revision( util.rev_id(), "d1", head=(cls.c1.revision, cls.c2.revision), refresh=True, ) # but then c2 keeps going into d2 cls.d2 = env.generate_revision( util.rev_id(), "d2", head=cls.c2.revision, refresh=True, splice=True, )
def revision(config, message=None, autogenerate=False, sql=False): """Create a new revision file.""" script = ScriptDirectory.from_config(config) template_args = {} imports = set() environment = util.asbool( config.get_main_option("revision_environment") ) if autogenerate: environment = True util.requires_07("autogenerate") def retrieve_migrations(rev, context): if script.get_revision(rev) is not script.get_revision("head"): raise util.CommandError("Target database is not up to date.") autogen._produce_migration_diffs(context, template_args, imports) return [] elif environment: def retrieve_migrations(rev, context): return [] if environment: with EnvironmentContext( config, script, fn=retrieve_migrations, as_sql=sql, template_args=template_args, ): script.run_env() script.generate_revision(util.rev_id(), message, **template_args)
def test_error_on_new_with_missing_revision(self): self.cfg.set_main_option("file_template", "%%(slug)s_%%(rev)s") script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() script.generate_revision(a, "foobar", refresh=True) assert_raises_message( util.CommandError, "Could not determine revision id from filename foobar_%s.py. " "Be sure the 'revision' variable is declared " "inside the script." % a, write_script, script, a, """ down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """, )
def test_lookup_legacy(self): self.cfg.set_main_option("file_template", "%%(rev)s") script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() script.generate_revision(a, None, refresh=True) write_script( script, a, """ down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """, ) script = ScriptDirectory.from_config(self.cfg) rev = script._get_rev(a) eq_(rev.revision, a) eq_(os.path.basename(rev.path), "%s.py" % a)
def main(args): # pragma: no cover """local task to make changes from glottologcurator available to the production site via an alembic migration script. pulls the changelog from glottologcurator and creates a new alembic revision with it. """ kw = {} if args.http_user and args.http_password: kw['auth'] = (args.http_user, args.http_password) changes = requests.get(args.log_url, **kw).json() config = Config() config.set_main_option("script_location", args.migrations_dir) scriptdir = ScriptDirectory.from_config(config) script = scriptdir.generate_revision( rev_id(), "Glottolog Curator", refresh=True, upgrades="""\ # from glottologcurator conn = op.get_bind() for sql, params in [ %s ]: conn.execute(sql, params) """ % '\n'.join(u' ("""{0}""", {1}),'.format( event[0], parse_json_with_datetime(event[1])) for event in changes['events'])) args.log.info('new alembic migration script created:') args.log.info(script.path) args.log.info('run "alembic upgrade head" to merge changes')
def setUp(self): self.env = staging_env() self.cfg = cfg = _no_sql_testing_config() cfg.set_main_option("dialect_name", "sqlite") cfg.remove_main_option("url") self.a = util.rev_id() script = ScriptDirectory.from_config(cfg) script.generate_revision(self.a, "revision a", refresh=True) write_script( script, self.a, ( compat.u( """# coding: utf-8 from __future__ import unicode_literals revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("« S’il vous plaît…") def downgrade(): op.execute("drôle de petite voix m’a réveillé") """ ) % self.a ), encoding="utf-8", )
def alembic_revision(log_url): """local task to merge changes from glottologcurator available to the production site via an alembic migration script. pulls the changelog from glottologcurator and create a new alembic revision with it. """ user = raw_input('HTTP Basic auth user for glottologcurator: ') password = getpass('HTTP Basic auth password for glottologcurator: ') kw = {} if user and password: kw['auth'] = (user, password) changes = requests.get(log_url, **kw).json() config = Config() config.set_main_option("script_location", path('.').joinpath('migrations')) scriptdir = ScriptDirectory.from_config(config) script = scriptdir.generate_revision( rev_id(), "Glottolog Curator", refresh=True, upgrades="""\ # from glottologcurator conn = op.get_bind() for sql, params in [ %s ]: conn.execute(sql, params) """ % '\n'.join(u' ("""{0}""", {1}),'.format(*event) for event in changes['events'])) print('new alembic migration script created:') print(script.path)
def handle(self, *args, **options): db_id = options.get("metadata", None) metadata_module_name = get_module_from_db_name(db_id) use_metadata = options.get("use_metadata") revisions = options.get("revisions") message = options.get("message") branch_label = options.get("branch_label") rev_id = options.get("rev_id") try: metadata_cls = import_string(metadata_module_name) except ImportError: raise CommandError("ImportError: Invalid Module Name '%s' " % metadata_module_name) migrations_conf_dict = get_migrations_config(db_id) if migrations_conf_dict is None: raise CommandError("Migrations configuration not found.") config = get_config_from_dict(metadata_cls, use_metadata=use_metadata, **migrations_conf_dict) script = ScriptDirectory.from_config(config) template_args = { 'config': config } return script.generate_revision( rev_id or util.rev_id(), message, refresh=True, head=revisions, branch_labels=branch_label, **template_args)
def test_error_on_new_with_missing_revision(self): self.cfg.set_main_option("file_template", "%%(slug)s_%%(rev)s") script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() script.generate_revision(a, "foobar", refresh=True) path = script.get_revision(a).path with open(path, 'w') as fp: fp.write(""" down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """) pyc_path = util.pyc_file_from_path(path) if os.access(pyc_path, os.F_OK): os.unlink(pyc_path) assert_raises_message( util.CommandError, "Could not determine revision id from filename foobar_%s.py. " "Be sure the 'revision' variable is declared " "inside the script." % a, Script._from_path, script, path)
def test_all_traverse(self): writer = autogenerate.Rewriter() mocker = mock.Mock(side_effect=lambda context, revision, op: op) writer.rewrites(ops.MigrateOperation)(mocker) addcolop = ops.AddColumnOp( 't1', sa.Column('x', sa.Integer()) ) directives = [ ops.MigrationScript( util.rev_id(), ops.UpgradeOps(ops=[ ops.ModifyTableOps('t1', ops=[ addcolop ]) ]), ops.DowngradeOps(ops=[ ]), ) ] ctx, rev = mock.Mock(), mock.Mock() writer(ctx, rev, directives) eq_( mocker.mock_calls, [ mock.call(ctx, rev, directives[0]), mock.call(ctx, rev, directives[0].upgrade_ops), mock.call(ctx, rev, directives[0].upgrade_ops.ops[0]), mock.call(ctx, rev, addcolop), mock.call(ctx, rev, directives[0].downgrade_ops), ] )
def test_needs_flag(self): a = util.rev_id() script = ScriptDirectory.from_config(self.cfg) script.generate_revision(a, None, refresh=True) write_script(script, a, """ revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """ % a, sourceless=True) script = ScriptDirectory.from_config(self.cfg) eq_(script.get_heads(), []) self.cfg.set_main_option("sourceless", "true") script = ScriptDirectory.from_config(self.cfg) eq_(script.get_heads(), [a])
def test_option(self): self.cfg.set_main_option("file_template", "myfile_%%(slug)s") script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() script.generate_revision(a, "some message", refresh=True) write_script(script, a, """ revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """ % a) script = ScriptDirectory.from_config(self.cfg) rev = script.get_revision(a) eq_(rev.revision, a) eq_(os.path.basename(rev.path), "myfile_some_message.py")
def setUp(self): env = staging_env() self.cfg = cfg = _no_sql_testing_config() self.rid = rid = util.rev_id() self.script = script = ScriptDirectory.from_config(cfg) script.generate_revision(rid, None, refresh=True)
def test_008_long_name(self): rid = util.rev_id() script = env.generate_revision(rid, "this is a really long name with " "lots of characters and also " "I'd like it to\nhave\nnewlines") assert os.access( os.path.join(env.dir, 'versions', '%s_this_is_a_really_lon.py' % rid), os.F_OK)
def create_merge_revision(rev, context): context.script.generate_revision( revid=rev_id(), message='Revision merge', refresh=True, head=heads, # template-only arg: autonomie_version=autonomie_version(), ) return []
def setUp(self): self.env = staging_env() self.cfg = cfg = _no_sql_testing_config() cfg.set_main_option('dialect_name', 'sqlite') cfg.remove_main_option('url') self.a = util.rev_id() script = ScriptDirectory.from_config(cfg) script.generate_revision(self.a, "revision a", refresh=True) write_script(script, self.a, (compat.u("""# coding: utf-8 from __future__ import unicode_literals revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("« S’il vous plaît…") def downgrade(): op.execute("drôle de petite voix m’a réveillé") """) % self.a), encoding='utf-8')
def test_option(self): self.cfg.set_main_option("file_template", "myfile_%%(slug)s") script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() script.generate_revision(a, "some message", refresh=True) write_script( script, a, """ revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """ % a) script = ScriptDirectory.from_config(self.cfg) rev = script._get_rev(a) eq_(rev.revision, a) eq_(os.path.basename(rev.path), "myfile_some_message.py")
def setup_class(cls): cls.env = env = staging_env() cls.a = env.generate_revision(util.rev_id(), '->a') cls.b = env.generate_revision(util.rev_id(), 'a->b') cls.c1 = env.generate_revision(util.rev_id(), 'b->c1', branch_labels='c1branch', refresh=True) cls.d1 = env.generate_revision(util.rev_id(), 'c1->d1') cls.c2 = env.generate_revision(util.rev_id(), 'b->c2', branch_labels='c2branch', head=cls.b.revision, splice=True) cls.d2 = env.generate_revision(util.rev_id(), 'c2->d2', head=cls.c2.revision)
def setup_class(cls): cls.env = env = staging_env() cls.a = env.generate_revision(util.rev_id(), "->a") cls.b = env.generate_revision(util.rev_id(), "a->b") cls.c1 = env.generate_revision( util.rev_id(), "b->c1", branch_labels="c1branch", refresh=True ) cls.d1 = env.generate_revision(util.rev_id(), "c1->d1") cls.c2 = env.generate_revision( util.rev_id(), "b->c2", branch_labels="c2branch", head=cls.b.revision, splice=True, ) cls.d2 = env.generate_revision( util.rev_id(), "c2->d2", head=cls.c2.revision )
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision(util.rev_id(), '->a1', head='base') cls.a2 = env.generate_revision(util.rev_id(), '->a2', head='base') cls.a3 = env.generate_revision(util.rev_id(), '->a3', head='base') cls.amerge = env.generate_revision( util.rev_id(), 'amerge', head=[cls.a1.revision, cls.a2.revision, cls.a3.revision]) cls.b1 = env.generate_revision(util.rev_id(), '->b1', head='base') cls.b2 = env.generate_revision(util.rev_id(), '->b2', head='base') cls.bmerge = env.generate_revision( util.rev_id(), 'bmerge', head=[cls.b1.revision, cls.b2.revision]) cls.c1 = env.generate_revision(util.rev_id(), '->c1', head='base') cls.c2 = env.generate_revision(util.rev_id(), '->c2', head='base') cls.c3 = env.generate_revision(util.rev_id(), '->c3', head='base') cls.cmerge = env.generate_revision( util.rev_id(), 'cmerge', head=[cls.c1.revision, cls.c2.revision, cls.c3.revision]) cls.d1 = env.generate_revision( util.rev_id(), 'overmerge', head="base", depends_on=[cls.a3.revision, cls.b2.revision, cls.c1.revision])
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision(util.rev_id(), '->a1') cls.b1 = env.generate_revision(util.rev_id(), 'a1->b1') cls.c1 = env.generate_revision(util.rev_id(), 'b1->c1') cls.a2 = env.generate_revision(util.rev_id(), '->a2', head=(), refresh=True) cls.b2 = env.generate_revision(util.rev_id(), 'a2->b2', head=cls.a2.revision) cls.c2 = env.generate_revision(util.rev_id(), 'b2->c2', head=cls.b2.revision) cls.a3 = env.generate_revision(util.rev_id(), '->a3', head=(), refresh=True) cls.b3 = env.generate_revision(util.rev_id(), 'a3->b3', head=cls.a3.revision) cls.c3 = env.generate_revision(util.rev_id(), 'b3->c3', head=cls.b3.revision) # mergepoint between c1, c2, c3 # d1 dependent on c2, c3 cls.d1 = env.generate_revision(util.rev_id(), 'd1', head=(cls.c1.revision, cls.c2.revision, cls.c3.revision), refresh=True) # but then c2 keeps going into d2 cls.d2 = env.generate_revision(util.rev_id(), 'd2', head=cls.c2.revision, refresh=True, splice=True) # c3 keeps going into d3 cls.d3 = env.generate_revision(util.rev_id(), 'd3', head=cls.c3.revision, refresh=True, splice=True)
def test_chained_ops(self): writer1 = autogenerate.Rewriter() writer2 = autogenerate.Rewriter() @writer1.rewrites(ops.AddColumnOp) def add_column_nullable(context, revision, op): if op.column.nullable: return op else: op.column.nullable = True return [ op, ops.AlterColumnOp( op.table_name, op.column.name, modify_nullable=False, existing_type=op.column.type, ), ] @writer2.rewrites(ops.AddColumnOp) def add_column_idx(context, revision, op): idx_op = ops.CreateIndexOp("ixt", op.table_name, [op.column.name]) return [op, idx_op] directives = [ ops.MigrationScript( util.rev_id(), ops.UpgradeOps( ops=[ ops.ModifyTableOps( "t1", ops=[ ops.AddColumnOp( "t1", sa.Column( "x", sa.Integer(), nullable=False ), ) ], ) ] ), ops.DowngradeOps(ops=[]), ) ] ctx, rev = mock.Mock(), mock.Mock() writer1.chain(writer2)(ctx, rev, directives) eq_( autogenerate.render_python_code(directives[0].upgrade_ops), "# ### commands auto generated by Alembic - please adjust! ###\n" " op.add_column('t1', " "sa.Column('x', sa.Integer(), nullable=True))\n" " op.create_index('ixt', 't1', ['x'], unique=False)\n" " op.alter_column('t1', 'x',\n" " existing_type=sa.Integer(),\n" " nullable=False)\n" " # ### end Alembic commands ###", )
def test_007_no_refresh(self): rid = util.rev_id() script = env.generate_revision(rid, "dont' refresh") is_(script, None) env2 = staging_env(create=False) eq_(env2._as_rev_number("head"), rid)
def _opened_transaction_fixture(self, future=False): self.env = staging_env() if future: self.cfg = _sqlite_testing_config(future=future) else: self.cfg = _sqlite_testing_config() if self.branched_connection: self._branched_connection_env() script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() b = util.rev_id() c = util.rev_id() script.generate_revision(a, "revision a", refresh=True) write_script( script, a, """ "rev a" revision = '%s' down_revision = None def upgrade(): pass def downgrade(): pass """ % (a, ), ) script.generate_revision(b, "revision b", refresh=True) write_script( script, b, """ "rev b" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): conn = op.get_bind() # this should fail for a SQLAlchemy 2.0 connection b.c. there is # already a transaction. trans = conn.begin() def downgrade(): pass """ % (b, a), ) script.generate_revision(c, "revision c", refresh=True) write_script( script, c, """ "rev c" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): pass def downgrade(): pass """ % (c, b), ) return a, b, c
def test_multiple_passes_with_mutations(self): writer1 = autogenerate.Rewriter() @writer1.rewrites(ops.CreateTableOp) def rewrite_alter_column(context, revision, op): op.table_name += "_pass" return op directives = [ ops.MigrationScript( util.rev_id(), ops.UpgradeOps(ops=[ ops.CreateTableOp( "test_table", [sa.Column("id", sa.Integer(), primary_key=True)], ) ]), ops.DowngradeOps(ops=[]), ) ] ctx, rev = mock.Mock(), mock.Mock() writer1(ctx, rev, directives) directives[0].upgrade_ops_list.extend([ ops.UpgradeOps(ops=[ ops.CreateTableOp( "another_test_table", [sa.Column("id", sa.Integer(), primary_key=True)], ) ]), ops.UpgradeOps(ops=[ ops.CreateTableOp( "third_test_table", [sa.Column("id", sa.Integer(), primary_key=True)], ) ]), ]) writer1(ctx, rev, directives) eq_( autogenerate.render_python_code(directives[0].upgrade_ops_list[0]), "# ### commands auto generated by Alembic - please adjust! ###\n" " op.create_table('test_table_pass',\n" " sa.Column('id', sa.Integer(), nullable=False),\n" " sa.PrimaryKeyConstraint('id')\n" " )\n" " # ### end Alembic commands ###", ) eq_( autogenerate.render_python_code(directives[0].upgrade_ops_list[1]), "# ### commands auto generated by Alembic - please adjust! ###\n" " op.create_table('another_test_table_pass',\n" " sa.Column('id', sa.Integer(), nullable=False),\n" " sa.PrimaryKeyConstraint('id')\n" " )\n" " # ### end Alembic commands ###", ) eq_( autogenerate.render_python_code(directives[0].upgrade_ops_list[2]), "# ### commands auto generated by Alembic - please adjust! ###\n" " op.create_table('third_test_table_pass',\n" " sa.Column('id', sa.Integer(), nullable=False),\n" " sa.PrimaryKeyConstraint('id')\n" " )\n" " # ### end Alembic commands ###", )
def setup_class(cls): cls.env = env = staging_env() cls.a1 = env.generate_revision(util.rev_id(), "->a1") cls.b1 = env.generate_revision(util.rev_id(), "a1->b1") cls.c1 = env.generate_revision(util.rev_id(), "b1->c1") cls.a2 = env.generate_revision(util.rev_id(), "->a2", head=(), refresh=True) cls.b2 = env.generate_revision(util.rev_id(), "a2->b2", head=cls.a2.revision) cls.c2 = env.generate_revision(util.rev_id(), "b2->c2", head=cls.b2.revision) cls.a3 = env.generate_revision(util.rev_id(), "->a3", head=(), refresh=True) cls.b3 = env.generate_revision(util.rev_id(), "a3->b3", head=cls.a3.revision) cls.c3 = env.generate_revision(util.rev_id(), "b3->c3", head=cls.b3.revision) # mergepoint between c1, c2, c3 # d1 dependent on c2, c3 cls.d1 = env.generate_revision( util.rev_id(), "d1", head=(cls.c1.revision, cls.c2.revision, cls.c3.revision), refresh=True, ) # but then c2 keeps going into d2 cls.d2 = env.generate_revision( util.rev_id(), "d2", head=cls.c2.revision, refresh=True, splice=True, ) # c3 keeps going into d3 cls.d3 = env.generate_revision( util.rev_id(), "d3", head=cls.c3.revision, refresh=True, splice=True, )
def gen(rev, head=None, **kw): if head: kw['head'] = [env._revs[h].revision for h in head.split()] revid = '__'.join((rev, util.rev_id())) env._revs[rev] = env.generate_revision(revid, rev, splice=True, **kw)
def _opened_transaction_fixture(self): self.env = staging_env() self.cfg = _sqlite_testing_config() script = ScriptDirectory.from_config(self.cfg) a = util.rev_id() b = util.rev_id() c = util.rev_id() script.generate_revision(a, "revision a", refresh=True) write_script( script, a, """ "rev a" revision = '%s' down_revision = None def upgrade(): pass def downgrade(): pass """ % (a, )) script.generate_revision(b, "revision b", refresh=True) write_script( script, b, """ "rev b" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): conn = op.get_bind() trans = conn.begin() def downgrade(): pass """ % (b, a)) script.generate_revision(c, "revision c", refresh=True) write_script( script, c, """ "rev c" revision = '%s' down_revision = '%s' from alembic import op def upgrade(): pass def downgrade(): pass """ % (c, b)) return a, b, c
def _test_002_rev_ids(self): global abc, def_ abc = util.rev_id() def_ = util.rev_id() ne_(abc, def_)
def _test_001_revisions(self): self.a = a = util.rev_id() self.b = b = util.rev_id() self.c = c = util.rev_id() script = ScriptDirectory.from_config(self.cfg) script.generate_revision(a, None, refresh=True) write_script( script, a, """ revision = '%s' down_revision = None from alembic import op def upgrade(): op.execute("CREATE TABLE foo(id integer)") def downgrade(): op.execute("DROP TABLE foo") """ % a, sourceless=self.sourceless, ) script.generate_revision(b, None, refresh=True) write_script( script, b, """ revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE TABLE bar(id integer)") def downgrade(): op.execute("DROP TABLE bar") """ % (b, a), sourceless=self.sourceless, ) script.generate_revision(c, None, refresh=True) write_script( script, c, """ revision = '%s' down_revision = '%s' from alembic import op def upgrade(): op.execute("CREATE TABLE bat(id integer)") def downgrade(): op.execute("DROP TABLE bat") """ % (c, b), sourceless=self.sourceless, )