From e0527ada1dfc45d990d0d4a8cfe08166e56613b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Moe?= Date: Thu, 29 Sep 2022 07:28:06 +0200 Subject: [PATCH] Detect existing lowercase table names --- grate/Migration/AnsiSqlDatabase.cs | 62 ++++++++++++++++++++++-------- grate/Migration/OracleDatabase.cs | 2 +- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/grate/Migration/AnsiSqlDatabase.cs b/grate/Migration/AnsiSqlDatabase.cs index 8ff60797..83a175c6 100644 --- a/grate/Migration/AnsiSqlDatabase.cs +++ b/grate/Migration/AnsiSqlDatabase.cs @@ -27,6 +27,8 @@ public abstract class AnsiSqlDatabase : IDatabase private DbConnection? _adminConnection; private readonly ISyntax _syntax; + protected bool LowercaseTableNames { get; set; } + private IDictionary? _scriptsRunCache; protected AnsiSqlDatabase(ILogger logger, ISyntax syntax) @@ -48,9 +50,9 @@ protected AnsiSqlDatabase(ILogger logger, ISyntax syntax) public string StatementSeparatorRegex => _syntax.StatementSeparatorRegex; - public string ScriptsRunTable => _syntax.TableWithSchema(SchemaName, "ScriptsRun"); - public string ScriptsRunErrorsTable => _syntax.TableWithSchema(SchemaName, "ScriptsRunErrors"); - public string VersionTable => _syntax.TableWithSchema(SchemaName, "Version"); + public string ScriptsRunTable => _syntax.TableWithSchema(SchemaName, ScriptsRunTableName); + public string ScriptsRunErrorsTable => _syntax.TableWithSchema(SchemaName, ScriptsRunErrorsTableName); + public string VersionTable => _syntax.TableWithSchema(SchemaName, VersionTableName); public virtual Task InitializeConnections(GrateConfiguration configuration) { @@ -237,13 +239,15 @@ protected async Task WaitUntilDatabaseIsReady() public async Task RunSupportTasks() { + LowercaseTableNames = await CheckIfExistingTablesAreLowercase(); + await CreateRunSchema(); await CreateScriptsRunTable(); await CreateScriptsRunErrorsTable(); await CreateVersionTable(); await AddStatusColumnToVersionTable(); - } - + } + private async Task CreateRunSchema() { if (SupportsSchemas && !await RunSchemaExists()) @@ -274,7 +278,7 @@ protected virtual async Task CreateScriptsRunTable() entry_date {_syntax.TimestampType} NULL, modified_date {_syntax.TimestampType} NULL, entered_by {_syntax.VarcharType}(50) NULL - {_syntax.PrimaryKeyConstraint("ScriptsRun", "id")} + {_syntax.PrimaryKeyConstraint(ScriptsRunTableName, "id")} )"; if (!await ScriptsRunTableExists()) @@ -297,7 +301,7 @@ protected virtual async Task CreateScriptsRunErrorsTable() entry_date {_syntax.TimestampType} NULL, modified_date {_syntax.TimestampType} NULL, entered_by {_syntax.VarcharType}(50) NULL - {_syntax.PrimaryKeyConstraint("ScriptsRunErrors", "id")} + {_syntax.PrimaryKeyConstraint(ScriptsRunErrorsTableName, "id")} )"; if (!await ScriptsRunErrorsTableExists()) { @@ -315,7 +319,7 @@ protected virtual async Task CreateVersionTable() entry_date {_syntax.TimestampType} NULL, modified_date {_syntax.TimestampType} NULL, entered_by {_syntax.VarcharType}(50) NULL - {_syntax.PrimaryKeyConstraint("Version", "id")} + {_syntax.PrimaryKeyConstraint(VersionTableName, "id")} )"; if (!await VersionTableExists()) { @@ -335,11 +339,17 @@ ALTER TABLE {VersionTable} } } - protected async Task ScriptsRunTableExists() => await TableExists(SchemaName, "ScriptsRun"); - protected async Task ScriptsRunErrorsTableExists() => await TableExists(SchemaName, "ScriptsRunErrors"); - public async Task VersionTableExists() => await TableExists(SchemaName, "Version"); - protected async Task StatusColumnInVersionTableExists() => await ColumnExists(SchemaName, "Version", "status"); + protected async Task ScriptsRunTableExists() => await TableExists(SchemaName, ScriptsRunTableName); + protected async Task ScriptsRunErrorsTableExists() => await TableExists(SchemaName, ScriptsRunErrorsTableName); + public async Task VersionTableExists() => await TableExists(SchemaName, VersionTableName); + protected async Task StatusColumnInVersionTableExists() => await ColumnExists(SchemaName, VersionTableName, "status"); + /// + /// Returns name in db if table exists. + /// + /// + /// + /// public async Task TableExists(string schemaName, string tableName) { var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName); @@ -347,11 +357,21 @@ public async Task TableExists(string schemaName, string tableName) string existsSql = ExistsSql(tableSchema, fullTableName); - var res = await ExecuteScalarAsync(ActiveConnection, existsSql); + var res = await ExecuteScalarAsync(ActiveConnection, existsSql); return !DBNull.Value.Equals(res) && res is not null; } + public async Task GetExistingTableName(string schemaName, string tableName) + { + var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName); + var tableSchema = SupportsSchemas ? schemaName : DatabaseName; + + string existsSql = ExistsSql(tableSchema, fullTableName); + + return await ExecuteScalarAsync(ActiveConnection, existsSql); + } + private async Task ColumnExists(string schemaName, string tableName, string columnName) { var fullTableName = SupportsSchemas ? tableName : _syntax.TableWithSchema(schemaName, tableName); @@ -366,10 +386,10 @@ private async Task ColumnExists(string schemaName, string tableName, strin protected virtual string ExistsSql(string tableSchema, string fullTableName) { return $@" -SELECT * FROM information_schema.tables +SELECT table_name FROM information_schema.tables WHERE table_schema = '{tableSchema}' AND -table_name = '{fullTableName}' +LOWER(table_name) = LOWER('{fullTableName}') "; } @@ -615,6 +635,12 @@ INSERT INTO {ScriptsRunErrorsTable} await ExecuteAsync(ActiveConnection, insertSql, scriptRunErrors); } + private async Task CheckIfExistingTablesAreLowercase() + { + var tn = await GetExistingTableName(SchemaName, "ScriptsRun"); + return tn == "scriptsrun"; + } + private static async Task Close(DbConnection? conn) { if (conn?.State == ConnectionState.Open) @@ -674,4 +700,10 @@ public async ValueTask DisposeAsync() } public abstract Task RestoreDatabase(string backupPath); + + private string ScriptsRunTableName => TableName("ScriptsRun"); + private string VersionTableName => TableName("Version"); + private string ScriptsRunErrorsTableName => TableName("ScriptsRunErrors"); + + public virtual string TableName(string tableName) => LowercaseTableNames ? tableName.ToLowerInvariant() : tableName; } diff --git a/grate/Migration/OracleDatabase.cs b/grate/Migration/OracleDatabase.cs index f6f7080c..e29c2cc1 100644 --- a/grate/Migration/OracleDatabase.cs +++ b/grate/Migration/OracleDatabase.cs @@ -29,7 +29,7 @@ public OracleDatabase(ILogger logger) protected override string ExistsSql(string tableSchema, string fullTableName) => $@" -SELECT * FROM user_tables +SELECT table_name FROM user_tables WHERE lower(table_name) = '{fullTableName.ToLowerInvariant()}' ";