Skip to content

Commit 7c44654

Browse files
authored
Bug #440: Replaced "ScriptRun" with [ScriptsRun] (and others) for SQL server to avoid issues with QUOTED_IDENTIFIER OFF (#500)
Replaced "ScriptsRun" with [ScriptsRun] (and others) for SQL Server to avoid issues with QUOTED_IDENTIFIER OFF
1 parent 71bb1a5 commit 7c44654

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/grate.sqlserver/Infrastructure/SqlServerSyntax.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ IF EXISTS(SELECT * FROM sysdatabases WHERE [name] = '{databaseName}')
3030
ALTER DATABASE [{databaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
3131
DROP DATABASE [{databaseName}]
3232
END";
33-
public string TableWithSchema(string schemaName, string tableName) => $"{schemaName}.\"{tableName}\"";
33+
public string TableWithSchema(string schemaName, string tableName) => $"{schemaName}.[{tableName}]";
3434
public string ReturnId => ";SELECT @@IDENTITY";
3535
public string TimestampType => "datetime";
3636
public string Quote(string text) => $"\"{text}\"";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using grate.Configuration;
2+
using SqlServer.TestInfrastructure;
3+
using TestCommon.Generic.Running_MigrationScripts;
4+
using TestCommon.TestInfrastructure;
5+
6+
namespace SqlServer.Reported_issues;
7+
8+
// ReSharper disable once InconsistentNaming
9+
[Collection(nameof(SqlServerGrateTestContext))]
10+
public class Quoted_Identifier_440(SqlServerGrateTestContext context, ITestOutputHelper testOutput) : MigrationsScriptsBase(context, testOutput)
11+
{
12+
const string sql = """
13+
SET QUOTED_IDENTIFIER OFF
14+
GO
15+
CREATE PROCEDURE [do_something_or_something_else]
16+
@Blip nvarchar(128),
17+
@Blop nvarchar(128)
18+
AS
19+
BEGIN
20+
SELECT @@VERSION;
21+
END
22+
GO
23+
""";
24+
25+
[Fact]
26+
public async Task Setting_QUOTED_IDENTIFIER_OFF_in_a_script_does_not_mess_up_migration()
27+
{
28+
var db = TestConfig.RandomDatabase();
29+
30+
var parent = CreateRandomTempDirectory();
31+
var knownFolders = FoldersConfiguration.Default();
32+
33+
var path = new DirectoryInfo(Path.Combine(parent.ToString(), knownFolders[KnownFolderKeys.Sprocs]!.Path));
34+
35+
WriteSql(path, "some_sproc.sql", sql);
36+
37+
var config = GrateConfigurationBuilder.Create(Context.DefaultConfiguration)
38+
.WithConnectionString(Context.ConnectionString(db))
39+
.WithFolders(knownFolders)
40+
.WithSqlFilesDirectory(parent)
41+
.Build();
42+
43+
await using (var migrator = Context.Migrator.WithConfiguration(config))
44+
{
45+
await migrator.Migrate();
46+
}
47+
}
48+
}

unittests/SqlServer/Running_MigrationScripts/Real_world_issues.cs unittests/SqlServer/Reported_issues/Run_after_create_database_232.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
using TestCommon.TestInfrastructure;
66
using static grate.Configuration.KnownFolderKeys;
77

8-
namespace SqlServer.Running_MigrationScripts;
8+
namespace SqlServer.Reported_issues;
99

1010
// ReSharper disable once InconsistentNaming
1111
/// <summary>
1212
/// Issues that have been encountered in the real world.
1313
/// Create tests to reproduce the issue and then fix the issue, and keep the test to ensure it doesn't regress.
1414
/// </summary>
1515
[Collection(nameof(SqlServerGrateTestContext))]
16-
public class Real_world_issues(SqlServerGrateTestContext context, ITestOutputHelper testOutput) : MigrationsScriptsBase(context, testOutput)
16+
public class Run_after_create_database_232(SqlServerGrateTestContext context, ITestOutputHelper testOutput) : MigrationsScriptsBase(context, testOutput)
1717
{
1818
private const string Bug232Sql = @"
1919
ALTER DATABASE {{DatabaseName}} SET ALLOW_SNAPSHOT_ISOLATION ON;

0 commit comments

Comments
 (0)