Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(migrations): add custom history repository #1263

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/framework/Framework.Async/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Cors/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
111 changes: 111 additions & 0 deletions src/framework/Framework.DBAccess/CustomNpgsqlHistoryRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/********************************************************************************
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
using Npgsql;
using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.DBAccess;

#pragma warning disable EF1001
public class CustomNpgsqlHistoryRepository(HistoryRepositoryDependencies dependencies) :
NpgsqlHistoryRepository(dependencies), IHistoryRepository
{
protected override string ExistsSql => $"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = '{this.TableSchema}' AND table_name = '{this.TableName}');";

public override bool Exists() =>
this.Dependencies.DatabaseCreator.Exists() &&
this.InterpretExistsResult(
this.Dependencies.RawSqlCommandBuilder
.Build(this.ExistsSql)
.ExecuteScalar(new RelationalCommandParameterObject(this.Dependencies.Connection, null, null, this.Dependencies.CurrentContext.Context, this.Dependencies.CommandLogger, CommandSource.Migrations)));

public override async Task<bool> ExistsAsync(CancellationToken cancellationToken = new())
{
if (!await this.Dependencies.DatabaseCreator.ExistsAsync(cancellationToken).ConfigureAwait(false))
{
return false;
}

var existsCommand = await this.Dependencies.RawSqlCommandBuilder
.Build(this.ExistsSql)
.ExecuteScalarAsync(new RelationalCommandParameterObject(this.Dependencies.Connection, null, null, this.Dependencies.CurrentContext.Context, this.Dependencies.CommandLogger, CommandSource.Migrations), cancellationToken)
.ConfigureAwait(false);
return this.InterpretExistsResult(existsCommand);
}

bool IHistoryRepository.CreateIfNotExists()
{
if (Exists())
{
return true;
}

try
{
return Dependencies.MigrationCommandExecutor.ExecuteNonQuery(GetCreateIfNotExistsCommands(), Dependencies.Connection, new MigrationExecutionState(), commitTransaction: true) != 0;
}
catch (PostgresException e) when (e.SqlState is PostgresErrorCodes.UniqueViolation
or PostgresErrorCodes.DuplicateTable
or PostgresErrorCodes.DuplicateObject)
{
return false;
}
}

async Task<bool> IHistoryRepository.CreateIfNotExistsAsync(CancellationToken cancellationToken)
{
if (await ExistsAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
{
return true;
}

try
{
return await Dependencies.MigrationCommandExecutor
.ExecuteNonQueryAsync(
GetCreateIfNotExistsCommands(),
Dependencies.Connection,
new MigrationExecutionState(),
commitTransaction: true,
cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None) != 0;
}
catch (PostgresException e) when (e.SqlState is PostgresErrorCodes.UniqueViolation
or PostgresErrorCodes.DuplicateTable
or PostgresErrorCodes.DuplicateObject)
{
return false;
}
}

private IReadOnlyList<MigrationCommand> GetCreateIfNotExistsCommands() =>
Dependencies.MigrationsSqlGenerator.Generate(
[
new SqlOperation
{
Sql = GetCreateIfNotExistsScript(),
SuppressTransaction = true
}
]);
}
#pragma warning enable EF1001

Check warning on line 111 in src/framework/Framework.DBAccess/CustomNpgsqlHistoryRepository.cs

View workflow job for this annotation

GitHub Actions / Build, check and test services (9.0)

Expected 'disable' or 'restore'

Check warning on line 111 in src/framework/Framework.DBAccess/CustomNpgsqlHistoryRepository.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, 9.0)

Expected 'disable' or 'restore'
2 changes: 1 addition & 1 deletion src/framework/Framework.DBAccess/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions src/framework/Framework.DBAccess/Framework.DBAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Framework.ErrorHandling\Framework.ErrorHandling.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.IO/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Identity/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Linq/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Logging/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Models/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Seeding/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Swagger/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Token/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/framework/Framework.Web/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>3.2.0</VersionPrefix>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
6 changes: 5 additions & 1 deletion src/portalbackend/PortalBackend.Migrations/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

using Laraue.EfCoreTriggers.PostgreSql.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Logging;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.Migrations;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Auditing;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ProcessIdentity.DependencyInjection;
Expand All @@ -44,7 +47,8 @@
.AddDbContext<PortalDbContext>(o =>
o.UseNpgsql(hostContext.Configuration.GetConnectionString("PortalDb"),
x => x.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name)
.MigrationsHistoryTable("__efmigrations_history_portal"))
.MigrationsHistoryTable("__efmigrations_history_portal", "public"))
.ReplaceService<IHistoryRepository, CustomNpgsqlHistoryRepository>()
.UsePostgreSqlTriggers())
.AddDatabaseInitializer<PortalDbContext>(hostContext.Configuration.GetSection("Seeding"));
})
Expand Down
6 changes: 5 additions & 1 deletion src/provisioning/Provisioning.Migrations/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
// See https://aka.ms/new-console-template for more information

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Logging;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.ProvisioningEntities;
Expand All @@ -39,7 +41,9 @@
services.AddDbContext<ProvisioningDbContext>(o =>
o.UseNpgsql(hostContext.Configuration.GetConnectionString("ProvisioningDb"),
x => x.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name)
.MigrationsHistoryTable("__efmigrations_history_provisioning", "public")))
.MigrationsHistoryTable("__efmigrations_history_provisioning", "public"))
.ReplaceService<IHistoryRepository, CustomNpgsqlHistoryRepository>()
)
.AddDatabaseInitializer<ProvisioningDbContext>(hostContext.Configuration.GetSection("Seeding"));
})
.AddLogging()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\framework\Framework.DBAccess\Framework.DBAccess.csproj" />
<ProjectReference Include="..\..\framework\Framework.Logging\Framework.Logging.csproj" />
<ProjectReference Include="..\..\framework\Framework.Seeding\Framework.Seeding.csproj" />
<ProjectReference Include="..\Provisioning.ProvisioningEntities\Provisioning.ProvisioningEntities.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DBAccess;
using System.Reflection;

namespace Org.Eclipse.TractusX.Portal.Backend.Provisioning.ProvisioningEntities;
Expand All @@ -37,7 +39,8 @@ public ProvisioningDbContext CreateDbContext(string[] args)
optionsBuilder.UseNpgsql(
config.GetConnectionString("ProvisioningDb"),
x => x.MigrationsAssembly(typeof(ProvisioningDbContextFactory).Assembly.GetName().Name)
.MigrationsHistoryTable("__efmigrations_history_provisioning", "public"));
.MigrationsHistoryTable("__efmigrations_history_provisioning", "public"))
.ReplaceService<IHistoryRepository, CustomNpgsqlHistoryRepository>();

return new ProvisioningDbContext(optionsBuilder.Options);
}
Expand Down
Loading