1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-03 00:52:49 -05:00

Add rerunable schema

This commit is contained in:
Michal Checinski
2023-08-23 17:40:47 +02:00
parent ee26db7e98
commit ff021b3854

View File

@ -10,19 +10,21 @@ namespace Bit.Migrator;
public static class SqlTableJournalExtensions
{
public static UpgradeEngineBuilder JournalRerunableToSqlTable(this UpgradeEngineBuilder builder, string schema, string table)
public static UpgradeEngineBuilder JournalRerunableToSqlTable(this UpgradeEngineBuilder builder, string schema, string table, bool rerunable = false)
{
builder.Configure(c => c.Journal = new RerunableSqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table));
builder.Configure(c => c.Journal = new RerunableSqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table, rerunable));
return builder;
}
}
public class RerunableSqlTableJournal : TableJournal
public class RerunableSqlTableJournal : SqlTableJournal
{
private bool Rerunable { get; set; }
public RerunableSqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string schema, string table)
: base(connectionManager, logger, new SqlServerObjectParser(), schema, table)
public RerunableSqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string schema, string table, bool rerunable = false)
: base(connectionManager, logger, schema, table)
{
Rerunable = rerunable;
}
protected new IDbCommand GetInsertScriptCommand(Func<IDbCommand> dbCommandFactory, SqlScript script)
@ -39,19 +41,24 @@ public class RerunableSqlTableJournal : TableJournal
appliedParam.Value = DateTime.Now;
command.Parameters.Add(appliedParam);
command.CommandText = GetInsertJournalEntrySql("@scriptName", "@applied");
var rerunableParam = command.CreateParameter();
rerunableParam.ParameterName = "rerunable";
rerunableParam.Value = Rerunable;
command.Parameters.Add(rerunableParam);
command.CommandText = GetInsertJournalEntrySql("@scriptName", "@applied", "@rerrunable");
command.CommandType = CommandType.Text;
return command;
}
protected override string GetInsertJournalEntrySql(string @scriptName, string @applied)
protected override string GetInsertJournalEntrySql(string @scriptName, string @applied, string @rerrunable)
{
return $"insert into {FqSchemaTableName} (ScriptName, Applied) values ({@scriptName}, {@applied})";
return $"insert into {FqSchemaTableName} (ScriptName, Applied, Rerunable) values ({@scriptName}, {@applied}, {@rerrunable})";
}
protected override string GetJournalEntriesSql()
{
return $"select [ScriptName] from {FqSchemaTableName} order by [ScriptName]";
return $"select [ScriptName] from {FqSchemaTableName} where [Rerunable] = 0 order by [ScriptName]";
}
protected override string CreateSchemaTableSql(string quotedPrimaryKeyName)
@ -60,7 +67,8 @@ public class RerunableSqlTableJournal : TableJournal
$@"create table {FqSchemaTableName} (
[Id] int identity(1,1) not null constraint {quotedPrimaryKeyName} primary key,
[ScriptName] nvarchar(255) not null,
[Applied] datetime not null
[Applied] datetime not null,
[Rerunable] bit not null DEFAULT 0
)";
}
}