Skip to content

Commit

Permalink
Fix query, ensure that we only check tables in the right schema
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
3x0dv5 authored Apr 16, 2024
1 parent 99e83fd commit adbc0e0
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion mssql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_table_list(self, cursor):
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 = %s""" % (
WHERE i.TABLE_SCHEMA = %s""" % (
get_schema_name())
else:
sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % (get_schema_name())
Expand Down

0 comments on commit adbc0e0

Please sign in to comment.