@@ -43,26 +43,40 @@ protected AnsiSqlDatabase(ILogger logger, ISyntax syntax)
43
43
. Split ( "=" , TrimEntries | RemoveEmptyEntries ) . Last ( ) ;
44
44
45
45
public abstract bool SupportsDdlTransactions { get ; }
46
- protected abstract bool SupportsSchemas { get ; }
46
+ public abstract bool SupportsSchemas { get ; }
47
47
public bool SplitBatchStatements => true ;
48
48
49
49
public string StatementSeparatorRegex => _syntax . StatementSeparatorRegex ;
50
50
51
- public string ScriptsRunTable => _syntax . TableWithSchema ( SchemaName , "ScriptsRun" ) ;
52
- public string ScriptsRunErrorsTable => _syntax . TableWithSchema ( SchemaName , "ScriptsRunErrors" ) ;
53
- public string VersionTable => _syntax . TableWithSchema ( SchemaName , "Version" ) ;
51
+ public string ScriptsRunTable => _syntax . TableWithSchema ( SchemaName , ScriptsRunTableName ) ;
52
+ public string ScriptsRunErrorsTable => _syntax . TableWithSchema ( SchemaName , ScriptsRunErrorsTableName ) ;
53
+ public string VersionTable => _syntax . TableWithSchema ( SchemaName , VersionTableName ) ;
54
54
55
- public virtual Task InitializeConnections ( GrateConfiguration configuration )
55
+ private string ScriptsRunTableName { get ; set ; }
56
+ private string ScriptsRunErrorsTableName { get ; set ; }
57
+ private string VersionTableName { get ; set ; }
58
+
59
+ public virtual async Task InitializeConnections ( GrateConfiguration configuration )
56
60
{
57
61
Logger . LogInformation ( "Initializing connections." ) ;
58
62
59
63
ConnectionString = configuration . ConnectionString ;
60
64
AdminConnectionString = configuration . AdminConnectionString ;
65
+
61
66
SchemaName = configuration . SchemaName ;
67
+
68
+ VersionTableName = configuration . VersionTableName ;
69
+ ScriptsRunTableName = configuration . ScriptsRunTableName ;
70
+ ScriptsRunErrorsTableName = configuration . ScriptsRunErrorsTableName ;
71
+
62
72
Config = configuration ;
63
- return Task . CompletedTask ;
64
73
}
65
74
75
+ private async Task < string > ExistingOrDefault ( string schemaName , string tableName ) =>
76
+ await ExistingTable ( schemaName , tableName ) ?? tableName ;
77
+
78
+
79
+
66
80
private string ? AdminConnectionString { get ; set ; }
67
81
protected string ? ConnectionString { get ; set ; }
68
82
@@ -263,6 +277,9 @@ private async Task<bool> RunSchemaExists()
263
277
264
278
protected virtual async Task CreateScriptsRunTable ( )
265
279
{
280
+ // Update scripts run table name with the correct casing, should it differ from the standard
281
+ ScriptsRunTableName = await ExistingOrDefault ( SchemaName , ScriptsRunTableName ) ;
282
+
266
283
string createSql = $@ "
267
284
CREATE TABLE { ScriptsRunTable } (
268
285
{ _syntax . PrimaryKeyColumn ( "id" ) } ,
@@ -285,6 +302,9 @@ protected virtual async Task CreateScriptsRunTable()
285
302
286
303
protected virtual async Task CreateScriptsRunErrorsTable ( )
287
304
{
305
+ // Update scripts run errors table name with the correct casing, should it differ from the standard
306
+ ScriptsRunErrorsTableName = await ExistingOrDefault ( SchemaName , ScriptsRunErrorsTableName ) ;
307
+
288
308
string createSql = $@ "
289
309
CREATE TABLE { ScriptsRunErrorsTable } (
290
310
{ _syntax . PrimaryKeyColumn ( "id" ) } ,
@@ -307,6 +327,9 @@ protected virtual async Task CreateScriptsRunErrorsTable()
307
327
308
328
protected virtual async Task CreateVersionTable ( )
309
329
{
330
+ // Update version table name with the correct casing, should it differ from the standard
331
+ VersionTableName = await ExistingOrDefault ( SchemaName , VersionTableName ) ;
332
+
310
333
string createSql = $@ "
311
334
CREATE TABLE { VersionTable } (
312
335
{ _syntax . PrimaryKeyColumn ( "id" ) } ,
@@ -317,6 +340,7 @@ protected virtual async Task CreateVersionTable()
317
340
entered_by { _syntax . VarcharType } (50) NULL
318
341
{ _syntax . PrimaryKeyConstraint ( "Version" , "id" ) }
319
342
)" ;
343
+
320
344
if ( ! await VersionTableExists ( ) )
321
345
{
322
346
await ExecuteNonQuery ( ActiveConnection , createSql , Config ? . CommandTimeout ) ;
@@ -335,21 +359,26 @@ ALTER TABLE {VersionTable}
335
359
}
336
360
}
337
361
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" ) ;
362
+ protected async Task < bool > ScriptsRunTableExists ( ) => ( await ExistingTable ( SchemaName , ScriptsRunTableName ) is not null ) ;
363
+ protected async Task < bool > ScriptsRunErrorsTableExists ( ) => ( await ExistingTable ( SchemaName , ScriptsRunErrorsTableName ) is not null ) ;
364
+ public async Task < bool > VersionTableExists ( ) => ( await ExistingTable ( SchemaName , VersionTableName ) is not null ) ;
365
+
366
+ protected async Task < bool > StatusColumnInVersionTableExists ( ) => await ColumnExists ( SchemaName , VersionTableName , "status" ) ;
342
367
343
- public async Task < bool > TableExists ( string schemaName , string tableName )
368
+ public async Task < string ? > ExistingTable ( string schemaName , string tableName )
344
369
{
345
370
var fullTableName = SupportsSchemas ? tableName : _syntax . TableWithSchema ( schemaName , tableName ) ;
346
371
var tableSchema = SupportsSchemas ? schemaName : DatabaseName ;
372
+
347
373
348
374
string existsSql = ExistsSql ( tableSchema , fullTableName ) ;
349
375
350
376
var res = await ExecuteScalarAsync < object > ( ActiveConnection , existsSql ) ;
351
377
352
- return ! DBNull . Value . Equals ( res ) && res is not null ;
378
+ var name = ( ! DBNull . Value . Equals ( res ) && res is not null ) ? ( string ) res : null ;
379
+
380
+ var prefix = SupportsSchemas ? string . Empty : _syntax . TableWithSchema ( schemaName , string . Empty ) ;
381
+ return name ? [ prefix . Length ..] ;
353
382
}
354
383
355
384
private async Task < bool > ColumnExists ( string schemaName , string tableName , string columnName )
@@ -366,10 +395,10 @@ private async Task<bool> ColumnExists(string schemaName, string tableName, strin
366
395
protected virtual string ExistsSql ( string tableSchema , string fullTableName )
367
396
{
368
397
return $@ "
369
- SELECT * FROM information_schema.tables
398
+ SELECT table_name FROM information_schema.tables
370
399
WHERE
371
- table_schema = '{ tableSchema } ' AND
372
- table_name = '{ fullTableName } '
400
+ LOWER( table_schema) = LOWER( '{ tableSchema } ') AND
401
+ LOWER( table_name) = LOWER( '{ fullTableName } ')
373
402
" ;
374
403
}
375
404
@@ -378,9 +407,9 @@ protected virtual string ExistsSql(string tableSchema, string fullTableName, str
378
407
return $@ "
379
408
SELECT * FROM information_schema.columns
380
409
WHERE
381
- table_schema = '{ tableSchema } ' AND
382
- table_name = '{ fullTableName } ' AND
383
- column_name = '{ columnName } '
410
+ LOWER( table_schema) = LOWER( '{ tableSchema } ') AND
411
+ LOWER( table_name) = LOWER( '{ fullTableName } ') AND
412
+ LOWER( column_name) = LOWER( '{ columnName } ')
384
413
" ;
385
414
}
386
415
0 commit comments