Bug
File: sqlx-mssql/src/migrate.rs, ensure_migrations_table
The IF NOT EXISTS check queries sys.tables.name with the full table_name value:
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = '{lit}')
CREATE TABLE {ident} (...)
sys.tables.name stores only the unqualified table name. If table_name is schema-qualified (e.g., dbo._sqlx_migrations), the literal 'dbo._sqlx_migrations' will never match '_sqlx_migrations' in sys.tables, causing the table to be re-created on every migration run (or fail with a duplicate).
This is a pre-existing issue that existed before the escaping fix in #4.
Suggested Fix
Parse the schema and table parts, then query with schema awareness:
IF NOT EXISTS (
SELECT * FROM sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.name = @table AND s.name = @schema
)
CREATE TABLE [schema].[table] (...)
Or use OBJECT_ID() which handles qualified names natively:
IF OBJECT_ID('{escaped_qualified_name}', 'U') IS NULL
CREATE TABLE {ident} (...)
Bug
File:
sqlx-mssql/src/migrate.rs,ensure_migrations_tableThe
IF NOT EXISTScheck queriessys.tables.namewith the fulltable_namevalue:sys.tables.namestores only the unqualified table name. Iftable_nameis schema-qualified (e.g.,dbo._sqlx_migrations), the literal'dbo._sqlx_migrations'will never match'_sqlx_migrations'insys.tables, causing the table to be re-created on every migration run (or fail with a duplicate).This is a pre-existing issue that existed before the escaping fix in #4.
Suggested Fix
Parse the schema and table parts, then query with schema awareness:
IF NOT EXISTS ( SELECT * FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = @table AND s.name = @schema ) CREATE TABLE [schema].[table] (...)Or use
OBJECT_ID()which handles qualified names natively: