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

Fix sql journal

This commit is contained in:
Michal Checinski
2023-09-01 17:20:43 +02:00
parent 3bbf36e69f
commit 953e645bf1

View File

@ -26,6 +26,15 @@ public class RerunableSqlTableJournal : SqlTableJournal
Rerunable = rerunable; Rerunable = rerunable;
} }
public override void StoreExecutedScript(SqlScript script, Func<IDbCommand> dbCommandFactory)
{
EnsureTableExistsAndIsLatestVersion(dbCommandFactory);
using (var command = GetInsertScriptCommand(dbCommandFactory, script))
{
command.ExecuteNonQuery();
}
}
protected new IDbCommand GetInsertScriptCommand(Func<IDbCommand> dbCommandFactory, SqlScript script) protected new IDbCommand GetInsertScriptCommand(Func<IDbCommand> dbCommandFactory, SqlScript script)
{ {
var command = dbCommandFactory(); var command = dbCommandFactory();
@ -35,6 +44,14 @@ public class RerunableSqlTableJournal : SqlTableJournal
scriptNameParam.Value = script.Name; scriptNameParam.Value = script.Name;
command.Parameters.Add(scriptNameParam); command.Parameters.Add(scriptNameParam);
var scriptFilename = script.Name.Replace("Bit.Migrator.", "");
scriptFilename = scriptFilename.Substring(scriptFilename.IndexOf('.')+1);
var scriptFileNameParam = command.CreateParameter();
scriptFileNameParam.ParameterName = "scriptFileName";
scriptFileNameParam.Value = $"%{scriptFilename}";
command.Parameters.Add(scriptFileNameParam);
var appliedParam = command.CreateParameter(); var appliedParam = command.CreateParameter();
appliedParam.ParameterName = "applied"; appliedParam.ParameterName = "applied";
appliedParam.Value = DateTime.Now; appliedParam.Value = DateTime.Now;
@ -45,28 +62,26 @@ public class RerunableSqlTableJournal : SqlTableJournal
rerunableParam.Value = Rerunable; rerunableParam.Value = Rerunable;
command.Parameters.Add(rerunableParam); command.Parameters.Add(rerunableParam);
command.CommandText = GetInsertJournalEntrySql("@scriptName", "@applied", "@rerunable"); command.CommandText = GetInsertJournalEntrySql("@scriptName", "@applied", "@rerunable", "@scriptFileName");
command.CommandType = CommandType.Text; command.CommandType = CommandType.Text;
return command; return command;
} }
protected string GetInsertJournalEntrySql(string @scriptName, string @applied, string @rerunable) protected string GetInsertJournalEntrySql(string @scriptName, string @applied, string @rerunable, string @scriptFileName)
{ {
return $"IF EXISTS (SELECT * FROM {FqSchemaTableName} WHERE Rerunable = 1 AND ScriptName ='{@scriptName}') " + return @$"IF EXISTS (SELECT * FROM {FqSchemaTableName} WHERE Rerunable = 1 AND ScriptName like {@scriptFileName})
"BEGIN " + BEGIN
$"UPDATE {FqSchemaTableName} SET Applied = {@applied}, Rerunable = {@rerunable} WHERE ScriptName = '{@scriptName}' " + UPDATE {FqSchemaTableName} SET ScriptName = {@scriptName}, Applied = {@applied}, Rerunable = {@rerunable} WHERE ScriptName like {@scriptFileName}
"END " + END
"ELSE " + ELSE
"BEGIN " + BEGIN
$"insert into {FqSchemaTableName} (ScriptName, Applied, Rerunable) values ({@scriptName}, {@applied}, {@rerunable}) " + insert into {FqSchemaTableName} (ScriptName, Applied, Rerunable) values ({@scriptName}, {@applied}, {@rerunable})
"END "; END ";
} }
protected override string GetJournalEntriesSql() protected override string GetJournalEntriesSql()
{ {
return @$"DECLARE @columnVariable AS NVARCHAR(max) return @$"DECLARE @columnVariable AS NVARCHAR(max)
DECLARE @SQLString AS NVARCHAR(max)
SELECT @columnVariable = SELECT @columnVariable =
CASE WHEN EXISTS CASE WHEN EXISTS
@ -78,14 +93,12 @@ public class RerunableSqlTableJournal : SqlTableJournal
'where [Rerunable] = 0' 'where [Rerunable] = 0'
) )
ELSE ELSE
('') (
''
)
END END
PRINT @columnVariable DECLARE @SQLString AS NVARCHAR(max) = N'select [ScriptName] from dbo.Migration ' + @columnVariable + ' order by [ScriptName]'
SET @SQLString = N'select [ScriptName] from dbo.Migration ' + @columnVariable + ' order by [ScriptName]'
PRINT @SQLString
EXECUTE sp_executesql @SQLString"; EXECUTE sp_executesql @SQLString";
} }