diff options
Diffstat (limited to 'docker/crupest-api')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs | 9 | ||||
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs | 53 |
2 files changed, 0 insertions, 62 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs index cf10916..f1ae616 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs @@ -33,17 +33,8 @@ public class Table public List<TableColumn> Columns { get; set; } = new List<TableColumn>(); } -public class MigrationRecord -{ - public string TableName { get; set; } = default!; - public int Version { get; set; } - public Table Structure { get; set; } = default!; -} - public interface IDatabaseMigrator { - List<MigrationRecord> GetRecords(IDbConnection dbConnection, string tableName); - Table? GetTable(IDbConnection dbConnection, string tableName); Table ConvertTableInfoToTable(TableInfo tableInfo); string GenerateCreateTableColumnSqlSegment(TableColumn column); diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs index 536d8d6..33310d6 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs @@ -16,61 +16,8 @@ public class SqliteDatabaseMigrator : IDatabaseMigrator } } - private const string MigrationHistoryTableName = "migration_history"; - - private class MigrationRecordEntity - { - public string TableName { get; set; } = string.Empty; - public int Version { get; set; } - public string Structure { get; set; } = string.Empty; - } - - private void EnsureHistoryDatabase(IDbConnection dbConnection) - { - var exist = dbConnection.Query<int>($"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{MigrationHistoryTableName}';").Single() == 1; - if (!exist) - { - dbConnection.Execute($@" - CREATE TABLE {MigrationHistoryTableName} ( - Id INTEGER PRIMARY KEY AUTOINCREMENT, - TableName TEXT NOT NULL, - Version INT NOT NULL, - Structure TEXT NOT NULL - ); - "); - } - } - - public List<MigrationRecord> GetRecords(IDbConnection dbConnection, string tableName) - { - CheckTableName(tableName); - EnsureHistoryDatabase(dbConnection); - - var recordEntities = dbConnection.Query<MigrationRecordEntity>( - $"SELECT * FROM {MigrationHistoryTableName} WHERE TableName = @TableName ORDER BY Version ASC;", - new { TableName = tableName } - ).ToList(); - - var records = recordEntities.Select(entity => - { - var structure = JsonSerializer.Deserialize<Table>(entity.Structure); - if (structure is null) throw new Exception("Migration record is corrupted. Failed to convert structure."); - return new MigrationRecord - { - TableName = entity.TableName, - Version = entity.Version, - Structure = structure - }; - }).ToList(); - - return records; - } - - public Table? GetTable(IDbConnection dbConnection, string tableName) { - CheckTableName(tableName); - var count = dbConnection.QuerySingle<int>( "SELECT count(*) FROM sqlite_schema WHERE type = 'table' AND name = @TableName;", new { TableName = tableName }); |