mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +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>
132 lines
4.9 KiB
C#
132 lines
4.9 KiB
C#
using System.Reflection;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Settings;
|
|
using Bit.Infrastructure.Dapper;
|
|
using Bit.Infrastructure.EntityFramework;
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
using Bit.Infrastructure.IntegrationTest.Services;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using Xunit.Sdk;
|
|
|
|
namespace Bit.Infrastructure.IntegrationTest;
|
|
|
|
public class DatabaseDataAttribute : DataAttribute
|
|
{
|
|
public bool SelfHosted { get; set; }
|
|
public bool UseFakeTimeProvider { get; set; }
|
|
public string? MigrationName { get; set; }
|
|
|
|
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
|
|
{
|
|
var parameters = testMethod.GetParameters();
|
|
|
|
var config = DatabaseTheoryAttribute.GetConfiguration();
|
|
|
|
var serviceProviders = GetDatabaseProviders(config);
|
|
|
|
foreach (var provider in serviceProviders)
|
|
{
|
|
var objects = new object[parameters.Length];
|
|
for (var i = 0; i < parameters.Length; i++)
|
|
{
|
|
objects[i] = provider.GetRequiredService(parameters[i].ParameterType);
|
|
}
|
|
yield return objects;
|
|
}
|
|
}
|
|
|
|
protected virtual IEnumerable<IServiceProvider> GetDatabaseProviders(IConfiguration config)
|
|
{
|
|
var configureLogging = (ILoggingBuilder builder) =>
|
|
{
|
|
if (!config.GetValue<bool>("Quiet"))
|
|
{
|
|
builder.AddConfiguration(config);
|
|
builder.AddConsole();
|
|
builder.AddDebug();
|
|
}
|
|
};
|
|
|
|
var databases = config.GetDatabases();
|
|
|
|
foreach (var database in databases)
|
|
{
|
|
if (database.Type == SupportedDatabaseProviders.SqlServer && !database.UseEf)
|
|
{
|
|
var dapperSqlServerCollection = new ServiceCollection();
|
|
AddCommonServices(dapperSqlServerCollection, configureLogging);
|
|
dapperSqlServerCollection.AddDapperRepositories(SelfHosted);
|
|
var globalSettings = new GlobalSettings
|
|
{
|
|
DatabaseProvider = "sqlServer",
|
|
SqlServer = new GlobalSettings.SqlSettings
|
|
{
|
|
ConnectionString = database.ConnectionString,
|
|
},
|
|
};
|
|
dapperSqlServerCollection.AddSingleton(globalSettings);
|
|
dapperSqlServerCollection.AddSingleton<IGlobalSettings>(globalSettings);
|
|
dapperSqlServerCollection.AddSingleton(database);
|
|
dapperSqlServerCollection.AddDistributedSqlServerCache((o) =>
|
|
{
|
|
o.ConnectionString = database.ConnectionString;
|
|
o.SchemaName = "dbo";
|
|
o.TableName = "Cache";
|
|
});
|
|
|
|
if (!string.IsNullOrEmpty(MigrationName))
|
|
{
|
|
AddSqlMigrationTester(dapperSqlServerCollection, database.ConnectionString, MigrationName);
|
|
}
|
|
|
|
yield return dapperSqlServerCollection.BuildServiceProvider();
|
|
}
|
|
else
|
|
{
|
|
var efCollection = new ServiceCollection();
|
|
AddCommonServices(efCollection, configureLogging);
|
|
efCollection.SetupEntityFramework(database.ConnectionString, database.Type);
|
|
efCollection.AddPasswordManagerEFRepositories(SelfHosted);
|
|
efCollection.AddSingleton(database);
|
|
efCollection.AddSingleton<IDistributedCache, EntityFrameworkCache>();
|
|
|
|
if (!string.IsNullOrEmpty(MigrationName))
|
|
{
|
|
AddEfMigrationTester(efCollection, database.Type, MigrationName);
|
|
}
|
|
|
|
yield return efCollection.BuildServiceProvider();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddCommonServices(IServiceCollection services, Action<ILoggingBuilder> configureLogging)
|
|
{
|
|
services.AddLogging(configureLogging);
|
|
services.AddDataProtection();
|
|
|
|
if (UseFakeTimeProvider)
|
|
{
|
|
services.AddSingleton<TimeProvider, FakeTimeProvider>();
|
|
}
|
|
}
|
|
|
|
private void AddSqlMigrationTester(IServiceCollection services, string connectionString, string migrationName)
|
|
{
|
|
services.AddSingleton<IMigrationTesterService, SqlMigrationTesterService>(sp => new SqlMigrationTesterService(connectionString, migrationName));
|
|
}
|
|
|
|
private void AddEfMigrationTester(IServiceCollection services, SupportedDatabaseProviders databaseType, string migrationName)
|
|
{
|
|
services.AddSingleton<IMigrationTesterService, EfMigrationTesterService>(sp =>
|
|
{
|
|
var dbContext = sp.GetRequiredService<DatabaseContext>();
|
|
return new EfMigrationTesterService(dbContext, databaseType, migrationName);
|
|
});
|
|
}
|
|
}
|