Skip to content

Commit e0527ad

Browse files
committed
Detect existing lowercase table names
1 parent 2051845 commit e0527ad

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

grate/Migration/AnsiSqlDatabase.cs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public abstract class AnsiSqlDatabase : IDatabase
2727
private DbConnection? _adminConnection;
2828
private readonly ISyntax _syntax;
2929

30+
protected bool LowercaseTableNames { get; set; }
31+
3032
private IDictionary<string, string>? _scriptsRunCache;
3133

3234
protected AnsiSqlDatabase(ILogger logger, ISyntax syntax)
@@ -48,9 +50,9 @@ protected AnsiSqlDatabase(ILogger logger, ISyntax syntax)
4850

4951
public string StatementSeparatorRegex => _syntax.StatementSeparatorRegex;
5052

51-
public string ScriptsRunTable => _syntax.TableWithSchema(SchemaName, "ScriptsRun");
52-
public string ScriptsRunErrorsTable => _syntax.TableWithSchema(SchemaName, "ScriptsRunErrors");
53-
public string VersionTable => _syntax.TableWithSchema(SchemaName, "Version");
53+
public string ScriptsRunTable => _syntax.TableWithSchema(SchemaName, ScriptsRunTableName);
54+
public string ScriptsRunErrorsTable => _syntax.TableWithSchema(SchemaName, ScriptsRunErrorsTableName);
55+
public string VersionTable => _syntax.TableWithSchema(SchemaName, VersionTableName);
5456

5557
public virtual Task InitializeConnections(GrateConfiguration configuration)
5658
{
@@ -237,13 +239,15 @@ protected async Task WaitUntilDatabaseIsReady()
237239

238240
public async Task RunSupportTasks()
239241
{
242+
LowercaseTableNames = await CheckIfExistingTablesAreLowercase();
243+
240244
await CreateRunSchema();
241245
await CreateScriptsRunTable();
242246
await CreateScriptsRunErrorsTable();
243247
await CreateVersionTable();
244248
await AddStatusColumnToVersionTable();
245-
}
246-
249+
}
250+
247251
private async Task CreateRunSchema()
248252
{
249253
if (SupportsSchemas && !await RunSchemaExists())
@@ -274,7 +278,7 @@ protected virtual async Task CreateScriptsRunTable()
274278
entry_date {_syntax.TimestampType} NULL,
275279
modified_date {_syntax.TimestampType} NULL,
276280
entered_by {_syntax.VarcharType}(50) NULL
277-
{_syntax.PrimaryKeyConstraint("ScriptsRun", "id")}
281+
{_syntax.PrimaryKeyConstraint(ScriptsRunTableName, "id")}
278282
)";
279283

280284
if (!await ScriptsRunTableExists())
@@ -297,7 +301,7 @@ protected virtual async Task CreateScriptsRunErrorsTable()
297301
entry_date {_syntax.TimestampType} NULL,
298302
modified_date {_syntax.TimestampType} NULL,
299303
entered_by {_syntax.VarcharType}(50) NULL
300-
{_syntax.PrimaryKeyConstraint("ScriptsRunErrors", "id")}
304+
{_syntax.PrimaryKeyConstraint(ScriptsRunErrorsTableName, "id")}
301305
)";
302306
if (!await ScriptsRunErrorsTableExists())
303307
{
@@ -315,7 +319,7 @@ protected virtual async Task CreateVersionTable()
315319
entry_date {_syntax.TimestampType} NULL,
316320
modified_date {_syntax.TimestampType} NULL,
317321
entered_by {_syntax.VarcharType}(50) NULL
318-
{_syntax.PrimaryKeyConstraint("Version", "id")}
322+
{_syntax.PrimaryKeyConstraint(VersionTableName, "id")}
319323
)";
320324
if (!await VersionTableExists())
321325
{
@@ -335,23 +339,39 @@ ALTER TABLE {VersionTable}
335339
}
336340
}
337341

338-
protected async Task<bool> ScriptsRunTableExists() => await TableExists(SchemaName, "ScriptsRun");
339-
protected async Task<bool> ScriptsRunErrorsTableExists() => await TableExists(SchemaName, "ScriptsRunErrors");
340-
public async Task<bool> VersionTableExists() => await TableExists(SchemaName, "Version");
341-
protected async Task<bool> StatusColumnInVersionTableExists() => await ColumnExists(SchemaName, "Version", "status");
342+
protected async Task<bool> ScriptsRunTableExists() => await TableExists(SchemaName, ScriptsRunTableName);
343+
protected async Task<bool> ScriptsRunErrorsTableExists() => await TableExists(SchemaName, ScriptsRunErrorsTableName);
344+
public async Task<bool> VersionTableExists() => await TableExists(SchemaName, VersionTableName);
345+
protected async Task<bool> StatusColumnInVersionTableExists() => await ColumnExists(SchemaName, VersionTableName, "status");
342346

347+
/// <summary>
348+
/// Returns name in db if table exists.
349+
/// </summary>
350+
/// <param name="schemaName"></param>
351+
/// <param name="tableName"></param>
352+
/// <returns></returns>
343353
public async Task<bool> TableExists(string schemaName, string tableName)
344354
{
345355
var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName);
346356
var tableSchema = SupportsSchemas ? schemaName : DatabaseName;
347357

348358
string existsSql = ExistsSql(tableSchema, fullTableName);
349359

350-
var res = await ExecuteScalarAsync<object>(ActiveConnection, existsSql);
360+
var res = await ExecuteScalarAsync<string>(ActiveConnection, existsSql);
351361

352362
return !DBNull.Value.Equals(res) && res is not null;
353363
}
354364

365+
public async Task<string?> GetExistingTableName(string schemaName, string tableName)
366+
{
367+
var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName);
368+
var tableSchema = SupportsSchemas ? schemaName : DatabaseName;
369+
370+
string existsSql = ExistsSql(tableSchema, fullTableName);
371+
372+
return await ExecuteScalarAsync<string?>(ActiveConnection, existsSql);
373+
}
374+
355375
private async Task<bool> ColumnExists(string schemaName, string tableName, string columnName)
356376
{
357377
var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName);
@@ -366,10 +386,10 @@ private async Task<bool> ColumnExists(string schemaName, string tableName, strin
366386
protected virtual string ExistsSql(string tableSchema, string fullTableName)
367387
{
368388
return $@"
369-
SELECT * FROM information_schema.tables
389+
SELECT table_name FROM information_schema.tables
370390
WHERE
371391
table_schema = '{tableSchema}' AND
372-
table_name = '{fullTableName}'
392+
LOWER(table_name) = LOWER('{fullTableName}')
373393
";
374394
}
375395

@@ -615,6 +635,12 @@ INSERT INTO {ScriptsRunErrorsTable}
615635
await ExecuteAsync(ActiveConnection, insertSql, scriptRunErrors);
616636
}
617637

638+
private async Task<bool> CheckIfExistingTablesAreLowercase()
639+
{
640+
var tn = await GetExistingTableName(SchemaName, "ScriptsRun");
641+
return tn == "scriptsrun";
642+
}
643+
618644
private static async Task Close(DbConnection? conn)
619645
{
620646
if (conn?.State == ConnectionState.Open)
@@ -674,4 +700,10 @@ public async ValueTask DisposeAsync()
674700
}
675701

676702
public abstract Task RestoreDatabase(string backupPath);
703+
704+
private string ScriptsRunTableName => TableName("ScriptsRun");
705+
private string VersionTableName => TableName("Version");
706+
private string ScriptsRunErrorsTableName => TableName("ScriptsRunErrors");
707+
708+
public virtual string TableName(string tableName) => LowercaseTableNames ? tableName.ToLowerInvariant() : tableName;
677709
}

grate/Migration/OracleDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public OracleDatabase(ILogger<OracleDatabase> logger)
2929

3030
protected override string ExistsSql(string tableSchema, string fullTableName) =>
3131
$@"
32-
SELECT * FROM user_tables
32+
SELECT table_name FROM user_tables
3333
WHERE
3434
lower(table_name) = '{fullTableName.ToLowerInvariant()}'
3535
";

0 commit comments

Comments
 (0)