Skip to content

Commit 697c7fc

Browse files
authored
Refactor database projects to support multiple database types (#237)
* gh-235 Refactor sqlite support to new projects * Set DB type for integration test Signed-off-by: Victor Chang <[email protected]>
1 parent c83c50a commit 697c7fc

File tree

76 files changed

+1698
-1415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1698
-1415
lines changed

.licenserc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ header:
3030
- 'src/.sonarlint/**'
3131
- 'src/coverlet.runsettings'
3232
- 'src/Monai.Deploy.InformaticsGateway.sln'
33-
- 'src/Database/Migrations/**'
33+
- 'src/Database/EntityFramework/Migrations/**'
3434
- 'demos/**/.env/**'
3535
- 'demos/**/*.txt'
3636
- 'doc/dependency_decisions.yml'

docs/compliance/third-party-licenses.md

Lines changed: 1281 additions & 1281 deletions
Large diffs are not rendered by default.

src/Configuration/InformaticsGatewayConfiguration.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ namespace Monai.Deploy.InformaticsGateway.Configuration
2424
/// </summary>
2525
public class InformaticsGatewayConfiguration
2626
{
27-
/// <summary>
28-
/// Name of the key for retrieve database connection string.
29-
/// </summary>
30-
public const string DatabaseConnectionStringKey = "InformaticsGatewayDatabase";
31-
3227
/// <summary>
3328
/// Represents the <c>dicom</c> section of the configuration file.
3429
/// </summary>

src/InformaticsGateway/Repositories/StorageObjectsinferenceRequest.cs renamed to src/Database/Api/IDatabaseMigrationManager.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
* Copyright 2021-2022 MONAI Consortium
3-
* Copyright 2019-2021 NVIDIA Corporation
2+
* Copyright 2022 MONAI Consortium
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -15,9 +14,12 @@
1514
* limitations under the License.
1615
*/
1716

18-
namespace Monai.Deploy.InformaticsGateway.Repositories
17+
using Microsoft.Extensions.Hosting;
18+
19+
namespace Monai.Deploy.InformaticsGateway.Database.Api
1920
{
20-
public class StorageObjectsinferenceRequest
21+
public interface IDatabaseMigrationManager
2122
{
23+
IHost Migrate(IHost host);
2224
}
23-
}
25+
}

src/InformaticsGateway/Repositories/IInformaticsGatewayRepository.cs renamed to src/Database/Api/IInformaticsGatewayRepository.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
using System;
18-
using System.Collections.Generic;
19-
using System.Linq;
20-
using System.Threading;
21-
using System.Threading.Tasks;
2217
using Microsoft.EntityFrameworkCore.ChangeTracking;
2318

24-
namespace Monai.Deploy.InformaticsGateway.Repositories
19+
namespace Monai.Deploy.InformaticsGateway.Database.Api
2520
{
2621
public interface IInformaticsGatewayRepository<T> where T : class
2722
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!--
2+
~ Copyright 2021-2022 MONAI Consortium
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<Project Sdk="Microsoft.NET.Sdk">
18+
19+
<PropertyGroup>
20+
<RootNamespace>Monai.Deploy.InformaticsGateway.Database.Repositories</RootNamespace>
21+
<TargetFramework>net6.0</TargetFramework>
22+
<ImplicitUsings>enable</ImplicitUsings>
23+
<Nullable>enable</Nullable>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<Compile Remove="Test\**" />
28+
<EmbeddedResource Remove="Test\**" />
29+
<None Remove="Test\**" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<ProjectReference Include="..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
38+
</ItemGroup>
39+
40+
</Project>

src/Database/StorageMetadataWrapper.cs renamed to src/Database/Api/StorageMetadataWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using Ardalis.GuardClauses;
2020
using Monai.Deploy.InformaticsGateway.Api.Storage;
2121

22-
namespace Monai.Deploy.InformaticsGateway.Database
22+
namespace Monai.Deploy.InformaticsGateway.Database.Api
2323
{
2424
public class StorageMetadataWrapper
2525
{
@@ -61,7 +61,7 @@ public void Update(FileStorageMetadata metadata)
6161

6262
public FileStorageMetadata GetObject()
6363
{
64-
var type = System.Type.GetType(TypeName, true);
64+
var type = Type.GetType(TypeName, true);
6565
return JsonSerializer.Deserialize(Value, type) as FileStorageMetadata;
6666
}
6767
}

src/Database/Test/Monai.Deploy.InformaticsGateway.Database.Test.csproj renamed to src/Database/Api/Test/Monai.Deploy.InformaticsGateway.Database.Api.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41-
<ProjectReference Include="..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
42-
<ProjectReference Include="..\Monai.Deploy.InformaticsGateway.Database.csproj" />
41+
<ProjectReference Include="..\..\..\Api\Monai.Deploy.InformaticsGateway.Api.csproj" />
42+
<ProjectReference Include="..\Monai.Deploy.InformaticsGateway.Database.Api.csproj" />
4343
</ItemGroup>
4444

4545
</Project>

src/Database/Test/StorageMetadataWrapperTest.cs renamed to src/Database/Api/Test/StorageMetadataWrapperTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
using Monai.Deploy.InformaticsGateway.Api.Rest;
1718
using Monai.Deploy.InformaticsGateway.Api.Storage;
19+
using Monai.Deploy.InformaticsGateway.Database.Api;
1820

1921
namespace Monai.Deploy.InformaticsGateway.Database.Test
2022
{
@@ -27,7 +29,7 @@ public void GivenAFhirFileStorageMetadataObject_WhenInitializedWithStorageMetada
2729
Guid.NewGuid().ToString(),
2830
Guid.NewGuid().ToString(),
2931
Guid.NewGuid().ToString(),
30-
Api.Rest.FhirStorageFormat.Json);
32+
FhirStorageFormat.Json);
3133
metadata.SetWorkflows("A", "B", "C");
3234

3335
var wrapper = new StorageMetadataWrapper(metadata);
File renamed without changes.

src/Database/DatabaseManager.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2022 MONAI Consortium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Microsoft.EntityFrameworkCore;
18+
using Microsoft.Extensions.Configuration;
19+
using Microsoft.Extensions.DependencyInjection;
20+
using Monai.Deploy.InformaticsGateway.Configuration;
21+
using Monai.Deploy.InformaticsGateway.Database.Api;
22+
using Monai.Deploy.InformaticsGateway.Database.EntityFramework;
23+
using Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations;
24+
25+
namespace Monai.Deploy.InformaticsGateway.Database
26+
{
27+
public static class DatabaseManager
28+
{
29+
public static IServiceCollection ConfigureDatabase(this IServiceCollection services, IConfigurationSection? connectionStringConfigurationSection)
30+
{
31+
if (connectionStringConfigurationSection is null)
32+
{
33+
throw new ConfigurationException("No database connections found in configuration section 'ConnectionStrings'.");
34+
}
35+
36+
37+
var databaseType = connectionStringConfigurationSection["Type"];
38+
switch (databaseType)
39+
{
40+
case "Sqlite":
41+
services.AddScoped<IDatabaseMigrationManager, EfDatabaseMigrationManager>();
42+
services.AddScoped(typeof(IInformaticsGatewayRepository<>), typeof(InformaticsGatewayRepository<>));
43+
services.AddDbContext<InformaticsGatewayContext>(
44+
options => options.UseSqlite(connectionStringConfigurationSection[SR.DatabaseConnectionStringKey]),
45+
ServiceLifetime.Transient);
46+
return services;
47+
default:
48+
throw new ConfigurationException($"Unsupported database type defined: '{databaseType}'");
49+
}
50+
}
51+
}
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2022 MONAI Consortium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Microsoft.Extensions.DependencyInjection;
18+
using Microsoft.Extensions.Hosting;
19+
using Monai.Deploy.InformaticsGateway.Database.Api;
20+
21+
namespace Monai.Deploy.InformaticsGateway.Database
22+
{
23+
public static class DatabaseMigrationManager
24+
{
25+
public static IHost MigrateDatabase(this IHost host)
26+
{
27+
using (var scope = host.Services.CreateScope())
28+
{
29+
scope.ServiceProvider.GetRequiredService<IDatabaseMigrationManager>()?.Migrate(host);
30+
}
31+
return host;
32+
}
33+
}
34+
}
35+

src/Database/DestinationApplicationEntityConfiguration.cs renamed to src/Database/EntityFramework/Configuration/DestinationApplicationEntityConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2020
using Monai.Deploy.InformaticsGateway.Api;
2121

22-
namespace Monai.Deploy.InformaticsGateway.Database
22+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2323
{
2424
internal class DestinationApplicationEntityConfiguration : IEntityTypeConfiguration<DestinationApplicationEntity>
2525
{

src/Database/InferenceRequestConfiguration.cs renamed to src/Database/EntityFramework/Configuration/InferenceRequestConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2626
using Monai.Deploy.InformaticsGateway.Api.Rest;
2727

28-
namespace Monai.Deploy.InformaticsGateway.Database
28+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2929
{
3030
internal class InferenceRequestConfiguration : IEntityTypeConfiguration<InferenceRequest>
3131
{

src/Database/MonaiApplicationEntityConfiguration.cs renamed to src/Database/EntityFramework/Configuration/MonaiApplicationEntityConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2626
using Monai.Deploy.InformaticsGateway.Api;
2727

28-
namespace Monai.Deploy.InformaticsGateway.Database
28+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2929
{
3030
internal class MonaiApplicationEntityConfiguration : IEntityTypeConfiguration<MonaiApplicationEntity>
3131
{

src/Database/PayloadConfiguration.cs renamed to src/Database/EntityFramework/Configuration/PayloadConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2525
using Monai.Deploy.InformaticsGateway.Api.Storage;
2626

27-
namespace Monai.Deploy.InformaticsGateway.Database
27+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2828
{
2929
internal class PayloadConfiguration : IEntityTypeConfiguration<Payload>
3030
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021-2022 MONAI Consortium
3+
* Copyright 2019-2021 NVIDIA Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
19+
{
20+
public static class SR
21+
{
22+
/// <summary>
23+
/// Name of the key for retrieve database connection string.
24+
/// </summary>
25+
public const string DatabaseConnectionStringKey = "InformaticsGatewayDatabase";
26+
27+
}
28+
}

src/Database/SourceApplicationEntityConfiguration.cs renamed to src/Database/EntityFramework/Configuration/SourceApplicationEntityConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
using Microsoft.EntityFrameworkCore;
1919
using Monai.Deploy.InformaticsGateway.Api;
2020

21-
namespace Monai.Deploy.InformaticsGateway.Database
21+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2222
{
2323
internal class SourceApplicationEntityConfiguration : IEntityTypeConfiguration<SourceApplicationEntity>
2424
{

src/Database/StorageMetadataWrapperEntityConfiguration.cs renamed to src/Database/EntityFramework/Configuration/StorageMetadataWrapperEntityConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
using Microsoft.EntityFrameworkCore;
1919
using Microsoft.EntityFrameworkCore.Metadata.Builders;
20+
using Monai.Deploy.InformaticsGateway.Database.Api;
2021

21-
namespace Monai.Deploy.InformaticsGateway.Database
22+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations
2223
{
2324
internal class StorageMetadataWrapperEntityConfiguration : IEntityTypeConfiguration<StorageMetadataWrapper>
2425
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2022 MONAI Consortium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Microsoft.EntityFrameworkCore;
18+
using Microsoft.Extensions.DependencyInjection;
19+
using Microsoft.Extensions.Hosting;
20+
using Microsoft.Extensions.Logging;
21+
using Monai.Deploy.InformaticsGateway.Database.Api;
22+
23+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework
24+
{
25+
public class EfDatabaseMigrationManager : IDatabaseMigrationManager
26+
{
27+
public IHost Migrate(IHost host)
28+
{
29+
using (var scope = host.Services.CreateScope())
30+
{
31+
using (var dbContext = scope.ServiceProvider.GetRequiredService<InformaticsGatewayContext>())
32+
{
33+
try
34+
{
35+
dbContext.Database.Migrate();
36+
}
37+
catch (Exception ex)
38+
{
39+
var logger = scope.ServiceProvider.GetService<ILogger>();
40+
logger?.Log(LogLevel.Critical, "Failed to migrate database", ex);
41+
throw;
42+
}
43+
}
44+
}
45+
return host;
46+
}
47+
}
48+
}

src/Database/InformaticsGatewayContext.cs renamed to src/Database/EntityFramework/InformaticsGatewayContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
using Microsoft.EntityFrameworkCore.Diagnostics;
1919
using Microsoft.Extensions.Logging;
2020
using Monai.Deploy.InformaticsGateway.Api;
21+
using Monai.Deploy.InformaticsGateway.Database.EntityFramework.Configurations;
2122

22-
namespace Monai.Deploy.InformaticsGateway.Database
23+
namespace Monai.Deploy.InformaticsGateway.Database.EntityFramework
2324
{
2425
public class InformaticsGatewayContext : DbContext
2526
{

0 commit comments

Comments
 (0)