1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-30 13:33:24 +01:00
bitwarden-server/util/PostgresMigrations/Factories.cs
Justin Baur 61a0efbdfc
[PM-2444] Add Pipeline for Testing All Database Variants in CI (#2471)
* Add Pipeline

* Fix Lint

* Added a Change

* Update Pipeline

* Add Multi-Version Support

* Use Profile Switch for each profile

* Fix MySql

* Debug MySql

* Use Proper Seperator

* Add Allow User Variables=true

* Pipeline Work

* Expand Config for Postgres

* Change Config Key

* Add Debug Step

* Fix Debug Step

* Fix Tests

* Add Sleep

* Fix Tests

* Fix SQL Server Tests

* Add Sqlite

* Use Context Property

* Fix Tests

* Fix Test Logger

* Update AccountRevisionDate Check

* Fix Postgres Time Issues

* Formatting and Pipeline Update

* Remove Unneeded SqlServer Setting

* Update .github/workflows/infrastructure-tests.yml

Co-authored-by: mimartin12 <77340197+mimartin12@users.noreply.github.com>

---------

Co-authored-by: mimartin12 <77340197+mimartin12@users.noreply.github.com>
2023-05-30 13:25:55 -04:00

50 lines
1.7 KiB
C#

using Bit.Core.Settings;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.PostgresMigrations;
public class GlobalSettingsFactory
{
public GlobalSettings GlobalSettings { get; }
public GlobalSettingsFactory(string[] args)
{
GlobalSettings = new GlobalSettings();
// UserSecretsId here should match what is in Api.csproj
var config = new ConfigurationBuilder()
.AddUserSecrets("bitwarden-Api")
.AddCommandLine(args)
.Build();
config.GetSection("GlobalSettings").Bind(GlobalSettings);
}
}
public class DatabaseContextFactory : IDesignTimeDbContextFactory<DatabaseContext>
{
public DatabaseContext CreateDbContext(string[] args)
{
var services = new ServiceCollection();
services.AddDataProtection();
var serviceProvider = services.BuildServiceProvider();
var globalSettings = new GlobalSettingsFactory(args)
.GlobalSettings;
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
var connectionString = globalSettings.PostgreSql?.ConnectionString;
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new Exception("No Postgres connection string found.");
}
optionsBuilder.UseNpgsql(
connectionString,
b => b.MigrationsAssembly("PostgresMigrations"))
.UseApplicationServiceProvider(serviceProvider);
return new DatabaseContext(optionsBuilder.Options);
}
}