Skip to content

Commit 5be6408

Browse files
authored
Fix query, ensure that we only check tables in the right schema (#381)
If we don't use this `where` this query returns the tables from all the schemas and not just the schema we are looking for. Filter applied in the `left join` does not have the expected result in this scenario. Just run the query below and you see what I mean ```sql SELECT TABLE_NAME, i.TABLE_SCHEMA, TABLE_TYPE, CAST(ep.value AS VARCHAR) AS COMMENT FROM INFORMATION_SCHEMA.TABLES i LEFT JOIN sys.tables t ON t.name = i.TABLE_NAME LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id AND ((ep.name = 'MS_DESCRIPTION' AND ep.minor_id = 0) OR ep.value IS NULL) AND i.TABLE_SCHEMA = 'my_dev_schema' ``` If we have in the same database one schema the has the django_migrations but we are then running our application, that does not have migrations, against a different schema, this bit here breaks the application. The function has_table() will return True, but it should return False, because out schema does NOT have the table.
1 parent f684659 commit 5be6408

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

mssql/introspection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_table_list(self, cursor):
8686
LEFT JOIN sys.tables t ON t.name = i.TABLE_NAME
8787
LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id
8888
AND ((ep.name = 'MS_DESCRIPTION' AND ep.minor_id = 0) OR ep.value IS NULL)
89-
AND i.TABLE_SCHEMA = %s""" % (
89+
WHERE i.TABLE_SCHEMA = %s""" % (
9090
get_schema_name())
9191
else:
9292
sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % (get_schema_name())

0 commit comments

Comments
 (0)