From 8e159832596f15865f89675fa19202b171c99d7a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 29 Dec 2020 21:06:15 -0500 Subject: [PATCH] disambiguate SQL server temp table constraint names This seems to be raising errors lately which was not the case earlier, however SQL Server seems to produce name conflicts for named constraints against temp tables in different databases. I can raise the error every time here running two tests from ComponentReflectionTest with -n2. Unknown why the issue is happening now and didn't occur for several months. https://www.arbinada.com/en/node/1645 has some background. Change-Id: I8854dfd88503fb855a7e12622ebe97c08915e5bb --- lib/sqlalchemy/testing/config.py | 1 + lib/sqlalchemy/testing/provision.py | 5 +++++ .../testing/suite/test_reflection.py | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/sqlalchemy/testing/config.py b/lib/sqlalchemy/testing/config.py index 8c232f3198..0b8027b844 100644 --- a/lib/sqlalchemy/testing/config.py +++ b/lib/sqlalchemy/testing/config.py @@ -15,6 +15,7 @@ file_config = None test_schema = None test_schema_2 = None _current = None +ident = "main" _fixture_functions = None # installed by plugin_base diff --git a/lib/sqlalchemy/testing/provision.py b/lib/sqlalchemy/testing/provision.py index aa14a9c1ad..c4f489a69d 100644 --- a/lib/sqlalchemy/testing/provision.py +++ b/lib/sqlalchemy/testing/provision.py @@ -67,6 +67,11 @@ def setup_config(db_url, options, file_config, follower_ident): eng.connect().close() cfg = config.Config.register(eng, db_opts, options, file_config) + + # a symbolic name that tests can use if they need to disambiguate + # names across databases + config.ident = follower_ident + if follower_ident: configure_follower(cfg, follower_ident) return cfg diff --git a/lib/sqlalchemy/testing/suite/test_reflection.py b/lib/sqlalchemy/testing/suite/test_reflection.py index 3e4d7a2c0f..f1c5736623 100644 --- a/lib/sqlalchemy/testing/suite/test_reflection.py +++ b/lib/sqlalchemy/testing/suite/test_reflection.py @@ -458,7 +458,13 @@ class ComponentReflectionTest(fixtures.TablesTest): Column("id", sa.INT, primary_key=True), Column("name", sa.VARCHAR(50)), Column("foo", sa.INT), - sa.UniqueConstraint("name", name="user_tmp_uq"), + # disambiguate temp table unique constraint names. this is + # pretty arbitrary for a generic dialect however we are doing + # it to suit SQL Server which will produce name conflicts for + # unique constraints created against temp tables in different + # databases. + # https://www.arbinada.com/en/node/1645 + sa.UniqueConstraint("name", name="user_tmp_uq_%s" % config.ident), sa.Index("user_tmp_ix", "foo"), **kw ) @@ -1091,7 +1097,15 @@ class ComponentReflectionTest(fixtures.TablesTest): # Different dialects handle duplicate index and constraints # differently, so ignore this flag refl.pop("duplicates_index", None) - eq_(reflected, [{"column_names": ["name"], "name": "user_tmp_uq"}]) + eq_( + reflected, + [ + { + "column_names": ["name"], + "name": "user_tmp_uq_%s" % config.ident, + } + ], + ) @testing.requires.temp_table_reflect_indexes def test_get_temp_table_indexes(self): -- 2.47.3