Skip to content

Commit

Permalink
fix erikbra#230: Added various syntax changes to ensure case (in)sens…
Browse files Browse the repository at this point in the history
…itivity in the server is respected
  • Loading branch information
wokket committed Sep 6, 2022
1 parent 4fec1c1 commit 26c7bf9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public async Task Uses_Server_Casing_Rules_For_Schema()
await using (var migrator = Context.GetMigrator(grateConfig))
{
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
Expand All @@ -88,7 +87,7 @@ public async Task Uses_Server_Casing_Rules_For_Schema()
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.
Assert.True(await migrator.DbMigrator.Database.VersionTableExists()); // we migrated into the `GRATE` schema, which may be the same as 'grate' depending on server settings.
}
}
}
12 changes: 6 additions & 6 deletions grate/Migration/AnsiSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task CloseAdminConnection()
await Close(_adminConnection);
_adminConnection = null;
}

protected async Task<DbConnection> OpenNewConnection()
{
var conn = GetSqlConnection(ConnectionString);
Expand Down Expand Up @@ -180,10 +180,10 @@ protected async Task WaitUntilDatabaseIsReady()

public async Task RunSupportTasks()
{
await CreateRunSchema();
await CreateScriptsRunTable();
await CreateScriptsRunErrorsTable();
await CreateVersionTable();
await CreateRunSchema();
await CreateScriptsRunTable();
await CreateScriptsRunErrorsTable();
await CreateVersionTable();
}

private async Task CreateRunSchema()
Expand All @@ -198,7 +198,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>(Connection, sql);
return res == SchemaName;
return res != null; // if the server found a row matching by name, that's good enough for us
}

// TODO: Change MySQL/MariaDB from using schemas to using grate_ prefix
Expand Down
11 changes: 9 additions & 2 deletions grate/Migration/PostgreSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace grate.Migration;

public class PostgreSqlDatabase : AnsiSqlDatabase
{
public PostgreSqlDatabase(ILogger<PostgreSqlDatabase> logger)
public PostgreSqlDatabase(ILogger<PostgreSqlDatabase> logger)
: base(logger, new PostgreSqlSyntax())
{ }

Expand All @@ -20,4 +20,11 @@ public override Task RestoreDatabase(string backupPath)
{
throw new System.NotImplementedException("Restoring a database from file is not currently supported for Postgresql.");
}
}

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);
}
}
14 changes: 7 additions & 7 deletions grate/Migration/SqLiteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace grate.Migration;
public class SqliteDatabase : AnsiSqlDatabase
{
private static readonly SqliteSyntax Syntax = new();
public SqliteDatabase(ILogger<SqliteDatabase> logger)


public SqliteDatabase(ILogger<SqliteDatabase> logger)
: base(logger, Syntax)
{ }

Expand All @@ -24,9 +24,9 @@ protected override string ExistsSql(string tableSchema, string fullTableName) =>
$@"
SELECT name FROM sqlite_master
WHERE type ='table' AND
name = '{fullTableName}';
";
name = '{fullTableName}' COLLATE NOCASE;
"; // #230: Correct mismatched schema casing, sqllite is case-insensitive but the string comparisons in queries _are_ case sensitive by default

public override string DatabaseName => GetDatabaseName(Connection);

/// <summary>
Expand All @@ -43,7 +43,7 @@ public override Task DropDatabase()
{
File.Delete(db);
}

return Task.CompletedTask;
}

Expand Down

0 comments on commit 26c7bf9

Please sign in to comment.