]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
disambiguate SQL server temp table constraint names
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Dec 2020 02:06:15 +0000 (21:06 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Dec 2020 02:11:15 +0000 (21:11 -0500)
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
lib/sqlalchemy/testing/provision.py
lib/sqlalchemy/testing/suite/test_reflection.py

index 8c232f3198d5105d2613c65a2344232f3e37e485..0b8027b84404bd6da6f5ac10baa73b1161b453b0 100644 (file)
@@ -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
 
index aa14a9c1addea6e294fc5f4942e2d13fd62e754a..c4f489a69df07fa8269f744fe6f0b13f1e30e2b2 100644 (file)
@@ -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
index 3e4d7a2c0f099611f78f261ade5de53d8b4d9859..f1c5736623bc200db75e5f7d27dce9c07d0ea909 100644 (file)
@@ -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):