mirror of
https://github.com/bitwarden/server.git
synced 2024-11-24 12:35:25 +01:00
f5caecc6d6
* Add SQL script to migrate custom users with specific permissions to User type Remove 'editAssignedCollections' and 'deleteAssignedCollections' properties from Permissions in OrganizationUser table. Migrate custom users who only have these permissions to the User type. * Add MySQL migration to migrate custom users with specific permissions to User type * Add Postgres migration to migrate custom users with specific permissions to User type * Add Sqlite migration to migrate custom users with specific permissions to User type * Update AutoFixture usage in tests to resolve creating ILogger mock instances * Update EF integration tests database contexts to use each respective Migrations assembly. Configure Sqlite instance * Add RunMigration method to BaseEntityFrameworkRepository * Add FinalFlexibleCollectionsDataMigrationsTests * Improve data migration efficiency by using OPENJSON instead of multiple JSON_EXTRACT * Add batching to the sql data migrations * Update DbMigrator to run a specific script based on its name * Update DatabaseDataAttribute to be able to test a specific migration * Add reference to the migration projects to Infrastructure.IntegrationTest * Add integration test to test the migration FinalFlexibleCollectionsDataMigrations * Remove EFIntegration tests and remove RunMigration method from BaseEntityFrameworkRepository * Add IMigrationTesterService and implementations for SQL and EF migrations * Add FinalFlexibleCollectionsDataMigrationsTests and remove test from OrganizationUserRepositoryTests * Update sql data migration script based on performance feedback * Bump date on EF migration scripts * Add xmldoc comments to IMigrationTesterService and each implementation * Bump up the date on the EF migration scripts * Bump up dates on EF migrations * Added tests to assert no unwanted changes are made to the permissions json. Refactor tests. * Revert changes made to DbMigrator and refactor SqlMigrationTesterService to not use it. * Add method description * Fix test to assert no changes are made to custom user * Remove unnecessary COALESCE and SELECT CASE * Unident lines on SQL script * Update DatabaseDataAttribute MigrationName property to be nullable * Fix null reference checks * Remove unnecessary COALESCE from Postgres script * Bump dates on migration scripts * Bump up dates on EF migrations * Add migration tests for handling null * Add test for non json values * Fix test * Remove migrations * Recreate EF migrations * Update Postgres data migration script to check for valid JSON in Permissions column --------- Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
71 lines
3.1 KiB
C#
71 lines
3.1 KiB
C#
using System.Text;
|
|
using Bit.Core;
|
|
using Bit.Core.Test.Helpers.Factories;
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NSubstitute;
|
|
|
|
namespace Bit.Infrastructure.EFIntegration.Test.Helpers;
|
|
|
|
public static class DatabaseOptionsFactory
|
|
{
|
|
public static List<DbContextOptions<DatabaseContext>> Options { get; } = new();
|
|
|
|
static DatabaseOptionsFactory()
|
|
{
|
|
var services = new ServiceCollection()
|
|
.AddSingleton(sp =>
|
|
{
|
|
var dataProtector = Substitute.For<IDataProtector>();
|
|
dataProtector.Unprotect(Arg.Any<byte[]>())
|
|
.Returns<byte[]>(data =>
|
|
Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix +
|
|
Encoding.UTF8.GetString((byte[])data[0])));
|
|
|
|
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
|
|
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
|
|
.Returns(dataProtector);
|
|
|
|
return dataProtectionProvider;
|
|
})
|
|
.BuildServiceProvider();
|
|
|
|
var globalSettings = GlobalSettingsFactory.GlobalSettings;
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.PostgreSql?.ConnectionString))
|
|
{
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseNpgsql(globalSettings.PostgreSql.ConnectionString, npgsqlDbContextOptionsBuilder =>
|
|
{
|
|
npgsqlDbContextOptionsBuilder.MigrationsAssembly(nameof(PostgresMigrations));
|
|
})
|
|
.UseApplicationServiceProvider(services)
|
|
.Options);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.MySql?.ConnectionString))
|
|
{
|
|
var mySqlConnectionString = globalSettings.MySql.ConnectionString;
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseMySql(mySqlConnectionString, ServerVersion.AutoDetect(mySqlConnectionString), mySqlDbContextOptionsBuilder =>
|
|
{
|
|
mySqlDbContextOptionsBuilder.MigrationsAssembly(nameof(MySqlMigrations));
|
|
})
|
|
.UseApplicationServiceProvider(services)
|
|
.Options);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.Sqlite?.ConnectionString))
|
|
{
|
|
var sqliteConnectionString = globalSettings.Sqlite.ConnectionString;
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseSqlite(sqliteConnectionString, mySqlDbContextOptionsBuilder =>
|
|
{
|
|
mySqlDbContextOptionsBuilder.MigrationsAssembly(nameof(SqliteMigrations));
|
|
})
|
|
.UseApplicationServiceProvider(services)
|
|
.Options);
|
|
}
|
|
}
|
|
}
|