--- /dev/null
+.. change::
+ :tags: bug, mssql
+ :tickets: 7168
+
+ Fixed issue with :meth:`.Inspector.has_table` where it would return False
+ if a local temp table with the same name from a different session happened
+ to be returned first when querying tempdb. This is a continuation of
+ :ticket:`6910` which accounted for the temp table existing only in the
+ alternate session and not the current one.
)
)
- table_name = connection.execute(s.limit(1)).scalar()
- if table_name:
- # #6910: verify it's not a temp table from another session
- obj_id = connection.execute(
- text("SELECT object_id(:table_name)"),
- {"table_name": "tempdb.dbo.[{}]".format(table_name)},
- ).scalar()
- return bool(obj_id)
+ # #7168: fetch all (not just first match) in case some other #temp
+ # table with the same name happens to appear first
+ table_names = connection.execute(s).scalars().fetchall()
+ # #6910: verify it's not a temp table from another session
+ for table_name in table_names:
+ if bool(
+ connection.scalar(
+ text("SELECT object_id(:table_name)"),
+ {"table_name": "tempdb.dbo.[{}]".format(table_name)},
+ )
+ ):
+ return True
else:
return False
else:
found_it = testing.db.dialect.has_table(connection, table_name)
eq_(found_it, exists)
+ def test_has_table_temp_not_present_but_another_session(self):
+ """test #6910"""
+
+ with testing.db.connect() as c1, testing.db.connect() as c2:
+
+ try:
+ with c1.begin():
+ c1.exec_driver_sql(
+ "create table #myveryveryuniquetemptablename (a int)"
+ )
+ assert not c2.dialect.has_table(
+ c2, "#myveryveryuniquetemptablename"
+ )
+ finally:
+ with c1.begin():
+ c1.exec_driver_sql(
+ "drop table #myveryveryuniquetemptablename"
+ )
+
+ def test_has_table_temp_temp_present_both_sessions(self):
+ """test #7168, continues from #6910"""
+
+ with testing.db.connect() as c1, testing.db.connect() as c2:
+ try:
+ with c1.begin():
+ c1.exec_driver_sql(
+ "create table #myveryveryuniquetemptablename (a int)"
+ )
+
+ with c2.begin():
+ c2.exec_driver_sql(
+ "create table #myveryveryuniquetemptablename (a int)"
+ )
+
+ assert c2.dialect.has_table(
+ c2, "#myveryveryuniquetemptablename"
+ )
+ finally:
+ with c1.begin():
+ c1.exec_driver_sql(
+ "drop table #myveryveryuniquetemptablename"
+ )
+ with c2.begin():
+ c2.exec_driver_sql(
+ "drop table #myveryveryuniquetemptablename"
+ )
+
def test_db_qualified_items(self, metadata, connection):
Table("foo", metadata, Column("id", Integer, primary_key=True))
Table(