mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[DEVOPS-1519] Add transition mode to mssql migrator utility (#3259)
* Add RerunableSqlTableJournal
* Add extension to use rerunable sql table journal
* Use rerunable sql journal
* format
* Enable logging
* FIx
* Disable logging
* Rename to SqlTableJournalExtensions
* Move RerunableSqlTableJournal to Extension class
* Fix usings
* Add rerunable schema
* Format
* Fix typo
* Enable logging in db migrator
* add rerunable column in dbo migrations table migration
* Trying
* Fix journal table name
* Trying to migrate first
* After migration
* Testing
* Add update from rerunable to not rerunable script
* Change name
* Add rerunable option and script folder name
* Add rerunable options and folder
* Fix
* Add transition (aka rerunable) migrations to Setup
* Parse parameters on migrator utility
* Fix sql scripts
* Remove CreateSchemaTableSql as it'll be migrated using migration
* Embed dbScripts_data_migration folder
* Remove testing sql script
* Add optins parsing nuget for msSqlMigratorUtility
* Fix sql journal
* Ran dotnet format
* Comment out index
* ▫️Revert "Comment out index"
This reverts commit df15fa91e0
.
* Disable logging
* Add newline
* Rename rerunable to repeatable
* remove repeatable journal
* Remove migration adding the repeatable column in dbo.Migrations table
* Add using
* Enable log for testing
* Disable logging in the setup
* Remove unused method
* Add migrator constants
* Use constants in yet another place
* Fix
* Add constant
* Fix
* Fix
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using System.Reflection;
|
||||
using Bit.Core;
|
||||
using DbUp;
|
||||
using DbUp.Helpers;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -24,6 +25,8 @@ public class DbMigrator
|
||||
}
|
||||
|
||||
public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true,
|
||||
bool repeatable = false,
|
||||
string folderName = MigratorConstants.DefaultMigrationsFolderName,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var attempt = 1;
|
||||
@ -32,7 +35,7 @@ public class DbMigrator
|
||||
{
|
||||
try
|
||||
{
|
||||
var success = MigrateDatabase(enableLogging, cancellationToken);
|
||||
var success = MigrateDatabase(enableLogging, repeatable, folderName, cancellationToken);
|
||||
return success;
|
||||
}
|
||||
catch (SqlException ex)
|
||||
@ -54,6 +57,8 @@ public class DbMigrator
|
||||
}
|
||||
|
||||
public bool MigrateDatabase(bool enableLogging = true,
|
||||
bool repeatable = false,
|
||||
string folderName = MigratorConstants.DefaultMigrationsFolderName,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (_logger != null)
|
||||
@ -98,12 +103,20 @@ public class DbMigrator
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var builder = DeployChanges.To
|
||||
.SqlDatabase(_connectionString)
|
||||
.JournalToSqlTable("dbo", "Migration")
|
||||
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
|
||||
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
|
||||
s => s.Contains($".{folderName}.") && !s.Contains(".Archive."))
|
||||
.WithTransaction()
|
||||
.WithExecutionTimeout(new TimeSpan(0, 5, 0));
|
||||
|
||||
if (repeatable)
|
||||
{
|
||||
builder.JournalTo(new NullJournal());
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.JournalToSqlTable("dbo", MigratorConstants.SqlTableJournalName);
|
||||
}
|
||||
|
||||
if (enableLogging)
|
||||
{
|
||||
if (_logger != null)
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="DbScripts\**\*.sql" />
|
||||
<EmbeddedResource Include="DbScripts_data_migration\**\*.sql" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
8
util/Migrator/MigratorConstants.cs
Normal file
8
util/Migrator/MigratorConstants.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Bit.Migrator;
|
||||
|
||||
public static class MigratorConstants
|
||||
{
|
||||
public const string SqlTableJournalName = "Migration";
|
||||
public const string DefaultMigrationsFolderName = "DbScripts";
|
||||
public const string TransitionMigrationsFolderName = "DbScripts_data_migration";
|
||||
}
|
@ -70,7 +70,7 @@ public class SqlServerDbMigrator : IDbMigrator
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var builder = DeployChanges.To
|
||||
.SqlDatabase(_connectionString)
|
||||
.JournalToSqlTable("dbo", "Migration")
|
||||
.JournalToSqlTable("dbo", MigratorConstants.SqlTableJournalName)
|
||||
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
|
||||
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
|
||||
.WithTransaction()
|
||||
|
Reference in New Issue
Block a user