-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DEVOPS-1537 & DEVOPS-1519] Update dbo.Migrations table to support own migration schema #3212
Changes from all commits
d83554b
0d67ee7
4949c62
e47d544
7a7585e
068c73b
1968d11
2c3c869
683fd71
ee26db7
ff021b3
47f8555
c606efe
edc8139
d413b4b
98168fe
d73dbfb
a742316
d95c766
b5420e1
731be1f
3d65219
d82025b
5ecffda
5d7da92
d242e6e
c6df753
82c1742
928179e
8dab294
50145fb
3bbf36e
953e645
c3f58e6
df15fa9
00056cb
9ba7b94
54e92b5
3682633
95c14e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ALTER TABLE dbo.Migration | ||
ADD Repeatable bit NOT NULL DEFAULT 0 | ||
GO |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System.Data; | ||
using DbUp.Builder; | ||
using DbUp.Engine; | ||
using DbUp.Engine.Output; | ||
using DbUp.Engine.Transactions; | ||
using DbUp.SqlServer; | ||
|
||
namespace Bit.Migrator; | ||
|
||
public static class SqlTableJournalExtensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Did you ever look into my comment about using |
||
{ | ||
public static UpgradeEngineBuilder JournalRepeatableToSqlTable(this UpgradeEngineBuilder builder, string schema, string table, bool repeatable = false) | ||
{ | ||
builder.Configure(c => c.Journal = new RepeatableSqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table, repeatable)); | ||
return builder; | ||
} | ||
} | ||
|
||
public class RepeatableSqlTableJournal : SqlTableJournal | ||
{ | ||
private bool Repeatable { get; set; } | ||
|
||
public RepeatableSqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string schema, string table, bool repeatable = false) | ||
: base(connectionManager, logger, schema, table) | ||
{ | ||
Repeatable = repeatable; | ||
} | ||
|
||
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) | ||
{ | ||
var command = dbCommandFactory(); | ||
|
||
var scriptNameParam = command.CreateParameter(); | ||
scriptNameParam.ParameterName = "scriptName"; | ||
scriptNameParam.Value = script.Name; | ||
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(); | ||
appliedParam.ParameterName = "applied"; | ||
appliedParam.Value = DateTime.Now; | ||
command.Parameters.Add(appliedParam); | ||
|
||
var repeatableParam = command.CreateParameter(); | ||
repeatableParam.ParameterName = "repeatable"; | ||
repeatableParam.Value = Repeatable; | ||
command.Parameters.Add(repeatableParam); | ||
|
||
command.CommandText = GetInsertJournalEntrySql("@scriptName", "@applied", "@repeatable", "@scriptFileName"); | ||
command.CommandType = CommandType.Text; | ||
return command; | ||
} | ||
|
||
protected string GetInsertJournalEntrySql(string @scriptName, string @applied, string @repeatable, string @scriptFileName) | ||
{ | ||
return @$"IF EXISTS (SELECT * FROM {FqSchemaTableName} WHERE Repeatable = 1 AND ScriptName like {@scriptFileName}) | ||
BEGIN | ||
UPDATE {FqSchemaTableName} SET ScriptName = {@scriptName}, Applied = {@applied}, Repeatable = {@repeatable} WHERE ScriptName like {@scriptFileName} | ||
END | ||
ELSE | ||
BEGIN | ||
insert into {FqSchemaTableName} (ScriptName, Applied, Repeatable) values ({@scriptName}, {@applied}, {@repeatable}) | ||
END "; | ||
} | ||
|
||
protected override string GetJournalEntriesSql() | ||
{ | ||
return @$"DECLARE @columnVariable AS NVARCHAR(max) | ||
|
||
SELECT @columnVariable = | ||
CASE WHEN EXISTS | ||
( | ||
SELECT 1 FROM sys.columns WHERE Name = N'Repeatable' AND Object_ID = Object_ID(N'dbo.Migration') | ||
) | ||
THEN | ||
( | ||
'where [Repeatable] = 0' | ||
) | ||
ELSE | ||
( | ||
'' | ||
) | ||
END | ||
|
||
DECLARE @SQLString AS NVARCHAR(max) = N'select [ScriptName] from dbo.Migration ' + @columnVariable + ' order by [ScriptName]' | ||
|
||
EXECUTE sp_executesql @SQLString"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Is the
_data_migration
in docs somewhere, or a recommendation?DbDataMigrationScripts
orDbDataMigrations
is cleaner to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've got another card to rename this folder: https://bitwarden.atlassian.net/browse/DEVOPS-1520