Skip to content

Commit

Permalink
Refactor database projects to support multiple database types (#237)
Browse files Browse the repository at this point in the history
* gh-235 Refactor sqlite support to new projects
* Set DB type for integration test

Signed-off-by: Victor Chang <vicchang@nvidia.com>
  • Loading branch information
mocsharp authored Nov 8, 2022
1 parent c83c50a commit 697c7fc
Show file tree
Hide file tree
Showing 76 changed files with 1,698 additions and 1,415 deletions.
2 changes: 1 addition & 1 deletion .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ header:
- 'src/.sonarlint/**'
- 'src/coverlet.runsettings'
- 'src/Monai.Deploy.InformaticsGateway.sln'
- 'src/Database/Migrations/**'
- 'src/Database/EntityFramework/Migrations/**'
- 'demos/**/.env/**'
- 'demos/**/*.txt'
- 'doc/dependency_decisions.yml'
Expand Down
2,562 changes: 1,281 additions & 1,281 deletions docs/compliance/third-party-licenses.md

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions src/Configuration/InformaticsGatewayConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ namespace Monai.Deploy.InformaticsGateway.Configuration
/// </summary>
public class InformaticsGatewayConfiguration
{
/// <summary>
/// Name of the key for retrieve database connection string.
/// </summary>
public const string DatabaseConnectionStringKey = "InformaticsGatewayDatabase";

/// <summary>
/// Represents the <c>dicom</c> section of the configuration file.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright 2021-2022 MONAI Consortium
* Copyright 2019-2021 NVIDIA Corporation
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +14,12 @@
* limitations under the License.
*/

namespace Monai.Deploy.InformaticsGateway.Repositories
using Microsoft.Extensions.Hosting;

namespace Monai.Deploy.InformaticsGateway.Database.Api
{
public class StorageObjectsinferenceRequest
public interface IDatabaseMigrationManager
{
IHost Migrate(IHost host);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Monai.Deploy.InformaticsGateway.Repositories
namespace Monai.Deploy.InformaticsGateway.Database.Api
{
public interface IInformaticsGatewayRepository<T> where T : class
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
~ Copyright 2021-2022 MONAI Consortium
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Monai.Deploy.InformaticsGateway.Database.Repositories</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Test\**" />
<EmbeddedResource Remove="Test\**" />
<None Remove="Test\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Ardalis.GuardClauses;
using Monai.Deploy.InformaticsGateway.Api.Storage;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.Api
{
public class StorageMetadataWrapper
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public void Update(FileStorageMetadata metadata)

public FileStorageMetadata GetObject()
{
var type = System.Type.GetType(TypeName, true);
var type = Type.GetType(TypeName, true);
return JsonSerializer.Deserialize(Value, type) as FileStorageMetadata;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
<ProjectReference Include="..\Monai.Deploy.InformaticsGateway.Database.csproj" />
<ProjectReference Include="..\..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
<ProjectReference Include="..\Monai.Deploy.InformaticsGateway.Database.Api.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

using Monai.Deploy.InformaticsGateway.Api.Rest;
using Monai.Deploy.InformaticsGateway.Api.Storage;
using Monai.Deploy.InformaticsGateway.Database.Api;

namespace Monai.Deploy.InformaticsGateway.Database.Test
{
Expand All @@ -27,7 +29,7 @@ public void GivenAFhirFileStorageMetadataObject_WhenInitializedWithStorageMetada
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Api.Rest.FhirStorageFormat.Json);
FhirStorageFormat.Json);
metadata.SetWorkflows("A", "B", "C");

var wrapper = new StorageMetadataWrapper(metadata);
Expand Down
File renamed without changes.
52 changes: 52 additions & 0 deletions src/Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Monai.Deploy.InformaticsGateway.Configuration;
using Monai.Deploy.InformaticsGateway.Database.Api;
using Monai.Deploy.InformaticsGateway.Database.EntityFramework;
using Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations;

namespace Monai.Deploy.InformaticsGateway.Database
{
public static class DatabaseManager
{
public static IServiceCollection ConfigureDatabase(this IServiceCollection services, IConfigurationSection? connectionStringConfigurationSection)
{
if (connectionStringConfigurationSection is null)
{
throw new ConfigurationException("No database connections found in configuration section 'ConnectionStrings'.");
}


var databaseType = connectionStringConfigurationSection["Type"];
switch (databaseType)
{
case "Sqlite":
services.AddScoped<IDatabaseMigrationManager, EfDatabaseMigrationManager>();
services.AddScoped(typeof(IInformaticsGatewayRepository<>), typeof(InformaticsGatewayRepository<>));
services.AddDbContext<InformaticsGatewayContext>(
options => options.UseSqlite(connectionStringConfigurationSection[SR.DatabaseConnectionStringKey]),
ServiceLifetime.Transient);
return services;
default:
throw new ConfigurationException($"Unsupported database type defined: '{databaseType}'");
}
}
}
}
35 changes: 35 additions & 0 deletions src/Database/DatabaseMigrationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Monai.Deploy.InformaticsGateway.Database.Api;

namespace Monai.Deploy.InformaticsGateway.Database
{
public static class DatabaseMigrationManager
{
public static IHost MigrateDatabase(this IHost host)
{
using (var scope = host.Services.CreateScope())
{
scope.ServiceProvider.GetRequiredService<IDatabaseMigrationManager>()?.Migrate(host);
}
return host;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monai.Deploy.InformaticsGateway.Api;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class DestinationApplicationEntityConfiguration : IEntityTypeConfiguration<DestinationApplicationEntity>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monai.Deploy.InformaticsGateway.Api.Rest;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class InferenceRequestConfiguration : IEntityTypeConfiguration<InferenceRequest>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monai.Deploy.InformaticsGateway.Api;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class MonaiApplicationEntityConfiguration : IEntityTypeConfiguration<MonaiApplicationEntity>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monai.Deploy.InformaticsGateway.Api.Storage;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class PayloadConfiguration : IEntityTypeConfiguration<Payload>
{
Expand Down
28 changes: 28 additions & 0 deletions src/Database/EntityFramework/Configuration/SR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021-2022 MONAI Consortium
* Copyright 2019-2021 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
public static class SR
{
/// <summary>
/// Name of the key for retrieve database connection string.
/// </summary>
public const string DatabaseConnectionStringKey = "InformaticsGatewayDatabase";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using Microsoft.EntityFrameworkCore;
using Monai.Deploy.InformaticsGateway.Api;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class SourceApplicationEntityConfiguration : IEntityTypeConfiguration<SourceApplicationEntity>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Monai.Deploy.InformaticsGateway.Database.Api;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
{
internal class StorageMetadataWrapperEntityConfiguration : IEntityTypeConfiguration<StorageMetadataWrapper>
{
Expand Down
48 changes: 48 additions & 0 deletions src/Database/EntityFramework/EfDatabaseMigrationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 MONAI Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Monai.Deploy.InformaticsGateway.Database.Api;

namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework
{
public class EfDatabaseMigrationManager : IDatabaseMigrationManager
{
public IHost Migrate(IHost host)
{
using (var scope = host.Services.CreateScope())
{
using (var dbContext = scope.ServiceProvider.GetRequiredService<InformaticsGatewayContext>())
{
try
{
dbContext.Database.Migrate();
}
catch (Exception ex)
{
var logger = scope.ServiceProvider.GetService<ILogger>();
logger?.Log(LogLevel.Critical, "Failed to migrate database", ex);
throw;
}
}
}
return host;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using Monai.Deploy.InformaticsGateway.Api;
using Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations;

namespace Monai.Deploy.InformaticsGateway.Database
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework
{
public class InformaticsGatewayContext : DbContext
{
Expand Down
Loading

0 comments on commit 697c7fc

Please sign in to comment.