mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 23:52:50 -05:00
Bitwarden Unified Self-Host project (#2410)
This commit is contained in:
@ -1,13 +1,11 @@
|
||||
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Bit;
|
||||
namespace Bit.EfShared;
|
||||
|
||||
// This file is a manual addition to a project that it helps, a project that chooses to compile it
|
||||
// should have a projet reference to Core.csproj and a package reference to Microsoft.EntityFrameworkCore.Design
|
||||
// should have a project reference to Core.csproj and a package reference to Microsoft.EntityFrameworkCore.Design
|
||||
// The reason for this is that if it belonged to it's own library you would have to add manual references to the above
|
||||
// and manage the version for the EntityFrameworkCore package. This way it also doesn't create another dll
|
||||
// To include this you can view examples in the MySqlMigrations and PostgresMigrations .csproj files.
|
||||
|
109
util/Migrator/SqlServerDbMigrator.cs
Normal file
109
util/Migrator/SqlServerDbMigrator.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Reflection;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using DbUp;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Migrator;
|
||||
|
||||
public class SqlServerDbMigrator : IDbMigrator
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<SqlServerDbMigrator> _logger;
|
||||
private readonly string _masterConnectionString;
|
||||
|
||||
public SqlServerDbMigrator(GlobalSettings globalSettings, ILogger<SqlServerDbMigrator> logger)
|
||||
{
|
||||
_connectionString = globalSettings.SqlServer.ConnectionString;
|
||||
_logger = logger;
|
||||
_masterConnectionString = new SqlConnectionStringBuilder(_connectionString)
|
||||
{
|
||||
InitialCatalog = "master"
|
||||
}.ConnectionString;
|
||||
}
|
||||
|
||||
public bool MigrateDatabase(bool enableLogging = true,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
|
||||
}
|
||||
|
||||
using (var connection = new SqlConnection(_masterConnectionString))
|
||||
{
|
||||
var databaseName = new SqlConnectionStringBuilder(_connectionString).InitialCatalog;
|
||||
if (string.IsNullOrWhiteSpace(databaseName))
|
||||
{
|
||||
databaseName = "vault";
|
||||
}
|
||||
|
||||
var databaseNameQuoted = new SqlCommandBuilder().QuoteIdentifier(databaseName);
|
||||
var command = new SqlCommand(
|
||||
"IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = @DatabaseName) = 0) " +
|
||||
"CREATE DATABASE " + databaseNameQuoted + ";", connection);
|
||||
command.Parameters.Add("@DatabaseName", SqlDbType.VarChar).Value = databaseName;
|
||||
command.Connection.Open();
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText = "IF ((SELECT DATABASEPROPERTYEX([name], 'IsAutoClose') " +
|
||||
"FROM sys.databases WHERE [name] = @DatabaseName) = 1) " +
|
||||
"ALTER DATABASE " + databaseNameQuoted + " SET AUTO_CLOSE OFF;";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
{
|
||||
// Rename old migration scripts to new namespace.
|
||||
var command = new SqlCommand(
|
||||
"IF OBJECT_ID('Migration','U') IS NOT NULL " +
|
||||
"UPDATE [dbo].[Migration] SET " +
|
||||
"[ScriptName] = REPLACE([ScriptName], 'Bit.Setup.', 'Bit.Migrator.');", connection);
|
||||
command.Connection.Open();
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var builder = DeployChanges.To
|
||||
.SqlDatabase(_connectionString)
|
||||
.JournalToSqlTable("dbo", "Migration")
|
||||
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
|
||||
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
|
||||
.WithTransaction()
|
||||
.WithExecutionTimeout(TimeSpan.FromMinutes(5));
|
||||
|
||||
if (enableLogging)
|
||||
{
|
||||
if (_logger != null)
|
||||
{
|
||||
builder.LogTo(new DbUpLogger(_logger));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.LogToConsole();
|
||||
}
|
||||
}
|
||||
|
||||
var upgrader = builder.Build();
|
||||
var result = upgrader.PerformUpgrade();
|
||||
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
if (result.Successful)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migration successful.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError(Constants.BypassFiltersEventId, result.Error, "Migration failed.");
|
||||
}
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return result.Successful;
|
||||
}
|
||||
}
|
@ -46,6 +46,23 @@
|
||||
"StackExchange.Redis": "2.5.43"
|
||||
}
|
||||
},
|
||||
"AutoMapper": {
|
||||
"type": "Transitive",
|
||||
"resolved": "11.0.0",
|
||||
"contentHash": "+596AnKykYCk9RxXCEF4GYuapSebQtFVvIA1oVG1rrRkCLAC7AkWehJ0brCfYUbdDW3v1H/p0W3hob7JoXGjMw==",
|
||||
"dependencies": {
|
||||
"Microsoft.CSharp": "4.7.0"
|
||||
}
|
||||
},
|
||||
"AutoMapper.Extensions.Microsoft.DependencyInjection": {
|
||||
"type": "Transitive",
|
||||
"resolved": "11.0.0",
|
||||
"contentHash": "0asw5WxdCFh2OTi9Gv+oKyH9SzxwYQSnO8TV5Dd0GggovILzJW4UimP26JAcxc3yB5NnC5urooZ1BBs8ElpiBw==",
|
||||
"dependencies": {
|
||||
"AutoMapper": "11.0.0",
|
||||
"Microsoft.Extensions.Options": "6.0.0"
|
||||
}
|
||||
},
|
||||
"AWSSDK.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "3.7.10.11",
|
||||
@ -246,6 +263,23 @@
|
||||
"Microsoft.NETCore.Platforms": "1.0.1"
|
||||
}
|
||||
},
|
||||
"linq2db": {
|
||||
"type": "Transitive",
|
||||
"resolved": "3.7.0",
|
||||
"contentHash": "iDous2TbSchtALnTLNXQnprmNZF4GrXas0MBz6ZHWkSdilSJjcf26qFM7Qf98Mny0OXHEmNXG/jtIDhoVJ5KmQ==",
|
||||
"dependencies": {
|
||||
"System.ComponentModel.Annotations": "4.7.0"
|
||||
}
|
||||
},
|
||||
"linq2db.EntityFrameworkCore": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.7.1",
|
||||
"contentHash": "Bb25vUDyFw3nKnf7KY+bauwKGD0hdM7/syodS+IgHdWlcbH9g7tHxYmMa9+DNuL0yy6DFvP6Q3BkClm7zbQdAw==",
|
||||
"dependencies": {
|
||||
"Microsoft.EntityFrameworkCore.Relational": "6.0.0",
|
||||
"linq2db": "3.7.0"
|
||||
}
|
||||
},
|
||||
"MailKit": {
|
||||
"type": "Transitive",
|
||||
"resolved": "3.2.0",
|
||||
@ -474,6 +508,39 @@
|
||||
"resolved": "4.0.0",
|
||||
"contentHash": "wtLlRwQX7YoBUYm25xBjJ3UsuLgycme1xXqDn8t3S5kPCWiZrx8uOkyZHLKzH4kkCiQ9m2/J5JeCKNRbZNn3Qg=="
|
||||
},
|
||||
"Microsoft.EntityFrameworkCore": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "gTh3SJsF5WNjEmG32kYc3U4tjeTIv55QOrwHAJcF/xtrIVMteDHMArGC35N0dw86WFY0v8yFkKYKOIOln4jkfQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.EntityFrameworkCore.Abstractions": "6.0.4",
|
||||
"Microsoft.EntityFrameworkCore.Analyzers": "6.0.4",
|
||||
"Microsoft.Extensions.Caching.Memory": "6.0.1",
|
||||
"Microsoft.Extensions.DependencyInjection": "6.0.0",
|
||||
"Microsoft.Extensions.Logging": "6.0.0",
|
||||
"System.Collections.Immutable": "6.0.0",
|
||||
"System.Diagnostics.DiagnosticSource": "6.0.0"
|
||||
}
|
||||
},
|
||||
"Microsoft.EntityFrameworkCore.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "jycTQF0FUJp10cGWBmtsyFhQNeISU9CltDRKCaNiX4QRSEFzgRgaFN4vAFK0T+G5etmXugyddijE4NWCGtgznQ=="
|
||||
},
|
||||
"Microsoft.EntityFrameworkCore.Analyzers": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "t12WodVyGGP2CuLo7R1qwcawHY5zlg+GiQzvkceZpsjcFJVyTFFBFDPg1isBtzurLzWsl+G3z5fVXeic90mPxg=="
|
||||
},
|
||||
"Microsoft.EntityFrameworkCore.Relational": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "E867NbEXYRTElBF5ff+1AN5Awa1jkORy/Rrm0ueibaTAV5uw89LsLoH6yTe+b9urZTWMHtLfGd1RDdNjk8+KzA==",
|
||||
"dependencies": {
|
||||
"Microsoft.EntityFrameworkCore": "6.0.4",
|
||||
"Microsoft.Extensions.Configuration.Abstractions": "6.0.0"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.Caching.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.0",
|
||||
@ -484,13 +551,14 @@
|
||||
},
|
||||
"Microsoft.Extensions.Caching.Memory": {
|
||||
"type": "Transitive",
|
||||
"resolved": "3.1.8",
|
||||
"contentHash": "u04q7+tgc8l6pQ5HOcr6scgapkQQHnrhpGoCaaAZd24R36/NxGsGxuhSmhHOrQx9CsBLe2CVBN/4CkLlxtnnXw==",
|
||||
"resolved": "6.0.1",
|
||||
"contentHash": "B4y+Cev05eMcjf1na0v9gza6GUtahXbtY1JCypIgx3B4Ea/KAgsWyXEmW4q6zMbmTMtKzmPVk09rvFJirvMwTg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.Caching.Abstractions": "3.1.8",
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "3.1.8",
|
||||
"Microsoft.Extensions.Logging.Abstractions": "3.1.8",
|
||||
"Microsoft.Extensions.Options": "3.1.8"
|
||||
"Microsoft.Extensions.Caching.Abstractions": "6.0.0",
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "6.0.0",
|
||||
"Microsoft.Extensions.Logging.Abstractions": "6.0.0",
|
||||
"Microsoft.Extensions.Options": "6.0.0",
|
||||
"Microsoft.Extensions.Primitives": "6.0.0"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.Caching.StackExchangeRedis": {
|
||||
@ -811,6 +879,11 @@
|
||||
"System.Security.Cryptography.Pkcs": "6.0.0"
|
||||
}
|
||||
},
|
||||
"MySqlConnector": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.2",
|
||||
"contentHash": "JVokQTUNN3WHAu9Vw8ieeq1dXTFokJiig5P0VJ4f439UxRrsPo6SaVWC8Zdm6mkPeQFhZ0/9afdWa02EY/1j/w=="
|
||||
},
|
||||
"NETStandard.Library": {
|
||||
"type": "Transitive",
|
||||
"resolved": "1.6.1",
|
||||
@ -867,6 +940,25 @@
|
||||
"resolved": "13.0.1",
|
||||
"contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A=="
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "SJMlOmFHr32oOzVXeHmarGaBKkhi0wHVN/rzuu2tUSJ4Qx2AkHCpr9R/DhLWwDiklqgzFU++9wkFyGJxbx/zzg==",
|
||||
"dependencies": {
|
||||
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
|
||||
}
|
||||
},
|
||||
"Npgsql.EntityFrameworkCore.PostgreSQL": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.4",
|
||||
"contentHash": "fzgRmBd3nAFvKt/L70sJfFWAdobtwDEeOzOzruJq9og97O8/5B96inQOAgOpYyaUjPYpS4ZS5/bxm3vnOJ0+pQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.EntityFrameworkCore": "6.0.4",
|
||||
"Microsoft.EntityFrameworkCore.Abstractions": "6.0.4",
|
||||
"Microsoft.EntityFrameworkCore.Relational": "6.0.4",
|
||||
"Npgsql": "6.0.4"
|
||||
}
|
||||
},
|
||||
"NSec.Cryptography": {
|
||||
"type": "Transitive",
|
||||
"resolved": "20.2.0",
|
||||
@ -889,6 +981,16 @@
|
||||
"System.IO.Pipelines": "5.0.1"
|
||||
}
|
||||
},
|
||||
"Pomelo.EntityFrameworkCore.MySql": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.1",
|
||||
"contentHash": "sFIo5e9RmQoCTEvH6EeSV8ptmX3dw/6XgyD8R93X/i7A9+XCeG9KTjSNjrszVjVOtCu/eyvYqqcv2uZ/BHhlYA==",
|
||||
"dependencies": {
|
||||
"Microsoft.EntityFrameworkCore.Relational": "[6.0.1, 7.0.0)",
|
||||
"Microsoft.Extensions.DependencyInjection": "6.0.0",
|
||||
"MySqlConnector": "2.1.2"
|
||||
}
|
||||
},
|
||||
"Portable.BouncyCastle": {
|
||||
"type": "Transitive",
|
||||
"resolved": "1.9.0",
|
||||
@ -1281,8 +1383,11 @@
|
||||
},
|
||||
"System.Collections.Immutable": {
|
||||
"type": "Transitive",
|
||||
"resolved": "1.7.0",
|
||||
"contentHash": "RVSM6wZUo6L2y6P3vN6gjUtyJ2IF2RVtrepF3J7nrDKfFQd5u/SnSUFclchYQis8/k5scHy9E+fVeKVQLnnkzw=="
|
||||
"resolved": "6.0.0",
|
||||
"contentHash": "l4zZJ1WU2hqpQQHXz1rvC3etVZN+2DLmQMO79FhOTZHMn8tDRr+WU287sbomD0BETlmKDn0ygUgVy9k5xkkJdA==",
|
||||
"dependencies": {
|
||||
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
|
||||
}
|
||||
},
|
||||
"System.Collections.NonGeneric": {
|
||||
"type": "Transitive",
|
||||
@ -1311,6 +1416,11 @@
|
||||
"System.Threading": "4.0.11"
|
||||
}
|
||||
},
|
||||
"System.ComponentModel.Annotations": {
|
||||
"type": "Transitive",
|
||||
"resolved": "4.7.0",
|
||||
"contentHash": "0YFqjhp/mYkDGpU0Ye1GjE53HMp9UVfGN7seGpAMttAC0C40v5gw598jCgpbBLMmCo0E5YRLBv5Z2doypO49ZQ=="
|
||||
},
|
||||
"System.Configuration.ConfigurationManager": {
|
||||
"type": "Transitive",
|
||||
"resolved": "6.0.0",
|
||||
@ -2579,41 +2689,52 @@
|
||||
"core": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"AWSSDK.SQS": "3.7.2.47",
|
||||
"AWSSDK.SimpleEmail": "3.7.0.150",
|
||||
"AspNetCoreRateLimit": "4.0.2",
|
||||
"AspNetCoreRateLimit.Redis": "1.0.1",
|
||||
"Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.2.1",
|
||||
"Azure.Storage.Blobs": "12.11.0",
|
||||
"Azure.Storage.Queues": "12.9.0",
|
||||
"BitPay.Light": "1.0.1907",
|
||||
"Braintree": "5.12.0",
|
||||
"Fido2.AspNet": "3.0.0-beta2",
|
||||
"Handlebars.Net": "2.1.2",
|
||||
"IdentityServer4": "4.1.2",
|
||||
"IdentityServer4.AccessTokenValidation": "3.0.1",
|
||||
"MailKit": "3.2.0",
|
||||
"Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4",
|
||||
"Microsoft.Azure.Cosmos.Table": "1.0.8",
|
||||
"Microsoft.Azure.NotificationHubs": "4.1.0",
|
||||
"Microsoft.Azure.ServiceBus": "5.2.0",
|
||||
"Microsoft.Data.SqlClient": "4.1.0",
|
||||
"Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6",
|
||||
"Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1",
|
||||
"Microsoft.Extensions.Configuration.UserSecrets": "6.0.1",
|
||||
"Microsoft.Extensions.Identity.Stores": "6.0.4",
|
||||
"Newtonsoft.Json": "13.0.1",
|
||||
"Otp.NET": "1.2.2",
|
||||
"Quartz": "3.4.0",
|
||||
"SendGrid": "9.27.0",
|
||||
"Sentry.Serilog": "3.16.0",
|
||||
"Serilog.AspNetCore": "5.0.0",
|
||||
"Serilog.Extensions.Logging": "3.1.0",
|
||||
"Serilog.Extensions.Logging.File": "2.0.0",
|
||||
"Serilog.Sinks.AzureCosmosDB": "2.0.0",
|
||||
"Serilog.Sinks.SyslogMessages": "2.0.6",
|
||||
"Stripe.net": "40.0.0",
|
||||
"YubicoDotNetClient": "1.2.0"
|
||||
"AWSSDK.SQS": "[3.7.2.47, )",
|
||||
"AWSSDK.SimpleEmail": "[3.7.0.150, )",
|
||||
"AspNetCoreRateLimit": "[4.0.2, )",
|
||||
"AspNetCoreRateLimit.Redis": "[1.0.1, )",
|
||||
"Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.2.1, )",
|
||||
"Azure.Storage.Blobs": "[12.11.0, )",
|
||||
"Azure.Storage.Queues": "[12.9.0, )",
|
||||
"BitPay.Light": "[1.0.1907, )",
|
||||
"Braintree": "[5.12.0, )",
|
||||
"Fido2.AspNet": "[3.0.0-beta2, )",
|
||||
"Handlebars.Net": "[2.1.2, )",
|
||||
"IdentityServer4": "[4.1.2, )",
|
||||
"IdentityServer4.AccessTokenValidation": "[3.0.1, )",
|
||||
"MailKit": "[3.2.0, )",
|
||||
"Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )",
|
||||
"Microsoft.Azure.Cosmos.Table": "[1.0.8, )",
|
||||
"Microsoft.Azure.NotificationHubs": "[4.1.0, )",
|
||||
"Microsoft.Azure.ServiceBus": "[5.2.0, )",
|
||||
"Microsoft.Data.SqlClient": "[4.1.0, )",
|
||||
"Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )",
|
||||
"Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )",
|
||||
"Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )",
|
||||
"Microsoft.Extensions.Identity.Stores": "[6.0.4, )",
|
||||
"Newtonsoft.Json": "[13.0.1, )",
|
||||
"Otp.NET": "[1.2.2, )",
|
||||
"Quartz": "[3.4.0, )",
|
||||
"SendGrid": "[9.27.0, )",
|
||||
"Sentry.Serilog": "[3.16.0, )",
|
||||
"Serilog.AspNetCore": "[5.0.0, )",
|
||||
"Serilog.Extensions.Logging": "[3.1.0, )",
|
||||
"Serilog.Extensions.Logging.File": "[2.0.0, )",
|
||||
"Serilog.Sinks.AzureCosmosDB": "[2.0.0, )",
|
||||
"Serilog.Sinks.SyslogMessages": "[2.0.6, )",
|
||||
"Stripe.net": "[40.0.0, )",
|
||||
"YubicoDotNetClient": "[1.2.0, )"
|
||||
}
|
||||
},
|
||||
"infrastructure.entityframework": {
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"AutoMapper.Extensions.Microsoft.DependencyInjection": "[11.0.0, )",
|
||||
"Core": "[2022.10.0, )",
|
||||
"Microsoft.EntityFrameworkCore.Relational": "[6.0.4, )",
|
||||
"Npgsql.EntityFrameworkCore.PostgreSQL": "[6.0.4, )",
|
||||
"Pomelo.EntityFrameworkCore.MySql": "[6.0.1, )",
|
||||
"linq2db.EntityFrameworkCore": "[6.7.1, )"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,14 +4,15 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MySqlMigrations;
|
||||
namespace Bit.MySqlMigrations;
|
||||
|
||||
public static class GlobalSettingsFactory
|
||||
{
|
||||
public static GlobalSettings GlobalSettings { get; } = new GlobalSettings();
|
||||
static GlobalSettingsFactory()
|
||||
{
|
||||
var configBuilder = new ConfigurationBuilder().AddUserSecrets<Bit.Api.Startup>();
|
||||
// UserSecretsId here should match what is in Api.csproj
|
||||
var configBuilder = new ConfigurationBuilder().AddUserSecrets("bitwarden-Api");
|
||||
var Configuration = configBuilder.Build();
|
||||
ConfigurationBinder.Bind(Configuration.GetSection("GlobalSettings"), GlobalSettings);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Bit.EfShared;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Bit.MySqlMigrations.Migrations;
|
||||
|
||||
|
41
util/MySqlMigrations/MySqlDbMigrator.cs
Normal file
41
util/MySqlMigrations/MySqlDbMigrator.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using Bit.Core;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.MySqlMigrations;
|
||||
|
||||
public class MySqlDbMigrator : IDbMigrator
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly ILogger<MySqlDbMigrator> _logger;
|
||||
|
||||
public MySqlDbMigrator(IServiceScopeFactory serviceScopeFactory, ILogger<MySqlDbMigrator> logger)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool MigrateDatabase(bool enableLogging = true,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
|
||||
}
|
||||
|
||||
using var scope = _serviceScopeFactory.CreateScope();
|
||||
var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
databaseContext.Database.Migrate();
|
||||
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migration successful.");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<UserSecretsId>9f1cd3e0-70f2-4921-8068-b2538fd7c3f7</UserSecretsId>
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Core\Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\Api\Api.csproj" />
|
||||
<ProjectReference Include="..\..\src\Infrastructure.EntityFramework\Infrastructure.EntityFramework.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -4,14 +4,15 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace MySqlMigrations;
|
||||
namespace Bit.PostgresMigrations;
|
||||
|
||||
public static class GlobalSettingsFactory
|
||||
{
|
||||
public static GlobalSettings GlobalSettings { get; } = new GlobalSettings();
|
||||
static GlobalSettingsFactory()
|
||||
{
|
||||
var configBuilder = new ConfigurationBuilder().AddUserSecrets<Bit.Api.Startup>();
|
||||
// UserSecretsId here should match what is in Api.csproj
|
||||
var configBuilder = new ConfigurationBuilder().AddUserSecrets("bitwarden-Api");
|
||||
var Configuration = configBuilder.Build();
|
||||
ConfigurationBinder.Bind(Configuration.GetSection("GlobalSettings"), GlobalSettings);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Bit.EfShared;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Bit.PostgresMigrations.Migrations;
|
||||
|
||||
|
41
util/PostgresMigrations/PostgresDbMigratorDbMigrator.cs
Normal file
41
util/PostgresMigrations/PostgresDbMigratorDbMigrator.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using Bit.Core;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.PostgresMigrations;
|
||||
|
||||
public class PostgresDbMigrator : IDbMigrator
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly ILogger<PostgresDbMigrator> _logger;
|
||||
|
||||
public PostgresDbMigrator(IServiceScopeFactory serviceScopeFactory, ILogger<PostgresDbMigrator> logger)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool MigrateDatabase(bool enableLogging = true,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migrating database.");
|
||||
}
|
||||
|
||||
using var scope = _serviceScopeFactory.CreateScope();
|
||||
var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
databaseContext.Database.Migrate();
|
||||
|
||||
if (enableLogging && _logger != null)
|
||||
{
|
||||
_logger.LogInformation(Constants.BypassFiltersEventId, "Migration successful.");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return true;
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Core\Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\Api\Api.csproj" />
|
||||
<ProjectReference Include="..\..\src\Infrastructure.EntityFramework\Infrastructure.EntityFramework.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Reference in New Issue
Block a user