Skip to content

Conversation

3x0dv5
Copy link
Contributor

@3x0dv5 3x0dv5 commented Apr 16, 2024

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 queries below and you see what I mean

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'
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)
                    WHERE 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 our schema does NOT have the table.

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.
@3x0dv5 3x0dv5 changed the title Fix query, ensure that we only check tables in the right schema BUG fix, ensure that we only check tables in the right schema Apr 16, 2024
@3x0dv5
Copy link
Contributor Author

3x0dv5 commented Apr 26, 2024

@mShan0 Hi, could you guys take a look at this PR. We've faced this issue while upgrading to the newer version of mssql-django

@mShan0 mShan0 merged commit 5be6408 into microsoft:dev Apr 26, 2024
@mShan0
Copy link
Contributor

mShan0 commented Apr 26, 2024

Thanks for the PR. Will get this released soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants