aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-25 21:47:08 +0800
committercrupest <crupest@outlook.com>2022-12-25 21:47:08 +0800
commit93a13a46fca7f19169f5985427cef7ce68b2315c (patch)
tree6590eb06b9b66388a6fec5d41167e7a7066fa3d3
parent08c1b22cc48a7e045090677a04f4d3e106476ecb (diff)
downloadcrupest-93a13a46fca7f19169f5985427cef7ce68b2315c.tar.gz
crupest-93a13a46fca7f19169f5985427cef7ce68b2315c.tar.bz2
crupest-93a13a46fca7f19169f5985427cef7ce68b2315c.zip
Add migration. v4.0
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/DatabaseMigrator.cs9
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/Migrations/SqliteDatabaseMigrator.cs53
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 });