Skip to content

Commit

Permalink
fix #230: Fix schema (in)sensitive casing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wokket authored and erikbra committed Dec 19, 2023
1 parent fc23c58 commit 6f0c140
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion grate/Migration/AnsiSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private async Task<bool> RunSchemaExists()
{
string sql = $"SELECT s.SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA s WHERE s.SCHEMA_NAME = '{SchemaName}'";
var res = await ExecuteScalarAsync<string>(ActiveConnection, sql);
return res == SchemaName;
return res != null; // #230: If the server found a record that's good enough for us
}

// TODO: Change MySQL/MariaDB from using schemas to using grate_ prefix
Expand Down
14 changes: 14 additions & 0 deletions grate/Migration/PostgreSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ public override Task RestoreDatabase(string backupPath)

public override IEnumerable<string> GetStatements(string sql)
=> ReflectionNpgsqlQueryParser.Split(sql);

protected override string ExistsSql(string tableSchema, string fullTableName)
{
// For #230. Postgres tables are lowercase by default unless you quote them when created, which we do. We _don't_ quote the schema though, so it will always be lowercase
// Ensure the table check uses the lowercase version of anything we're passed, as that's what we would have created.
return base.ExistsSql(tableSchema.ToLower(), fullTableName);
}

protected override string ExistsSql(string tableSchema, string fullTableName, string columnName)
{
// For #230. Postgres tables are lowercase by default unless you quote them when created, which we do. We _don't_ quote the schema though, so it will always be lowercase
// Ensure the table check uses the lowercase version of anything we're passed, as that's what we would have created.
return base.ExistsSql(tableSchema.ToLower(), fullTableName, columnName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,33 @@ public async Task Creates_a_new_version_with_status_InProgress()
var version = entries.Single(x => x.version == dbVersion);
version.status.Should().Be(MigrationStatus.InProgress);
}

[Test]
public async Task Bug230_Uses_Server_Casing_Rules_For_Schema()
{
//for bug #230 - when targeting an existing schema use the servers casing rules, not .Net's
var db = TestConfig.RandomDatabase();
var parent = CreateRandomTempDirectory();
var knownFolders = FoldersConfiguration.Default(null);

CreateDummySql(parent, knownFolders[Sprocs]); // make sure there's something that could be logged...

await using (var migrator = Context.GetMigrator(db, parent, knownFolders))
{
await migrator.Migrate();
Assert.True(await migrator.DbMigrator.Database.VersionTableExists()); // we migrated into the `grate` schema.
}

// Now we'll run again with the same name but different cased schema
var grateConfig = Context.GetConfiguration(db, parent, knownFolders) with
{
SchemaName = "GRATE"
};

await using (var migrator = Context.GetMigrator(grateConfig))
{
await migrator.Migrate(); // should either reuse the existing schema if a case-insensitive server, or create a new second schema for use if case-sensitive.
Assert.True(await migrator.DbMigrator.Database.VersionTableExists()); // we migrated into the `GRATE` schema, which may be the same as 'grate' depending on server settings.
}
}
}

0 comments on commit 6f0c140

Please sign in to comment.