-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMSSQLMigrations.cs
73 lines (66 loc) · 3.46 KB
/
MSSQLMigrations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Data;
using CentridNet.EFCoreAutoMigrator.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace CentridNet.EFCoreAutoMigrator.MigrationContexts{
public static class SqlServerMigrationsExtensions{
public static MigrationsProvider SqlServerDBMigrations(this DbContext dbContext, DBMigratorProps dbMigratorProps, MigrationScriptExecutor migrationScriptExecutor){
return new MSSQLMigrations(dbMigratorProps, migrationScriptExecutor);
}
}
public class MSSQLMigrations : MigrationsProvider
{
public MSSQLMigrations(DBMigratorProps dbMigratorProps, MigrationScriptExecutor migrationScriptExecutor) : base(dbMigratorProps, migrationScriptExecutor){}
protected override void EnsureMigrateTablesExist()
{
DataTable resultDataTable = dbContext.ExecuteSqlRawWithoutModel($"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'{DalConsts.MIGRATION_TABLE_NAME}';");
if (resultDataTable.Rows.Count == 0 || !Convert.ToBoolean(resultDataTable.Rows[0][0])){
migrationScriptExecutor.AddSQLCommand($@"CREATE TABLE {DalConsts.MIGRATION_TABLE_NAME} (
runId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
runDate DATETIME,
efcoreVersion VARCHAR (355) NOT NULL,
metadata TEXT,
snapshot VARBINARY(MAX) NOT NULL
);");
}
}
protected override void EnsureSnapshotLimitNotReached()
{
if (snapshotHistoryLimit > 0){
migrationScriptExecutor.AddSQLCommand($"DELETE FROM {DalConsts.MIGRATION_TABLE_NAME} WHERE runId NOT IN (SELECT TOP {snapshotHistoryLimit-1} runId FROM {DalConsts.MIGRATION_TABLE_NAME} ORDER BY runId DESC);");
}
}
protected override AutoMigratorTable GetLastMigrationRecord()
{
IList<AutoMigratorTable> migrationMetadata = dbContext.ExecuteSqlRawWithoutModel<AutoMigratorTable>($"SELECT TOP 1 * FROM {DalConsts.MIGRATION_TABLE_NAME} ORDER BY runId DESC;", (dbDataReader) => {
return new AutoMigratorTable(){
runId = (int)dbDataReader[0],
runDate = (DateTime)dbDataReader[1],
efcoreVersion = (string)dbDataReader[2],
metadata = (string)dbDataReader[3],
snapshot = (byte[])dbDataReader[4]
};
});
if (migrationMetadata.Count >0){
return migrationMetadata[0];
}
return null;
}
protected override void UpdateMigrationTables(byte[] snapshotData)
{
migrationScriptExecutor.AddSQLCommand($@"INSERT INTO {DalConsts.MIGRATION_TABLE_NAME} (
runDate,
efcoreVersion,
metadata,
snapshot
)
VALUES
(getdate(),
'{typeof(DbContext).Assembly.GetName().Version.ToString()}',
'{migrationMetadata.metadata}',
{"0x"+BitConverter.ToString(snapshotData).Replace("-", "")});");
}
}
}