class BatchApplyTest(TestBase): __requires__ = ('sqlalchemy_08', ) def setUp(self): self.op = Operations(mock.Mock(opts={})) def _simple_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table('tname', m, Column('id', Integer, primary_key=True), Column('x', String(10)), Column('y', Integer)) return ApplyBatchImpl(t, table_args, table_kwargs) def _uq_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table('tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), UniqueConstraint('y', name='uq1')) return ApplyBatchImpl(t, table_args, table_kwargs) def _ix_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table('tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), Index('ix1', 'y')) return ApplyBatchImpl(t, table_args, table_kwargs) def _fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table('tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id'))) return ApplyBatchImpl(t, table_args, table_kwargs) def _named_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id', name='ufk'))) return ApplyBatchImpl(t, table_args, table_kwargs) def _selfref_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table('tname', m, Column('id', Integer, primary_key=True), Column('parent_id', Integer, ForeignKey('tname.id')), Column('data', String)) return ApplyBatchImpl(t, table_args, table_kwargs) def _assert_impl(self, impl, colnames=None, ddl_contains=None, ddl_not_contains=None, dialect='default'): context = op_fixture(dialect=dialect) impl._create(context.impl) if colnames is None: colnames = ['id', 'x', 'y'] eq_(impl.new_table.c.keys(), colnames) pk_cols = [col for col in impl.new_table.c if col.primary_key] eq_(list(impl.new_table.primary_key), pk_cols) create_stmt = str( CreateTable(impl.new_table).compile(dialect=context.dialect)) create_stmt = re.sub(r'[\n\t]', '', create_stmt) idx_stmt = "" for idx in impl.new_table.indexes: idx_stmt += str(CreateIndex(idx).compile(dialect=context.dialect)) idx_stmt = re.sub(r'[\n\t]', '', idx_stmt) if ddl_contains: assert ddl_contains in create_stmt + idx_stmt if ddl_not_contains: assert ddl_not_contains not in create_stmt + idx_stmt expected = [ create_stmt, ] if impl.new_table.indexes: expected.append(idx_stmt) expected.extend([ 'INSERT INTO _alembic_batch_temp (%(colnames)s) ' 'SELECT %(tname_colnames)s FROM tname' % { "colnames": ", ".join([ impl.new_table.c[name].name for name in colnames if name in impl.table.c ]), "tname_colnames": ", ".join("CAST(tname.%s AS %s) AS anon_1" % (name, impl.new_table.c[name].type) if (impl.new_table.c[name].type is not impl.table. c[name].type) else "tname.%s" % name for name in colnames if name in impl.table.c) }, 'DROP TABLE tname', 'ALTER TABLE _alembic_batch_temp RENAME TO tname' ]) context.assert_(*expected) return impl.new_table def test_change_type(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', type_=Integer) new_table = self._assert_impl(impl) assert new_table.c.x.type._type_affinity is Integer def test_rename_col(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', name='q') new_table = self._assert_impl(impl) eq_(new_table.c.x.name, 'q') def test_add_col(self): impl = self._simple_fixture() col = Column('g', Integer) # operations.add_column produces a table t = self.op._table('tname', col) # noqa impl.add_column('tname', col) new_table = self._assert_impl(impl, colnames=['id', 'x', 'y', 'g']) eq_(new_table.c.g.name, 'g') def test_rename_col_pk(self): impl = self._simple_fixture() impl.alter_column('tname', 'id', name='foobar') new_table = self._assert_impl(impl, ddl_contains="PRIMARY KEY (foobar)") eq_(new_table.c.id.name, 'foobar') eq_(list(new_table.primary_key), [new_table.c.id]) def test_rename_col_fk(self): impl = self._fk_fixture() impl.alter_column('tname', 'user_id', name='foobar') new_table = self._assert_impl( impl, colnames=['id', 'email', 'user_id'], ddl_contains='FOREIGN KEY(foobar) REFERENCES "user" (id)') eq_(new_table.c.user_id.name, 'foobar') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), "user.id") def test_drop_col(self): impl = self._simple_fixture() impl.drop_column('tname', column('x')) new_table = self._assert_impl(impl, colnames=['id', 'y']) assert 'y' in new_table.c assert 'x' not in new_table.c def test_drop_col_remove_pk(self): impl = self._simple_fixture() impl.drop_column('tname', column('id')) new_table = self._assert_impl(impl, colnames=['x', 'y'], ddl_not_contains="PRIMARY KEY") assert 'y' in new_table.c assert 'id' not in new_table.c assert not new_table.primary_key def test_drop_col_remove_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('user_id')) new_table = self._assert_impl(impl, colnames=['id', 'email'], ddl_not_contains="FOREIGN KEY") assert 'user_id' not in new_table.c assert not new_table.foreign_keys def test_drop_col_retain_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('email')) new_table = self._assert_impl( impl, colnames=['id', 'user_id'], ddl_contains='FOREIGN KEY(user_id) REFERENCES "user" (id)') assert 'email' not in new_table.c assert new_table.c.user_id.foreign_keys def test_drop_col_retain_fk_selfref(self): impl = self._selfref_fk_fixture() impl.drop_column('tname', column('data')) new_table = self._assert_impl(impl, colnames=['id', 'parent_id']) assert 'data' not in new_table.c assert new_table.c.parent_id.foreign_keys def test_add_fk(self): impl = self._simple_fixture() impl.add_column('tname', Column('user_id', Integer)) fk = self.op._foreign_key_constraint('fk1', 'tname', 'user', ['user_id'], ['id']) impl.add_constraint(fk) new_table = self._assert_impl( impl, colnames=['id', 'x', 'y', 'user_id'], ddl_contains='CONSTRAINT fk1 FOREIGN KEY(user_id) ' 'REFERENCES "user" (id)') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), 'user.id') def test_drop_fk(self): impl = self._named_fk_fixture() fk = ForeignKeyConstraint([], [], name='ufk') impl.drop_constraint(fk) new_table = self._assert_impl(impl, colnames=['id', 'email', 'user_id'], ddl_not_contains="CONSTRANT fk1") eq_(list(new_table.foreign_keys), []) def test_add_uq(self): impl = self._simple_fixture() uq = self.op._unique_constraint('uq1', 'tname', ['y']) impl.add_constraint(uq) self._assert_impl(impl, colnames=['id', 'x', 'y'], ddl_contains="CONSTRAINT uq1 UNIQUE") def test_drop_uq(self): impl = self._uq_fixture() uq = self.op._unique_constraint('uq1', 'tname', ['y']) impl.drop_constraint(uq) self._assert_impl(impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_add_index(self): impl = self._simple_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.add_index(ix) self._assert_impl(impl, colnames=['id', 'x', 'y'], ddl_contains="CREATE INDEX ix1") def test_drop_index(self): impl = self._ix_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.drop_index(ix) self._assert_impl(impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_add_table_opts(self): impl = self._simple_fixture(table_kwargs={'mysql_engine': 'InnoDB'}) self._assert_impl(impl, ddl_contains="ENGINE=InnoDB", dialect='mysql')
class BatchApplyTest(TestBase): __requires__ = ('sqlalchemy_08', ) def setUp(self): self.op = Operations(mock.Mock(opts={})) def _simple_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String(10)), Column('y', Integer) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _uq_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), UniqueConstraint('y', name='uq1') ) return ApplyBatchImpl(t, table_args, table_kwargs) def _ix_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), Index('ix1', 'y') ) return ApplyBatchImpl(t, table_args, table_kwargs) def _fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id')) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _named_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id', name='ufk')) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _selfref_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('parent_id', Integer, ForeignKey('tname.id')), Column('data', String) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _assert_impl(self, impl, colnames=None, ddl_contains=None, ddl_not_contains=None, dialect='default'): context = op_fixture(dialect=dialect) impl._create(context.impl) if colnames is None: colnames = ['id', 'x', 'y'] eq_(impl.new_table.c.keys(), colnames) pk_cols = [col for col in impl.new_table.c if col.primary_key] eq_(list(impl.new_table.primary_key), pk_cols) create_stmt = str( CreateTable(impl.new_table).compile(dialect=context.dialect)) create_stmt = re.sub(r'[\n\t]', '', create_stmt) idx_stmt = "" for idx in impl.new_table.indexes: idx_stmt += str(CreateIndex(idx).compile(dialect=context.dialect)) idx_stmt = re.sub(r'[\n\t]', '', idx_stmt) if ddl_contains: assert ddl_contains in create_stmt + idx_stmt if ddl_not_contains: assert ddl_not_contains not in create_stmt + idx_stmt expected = [ create_stmt, ] if impl.new_table.indexes: expected.append(idx_stmt) expected.extend([ 'INSERT INTO _alembic_batch_temp (%(colnames)s) ' 'SELECT %(tname_colnames)s FROM tname' % { "colnames": ", ".join([ impl.new_table.c[name].name for name in colnames if name in impl.table.c]), "tname_colnames": ", ".join( "CAST(tname.%s AS %s) AS anon_1" % ( name, impl.new_table.c[name].type) if ( impl.new_table.c[name].type is not impl.table.c[name].type) else "tname.%s" % name for name in colnames if name in impl.table.c ) }, 'DROP TABLE tname', 'ALTER TABLE _alembic_batch_temp RENAME TO tname' ]) context.assert_(*expected) return impl.new_table def test_change_type(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', type_=Integer) new_table = self._assert_impl(impl) assert new_table.c.x.type._type_affinity is Integer def test_rename_col(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', name='q') new_table = self._assert_impl(impl) eq_(new_table.c.x.name, 'q') def test_add_col(self): impl = self._simple_fixture() col = Column('g', Integer) # operations.add_column produces a table t = self.op._table('tname', col) # noqa impl.add_column('tname', col) new_table = self._assert_impl(impl, colnames=['id', 'x', 'y', 'g']) eq_(new_table.c.g.name, 'g') def test_rename_col_pk(self): impl = self._simple_fixture() impl.alter_column('tname', 'id', name='foobar') new_table = self._assert_impl( impl, ddl_contains="PRIMARY KEY (foobar)") eq_(new_table.c.id.name, 'foobar') eq_(list(new_table.primary_key), [new_table.c.id]) def test_rename_col_fk(self): impl = self._fk_fixture() impl.alter_column('tname', 'user_id', name='foobar') new_table = self._assert_impl( impl, colnames=['id', 'email', 'user_id'], ddl_contains='FOREIGN KEY(foobar) REFERENCES "user" (id)') eq_(new_table.c.user_id.name, 'foobar') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), "user.id" ) def test_drop_col(self): impl = self._simple_fixture() impl.drop_column('tname', column('x')) new_table = self._assert_impl(impl, colnames=['id', 'y']) assert 'y' in new_table.c assert 'x' not in new_table.c def test_drop_col_remove_pk(self): impl = self._simple_fixture() impl.drop_column('tname', column('id')) new_table = self._assert_impl( impl, colnames=['x', 'y'], ddl_not_contains="PRIMARY KEY") assert 'y' in new_table.c assert 'id' not in new_table.c assert not new_table.primary_key def test_drop_col_remove_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('user_id')) new_table = self._assert_impl( impl, colnames=['id', 'email'], ddl_not_contains="FOREIGN KEY") assert 'user_id' not in new_table.c assert not new_table.foreign_keys def test_drop_col_retain_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('email')) new_table = self._assert_impl( impl, colnames=['id', 'user_id'], ddl_contains='FOREIGN KEY(user_id) REFERENCES "user" (id)') assert 'email' not in new_table.c assert new_table.c.user_id.foreign_keys def test_drop_col_retain_fk_selfref(self): impl = self._selfref_fk_fixture() impl.drop_column('tname', column('data')) new_table = self._assert_impl(impl, colnames=['id', 'parent_id']) assert 'data' not in new_table.c assert new_table.c.parent_id.foreign_keys def test_add_fk(self): impl = self._simple_fixture() impl.add_column('tname', Column('user_id', Integer)) fk = self.op._foreign_key_constraint( 'fk1', 'tname', 'user', ['user_id'], ['id']) impl.add_constraint(fk) new_table = self._assert_impl( impl, colnames=['id', 'x', 'y', 'user_id'], ddl_contains='CONSTRAINT fk1 FOREIGN KEY(user_id) ' 'REFERENCES "user" (id)') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), 'user.id' ) def test_drop_fk(self): impl = self._named_fk_fixture() fk = ForeignKeyConstraint([], [], name='ufk') impl.drop_constraint(fk) new_table = self._assert_impl( impl, colnames=['id', 'email', 'user_id'], ddl_not_contains="CONSTRANT fk1") eq_( list(new_table.foreign_keys), [] ) def test_add_uq(self): impl = self._simple_fixture() uq = self.op._unique_constraint( 'uq1', 'tname', ['y'] ) impl.add_constraint(uq) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_contains="CONSTRAINT uq1 UNIQUE") def test_drop_uq(self): impl = self._uq_fixture() uq = self.op._unique_constraint( 'uq1', 'tname', ['y'] ) impl.drop_constraint(uq) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_add_index(self): impl = self._simple_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.add_index(ix) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_contains="CREATE INDEX ix1") def test_drop_index(self): impl = self._ix_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.drop_index(ix) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_add_table_opts(self): impl = self._simple_fixture(table_kwargs={'mysql_engine': 'InnoDB'}) self._assert_impl( impl, ddl_contains="ENGINE=InnoDB", dialect='mysql' )
class BatchApplyTest(TestBase): __requires__ = ('sqlalchemy_08', ) def setUp(self): self.op = Operations(mock.Mock(opts={})) def _simple_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String(10)), Column('y', Integer) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _uq_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), UniqueConstraint('y', name='uq1') ) return ApplyBatchImpl(t, table_args, table_kwargs) def _ix_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('x', String()), Column('y', Integer), Index('ix1', 'y') ) return ApplyBatchImpl(t, table_args, table_kwargs) def _literal_ck_fixture( self, copy_from=None, table_args=(), table_kwargs={}): m = MetaData() if copy_from is not None: t = copy_from else: t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), CheckConstraint("email LIKE '%@%'") ) return ApplyBatchImpl(t, table_args, table_kwargs) def _sql_ck_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()) ) t.append_constraint(CheckConstraint(t.c.email.like('%@%'))) return ApplyBatchImpl(t, table_args, table_kwargs) def _fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id')) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _multi_fk_fixture(self, table_args=(), table_kwargs={}, schema=None): m = MetaData() if schema: schemaarg = "%s." % schema else: schemaarg = "" t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id_1', Integer, ForeignKey('%suser.id' % schemaarg)), Column('user_id_2', Integer, ForeignKey('%suser.id' % schemaarg)), Column('user_id_3', Integer), Column('user_id_version', Integer), ForeignKeyConstraint( ['user_id_3', 'user_id_version'], ['%suser.id' % schemaarg, '%suser.id_version' % schemaarg]), schema=schema ) return ApplyBatchImpl(t, table_args, table_kwargs) def _named_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('email', String()), Column('user_id', Integer, ForeignKey('user.id', name='ufk')) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _selfref_fk_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('parent_id', Integer, ForeignKey('tname.id')), Column('data', String) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _boolean_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('flag', Boolean) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _boolean_no_ck_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('flag', Boolean(create_constraint=False)) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _enum_fixture(self, table_args=(), table_kwargs={}): m = MetaData() t = Table( 'tname', m, Column('id', Integer, primary_key=True), Column('thing', Enum('a', 'b', 'c')) ) return ApplyBatchImpl(t, table_args, table_kwargs) def _assert_impl(self, impl, colnames=None, ddl_contains=None, ddl_not_contains=None, dialect='default', schema=None): context = op_fixture(dialect=dialect) impl._create(context.impl) if colnames is None: colnames = ['id', 'x', 'y'] eq_(impl.new_table.c.keys(), colnames) pk_cols = [col for col in impl.new_table.c if col.primary_key] eq_(list(impl.new_table.primary_key), pk_cols) create_stmt = str( CreateTable(impl.new_table).compile(dialect=context.dialect)) create_stmt = re.sub(r'[\n\t]', '', create_stmt) idx_stmt = "" for idx in impl.new_table.indexes: idx_stmt += str(CreateIndex(idx).compile(dialect=context.dialect)) idx_stmt = re.sub(r'[\n\t]', '', idx_stmt) if ddl_contains: assert ddl_contains in create_stmt + idx_stmt if ddl_not_contains: assert ddl_not_contains not in create_stmt + idx_stmt expected = [ create_stmt, ] if impl.new_table.indexes: expected.append(idx_stmt) if schema: args = {"schema": "%s." % schema} else: args = {"schema": ""} args['colnames'] = ", ".join([ impl.new_table.c[name].name for name in colnames if name in impl.table.c]) args['tname_colnames'] = ", ".join( "CAST(%(schema)stname.%(name)s AS %(type)s) AS anon_1" % { 'schema': args['schema'], 'name': name, 'type': impl.new_table.c[name].type } if ( impl.new_table.c[name].type._type_affinity is not impl.table.c[name].type._type_affinity) else "%(schema)stname.%(name)s" % { 'schema': args['schema'], 'name': name} for name in colnames if name in impl.table.c ) expected.extend([ 'INSERT INTO %(schema)s_alembic_batch_temp (%(colnames)s) ' 'SELECT %(tname_colnames)s FROM %(schema)stname' % args, 'DROP TABLE %(schema)stname' % args, 'ALTER TABLE %(schema)s_alembic_batch_temp ' 'RENAME TO %(schema)stname' % args ]) context.assert_(*expected) return impl.new_table def test_change_type(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', type_=Integer) new_table = self._assert_impl(impl) assert new_table.c.x.type._type_affinity is Integer def test_rename_col(self): impl = self._simple_fixture() impl.alter_column('tname', 'x', name='q') new_table = self._assert_impl(impl) eq_(new_table.c.x.name, 'q') def test_rename_col_boolean(self): impl = self._boolean_fixture() impl.alter_column('tname', 'flag', name='bflag') new_table = self._assert_impl( impl, ddl_contains="CHECK (bflag IN (0, 1)", colnames=["id", "flag"]) eq_(new_table.c.flag.name, 'bflag') eq_( len([ const for const in new_table.constraints if isinstance(const, CheckConstraint)]), 1) def test_rename_col_boolean_no_ck(self): impl = self._boolean_no_ck_fixture() impl.alter_column('tname', 'flag', name='bflag') new_table = self._assert_impl( impl, ddl_not_contains="CHECK", colnames=["id", "flag"]) eq_(new_table.c.flag.name, 'bflag') eq_( len([ const for const in new_table.constraints if isinstance(const, CheckConstraint)]), 0) def test_rename_col_enum(self): impl = self._enum_fixture() impl.alter_column('tname', 'thing', name='thang') new_table = self._assert_impl( impl, ddl_contains="CHECK (thang IN ('a', 'b', 'c')", colnames=["id", "thing"]) eq_(new_table.c.thing.name, 'thang') eq_( len([ const for const in new_table.constraints if isinstance(const, CheckConstraint)]), 1) def test_rename_col_literal_ck(self): impl = self._literal_ck_fixture() impl.alter_column('tname', 'email', name='emol') new_table = self._assert_impl( # note this is wrong, we don't dig into the SQL impl, ddl_contains="CHECK (email LIKE '%@%')", colnames=["id", "email"]) eq_( len([c for c in new_table.constraints if isinstance(c, CheckConstraint)]), 1) eq_(new_table.c.email.name, 'emol') def test_rename_col_literal_ck_workaround(self): impl = self._literal_ck_fixture( copy_from=Table( 'tname', MetaData(), Column('id', Integer, primary_key=True), Column('email', String), ), table_args=[CheckConstraint("emol LIKE '%@%'")]) impl.alter_column('tname', 'email', name='emol') new_table = self._assert_impl( impl, ddl_contains="CHECK (emol LIKE '%@%')", colnames=["id", "email"]) eq_( len([c for c in new_table.constraints if isinstance(c, CheckConstraint)]), 1) eq_(new_table.c.email.name, 'emol') def test_rename_col_sql_ck(self): impl = self._sql_ck_fixture() impl.alter_column('tname', 'email', name='emol') new_table = self._assert_impl( impl, ddl_contains="CHECK (emol LIKE '%@%')", colnames=["id", "email"]) eq_( len([c for c in new_table.constraints if isinstance(c, CheckConstraint)]), 1) eq_(new_table.c.email.name, 'emol') def test_add_col(self): impl = self._simple_fixture() col = Column('g', Integer) # operations.add_column produces a table t = self.op._table('tname', col) # noqa impl.add_column('tname', col) new_table = self._assert_impl(impl, colnames=['id', 'x', 'y', 'g']) eq_(new_table.c.g.name, 'g') def test_rename_col_pk(self): impl = self._simple_fixture() impl.alter_column('tname', 'id', name='foobar') new_table = self._assert_impl( impl, ddl_contains="PRIMARY KEY (foobar)") eq_(new_table.c.id.name, 'foobar') eq_(list(new_table.primary_key), [new_table.c.id]) def test_rename_col_fk(self): impl = self._fk_fixture() impl.alter_column('tname', 'user_id', name='foobar') new_table = self._assert_impl( impl, colnames=['id', 'email', 'user_id'], ddl_contains='FOREIGN KEY(foobar) REFERENCES "user" (id)') eq_(new_table.c.user_id.name, 'foobar') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), "user.id" ) def test_regen_multi_fk(self): impl = self._multi_fk_fixture() self._assert_impl( impl, colnames=[ 'id', 'email', 'user_id_1', 'user_id_2', 'user_id_3', 'user_id_version'], ddl_contains='FOREIGN KEY(user_id_3, user_id_version) ' 'REFERENCES "user" (id, id_version)') # _get_colspec() in 0.8 calls upon fk.column when schema is # present. not sure if we want to try to fix this @config.requirements.fail_before_sqla_09 def test_regen_multi_fk_schema(self): impl = self._multi_fk_fixture(schema='foo_schema') self._assert_impl( impl, colnames=[ 'id', 'email', 'user_id_1', 'user_id_2', 'user_id_3', 'user_id_version'], ddl_contains='FOREIGN KEY(user_id_3, user_id_version) ' 'REFERENCES foo_schema."user" (id, id_version)', schema='foo_schema') def test_drop_col(self): impl = self._simple_fixture() impl.drop_column('tname', column('x')) new_table = self._assert_impl(impl, colnames=['id', 'y']) assert 'y' in new_table.c assert 'x' not in new_table.c def test_drop_col_remove_pk(self): impl = self._simple_fixture() impl.drop_column('tname', column('id')) new_table = self._assert_impl( impl, colnames=['x', 'y'], ddl_not_contains="PRIMARY KEY") assert 'y' in new_table.c assert 'id' not in new_table.c assert not new_table.primary_key def test_drop_col_remove_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('user_id')) new_table = self._assert_impl( impl, colnames=['id', 'email'], ddl_not_contains="FOREIGN KEY") assert 'user_id' not in new_table.c assert not new_table.foreign_keys def test_drop_col_retain_fk(self): impl = self._fk_fixture() impl.drop_column('tname', column('email')) new_table = self._assert_impl( impl, colnames=['id', 'user_id'], ddl_contains='FOREIGN KEY(user_id) REFERENCES "user" (id)') assert 'email' not in new_table.c assert new_table.c.user_id.foreign_keys def test_drop_col_retain_fk_selfref(self): impl = self._selfref_fk_fixture() impl.drop_column('tname', column('data')) new_table = self._assert_impl(impl, colnames=['id', 'parent_id']) assert 'data' not in new_table.c assert new_table.c.parent_id.foreign_keys def test_add_fk(self): impl = self._simple_fixture() impl.add_column('tname', Column('user_id', Integer)) fk = self.op._foreign_key_constraint( 'fk1', 'tname', 'user', ['user_id'], ['id']) impl.add_constraint(fk) new_table = self._assert_impl( impl, colnames=['id', 'x', 'y', 'user_id'], ddl_contains='CONSTRAINT fk1 FOREIGN KEY(user_id) ' 'REFERENCES "user" (id)') eq_( list(new_table.c.user_id.foreign_keys)[0]._get_colspec(), 'user.id' ) def test_drop_fk(self): impl = self._named_fk_fixture() fk = ForeignKeyConstraint([], [], name='ufk') impl.drop_constraint(fk) new_table = self._assert_impl( impl, colnames=['id', 'email', 'user_id'], ddl_not_contains="CONSTRANT fk1") eq_( list(new_table.foreign_keys), [] ) def test_add_uq(self): impl = self._simple_fixture() uq = self.op._unique_constraint( 'uq1', 'tname', ['y'] ) impl.add_constraint(uq) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_contains="CONSTRAINT uq1 UNIQUE") def test_drop_uq(self): impl = self._uq_fixture() uq = self.op._unique_constraint( 'uq1', 'tname', ['y'] ) impl.drop_constraint(uq) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_create_index(self): impl = self._simple_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.create_index(ix) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_contains="CREATE INDEX ix1") def test_drop_index(self): impl = self._ix_fixture() ix = self.op._index('ix1', 'tname', ['y']) impl.drop_index(ix) self._assert_impl( impl, colnames=['id', 'x', 'y'], ddl_not_contains="CONSTRAINT uq1 UNIQUE") def test_add_table_opts(self): impl = self._simple_fixture(table_kwargs={'mysql_engine': 'InnoDB'}) self._assert_impl( impl, ddl_contains="ENGINE=InnoDB", dialect='mysql' )