From 188dd8b2f098e804c8e64956e9c6490d41f1d7ce Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 10 May 2022 08:39:57 -0400 Subject: implement full copy for indexes in batch Fixed issue in batch mode where CREATE INDEX would not use a new column name in the case of a column rename. Indexes were previously being moved to the new table without any steps to rewrite columns. We now vendor the copy approach from table.to_metadata so that there's a new index expressed in terms of the new columns. Change-Id: Ied84232037aee0b2bf2094b3d3474013d7b41b34 Fixes: #1034 --- tests/test_batch.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test_batch.py b/tests/test_batch.py index f0bbd75..b19fa98 100644 --- a/tests/test_batch.py +++ b/tests/test_batch.py @@ -281,16 +281,20 @@ class BatchApplyTest(TestBase): create_stmt = re.sub(r"[\n\t]", "", create_stmt) idx_stmt = "" - for idx in impl.indexes.values(): - idx_stmt += str(CreateIndex(idx).compile(dialect=context.dialect)) - for idx in impl.new_indexes.values(): - impl.new_table.name = impl.table.name + + # create indexes; these should be created in terms of the + # final table name + impl.new_table.name = impl.table.name + + for idx in impl._gather_indexes_from_both_tables(): idx_stmt += str(CreateIndex(idx).compile(dialect=context.dialect)) - impl.new_table.name = ApplyBatchImpl._calc_temp_name( - impl.table.name - ) + idx_stmt = re.sub(r"[\n\t]", "", idx_stmt) + # revert new table name to the temp name, assertions below + # are looking for the temp name + impl.new_table.name = ApplyBatchImpl._calc_temp_name(impl.table.name) + if ddl_contains: assert ddl_contains in create_stmt + idx_stmt if ddl_not_contains: @@ -357,6 +361,20 @@ class BatchApplyTest(TestBase): new_table = self._assert_impl(impl) eq_(new_table.c.x.name, "q") + def test_rename_col_w_index(self): + impl = self._ix_fixture() + impl.alter_column("tname", "y", name="y2") + new_table = self._assert_impl( + impl, ddl_contains="CREATE INDEX ix1 ON tname (y2)" + ) + eq_(new_table.c.y.name, "y2") + + def test_rename_col_w_uq(self): + impl = self._uq_fixture() + impl.alter_column("tname", "y", name="y2") + new_table = self._assert_impl(impl, ddl_contains="UNIQUE (y2)") + eq_(new_table.c.y.name, "y2") + def test_alter_column_comment(self): impl = self._simple_fixture() impl.alter_column("tname", "x", comment="some comment") -- cgit v1.2.1