Skip to content
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
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: .NET

on:
push:
branches: [ '**' ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v6

- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: '10.0.x'

- name: Restore
run: dotnet restore

- name: Build
run: dotnet build --no-restore -c Release
33 changes: 0 additions & 33 deletions .github/workflows/dotnet.yml

This file was deleted.

21 changes: 5 additions & 16 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,29 @@ jobs:
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v5
with:
dotnet-version: '8.0.x'
dotnet-version: '10.0.x'

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Add Garage Group NuGet Source
run: >
dotnet nuget add source ${{ vars.GG_NUGET_SOURCE_URL }}
-n garage
-u ${{ secrets.GG_NUGET_SOURCE_USER_NAME }}
-p ${{ secrets.GG_NUGET_SOURCE_USER_PASSWORD }}
--store-password-in-clear-text
uses: docker/setup-buildx-action@v4

- name: Publish Console.csproj
run: dotnet publish ./src/*/Console.csproj -c Release -o './publish'

- name: Login to Github Packages
uses: docker/login-action@v2
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build image and push to GitHub Container Registry
uses: docker/build-push-action@v4
uses: docker/build-push-action@v7
with:
context: .
file: ./Dockerfile
tags: |
ghcr.io/garagegroup/gg-sql-migration:${{ steps.vars.outputs.tag }}
ghcr.io/garagegroup/gg-sql-migration:latest
push: true

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0
FROM mcr.microsoft.com/dotnet/runtime:10.0

WORKDIR /app
COPY ./publish ./
Expand Down
10 changes: 10 additions & 0 deletions Infra.Sql.Migration.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Solution>
<Folder Name="/.sln/">
<File Path="action.yml" />
<File Path="Dockerfile" />
<File Path="./.github/workflows/build.yml" />
<File Path="./.github/workflows/push.yml" />
</Folder>
<Project Path="src/Console/Console.csproj" />
<Project Path="src/Handler/Handler.csproj" />
</Solution>
27 changes: 27 additions & 0 deletions src/Console/Application.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PrimeFuncPack;

namespace GarageGroup.Infra;

internal static class Application
{
public static Dependency<ISqlMigrateHandler> UseSqlMigrateHandler()
=>
MicrosoftDbProvider.Configure("SqlDb")
.UseSqlApi()
.With(ResolveSqlMigrationOption)
.UseSqlMigrateHandler();

private static SqlMigrationOption ResolveSqlMigrationOption(IServiceProvider serviceProvider)
{
var section = serviceProvider.GetRequiredService<IConfiguration>().GetRequiredSection("Migrations");

return new(
configPath: section["ConfigPath"])
{
BasePath = section["BasePath"]
};
}
}
11 changes: 6 additions & 5 deletions src/Console/Console.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<InvariantGlobalization>false</InvariantGlobalization>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);IDE0130;CA1859</NoWarn>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Sql.Migration.Console</AssemblyName>
</PropertyGroup>
Expand All @@ -20,9 +21,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GarageGroup.Infra.Handler.Console" Version="0.5.1" />
<PackageReference Include="GarageGroup.Infra.Sql.Api" Version="2.1.0" />
<PackageReference Include="GarageGroup.Infra.Sql.Api.Provider.Microsoft" Version="2.1.0" />
<PackageReference Include="GarageGroup.Infra.Handler.Console" Version="0.14.1" />
<PackageReference Include="GarageGroup.Infra.Sql.Api" Version="3.1.0" />
<PackageReference Include="GarageGroup.Infra.Sql.Api.Provider.Microsoft" Version="3.1.0" />
</ItemGroup>

</Project>
</Project>
8 changes: 2 additions & 6 deletions src/Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace GarageGroup.Infra.Sql.Migration.Console;

static class Program
{
static Task Main(string[] args)
=>
MicrosoftDbProvider.Configure("SqlDb")
.UseSqlApi()
.UseSqlMigrateHandler("Migrations")
.RunConsoleAsync<SqlMigrateHandler, Unit>(default, args);
Application.UseSqlMigrateHandler().UseConsoleRunner(args).RunAsync();
}
5 changes: 5 additions & 0 deletions src/Handler/Handler.Contract/ISqlMigrateHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace GarageGroup.Infra;

public interface ISqlMigrateHandler : IHandler<Unit, Unit>;
16 changes: 8 additions & 8 deletions src/Handler/Handler.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<InvariantGlobalization>true</InvariantGlobalization>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);IDE0130;CA1859</NoWarn>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Sql.Migration.Handler</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GarageGroup.Infra.Handler.Core" Version="0.3.1" />
<PackageReference Include="GarageGroup.Infra.Sql.Api.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="PrimeFuncPack.Dependency" Version="2.0.2" />
<PackageReference Include="PrimeFuncPack.Primitives.Strings" Version="2.0.2" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
<PackageReference Include="GarageGroup.Infra.Handler.Core" Version="1.0.0" />
<PackageReference Include="GarageGroup.Infra.Sql.Api.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.9" />
<PackageReference Include="PrimeFuncPack.Dependency" Version="2.2.0" />
<PackageReference Include="PrimeFuncPack.Primitives.Strings" Version="3.0.0" />
<PackageReference Include="YamlDotNet" Version="18.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,28 @@ namespace GarageGroup.Infra;

partial class SqlMigrateHandler
{
public ValueTask<Result<Unit, Failure<HandlerFailureCode>>> HandleAsync(Unit _, CancellationToken cancellationToken)
public async ValueTask<Result<Unit, Failure<HandlerFailureCode>>> HandleAsync(Unit unit, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled<Result<Unit, Failure<HandlerFailureCode>>>(cancellationToken);
}

return InnerHandleAsync(cancellationToken);
}

private async ValueTask<Result<Unit, Failure<HandlerFailureCode>>> InnerHandleAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Check if the change log table exists");
logger.LogInformation("Check if the change log table exists.");
await changeLogApi.EnsureTableAsync(cancellationToken).ConfigureAwait(false);

logger.LogInformation("Get the last change log from the database");
logger.LogInformation("Get the last change log from the database.");
var dbChangeLogId = await changeLogApi.GetLastChangeLogIdAsync(cancellationToken).ConfigureAwait(false);

var notExecutedMigrations = await migrationItemApi.GetMigrationItemsAsync(dbChangeLogId?.Id, cancellationToken).ConfigureAwait(false);
if (notExecutedMigrations.IsEmpty)
{
logger.LogInformation("All migrations were already applied");
return Result.Success<Unit>(default);
logger.LogInformation("All migrations were already applied.");
return Result.Success(unit);
}

foreach (var migration in notExecutedMigrations)
{
logger.LogInformation("Apply the migration {migrationId}", migration.Id);
logger.LogInformation("Apply the migration {migrationId}.", migration.Id);
await changeLogApi.ExecuteMigrationQueryAsync(migration, cancellationToken).ConfigureAwait(false);
}

logger.LogInformation("All migrations have been applied successfully");
return Result.Success<Unit>(default);
logger.LogInformation("All migrations have been applied successfully.");
return Result.Success(unit);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using Microsoft.Extensions.Logging;

namespace GarageGroup.Infra;

public sealed partial class SqlMigrateHandler : IHandler<Unit, Unit>
internal sealed partial class SqlMigrateHandler : ISqlMigrateHandler
{
internal static SqlMigrateHandler InternalCreate(
IDbChangeLogApi changeLogApi, ISqlMigrationItemApi migrationItemApi, ILoggerFactory loggerFactory)
Expand Down
12 changes: 3 additions & 9 deletions src/Handler/Internal.DbChangeLog/Api/Api.EnsureTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ namespace GarageGroup.Infra;

partial class DbChangeLogApi
{
public async ValueTask EnsureTableAsync(CancellationToken cancellationToken)
{
var query = new DbQuery(DbChangeLogCreateTableQuery)
{
TimeoutInSeconds = DbTimeoutInSeconds
};

_ = await sqlApi.ExecuteNonQueryAsync(query, cancellationToken).ConfigureAwait(false);
}
public Task EnsureTableAsync(CancellationToken cancellationToken)
=>
sqlApi.ExecuteNonQueryAsync(DbChangeLogCreateTableQuery, cancellationToken).AsTask();
}
17 changes: 7 additions & 10 deletions src/Handler/Internal.DbChangeLog/Api/Api.ExecuteMigrationQuery.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace GarageGroup.Infra;

partial class DbChangeLogApi
{
public async ValueTask ExecuteMigrationQueryAsync(SqlMigrationItem migrationItem, CancellationToken cancellationToken)
public async Task ExecuteMigrationQueryAsync(SqlMigrationItem migrationItem, CancellationToken cancellationToken)
{
var migrationQuery = await ReadMigrationQueryAsync(migrationItem, cancellationToken).ConfigureAwait(false);

Expand All @@ -17,25 +16,23 @@ public async ValueTask ExecuteMigrationQueryAsync(SqlMigrationItem migrationItem

_ = await sqlApi.ExecuteNonQueryAsync(migrationRequest, cancellationToken).ConfigureAwait(false);

var logInsertRequest = new DbQuery(
query: DbChangeLogInsertQuery,
parameters: new(
new("Id", migrationItem.Id),
new("Comment", migrationItem.Comment.OrEmpty())))
var logInsertRequest = new DbChangeLogInsertQuery(
migrationId: migrationItem.Id,
comment: migrationItem.Comment)
{
TimeoutInSeconds = DbTimeoutInSeconds
};

_ = await sqlApi.ExecuteNonQueryAsync(logInsertRequest, cancellationToken).ConfigureAwait(false);
}

private ValueTask<string> ReadMigrationQueryAsync(SqlMigrationItem migrationItem, CancellationToken cancellationToken)
private Task<string> ReadMigrationQueryAsync(SqlMigrationItem migrationItem, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(migrationItem.FilePath))
{
return new(string.Empty);
return Task.FromResult(string.Empty);
}

return new(fileReader.ReadAsync(option.BasePath, migrationItem.FilePath, cancellationToken));
return fileReader.ReadAsync(option.BasePath, migrationItem.FilePath, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ namespace GarageGroup.Infra;

partial class DbChangeLogApi
{
public async ValueTask<DbChangeLogId?> GetLastChangeLogIdAsync(CancellationToken cancellationToken)
public async Task<DbChangeLogId?> GetLastChangeLogIdAsync(CancellationToken cancellationToken)
{
var sqlRequest = new DbQuery(DbChangeLogIdLastGetQuery)
{
TimeoutInSeconds = DbTimeoutInSeconds
};

var dbResult = await sqlApi.QueryEntityOrAbsentAsync<DbChangeLogId>(sqlRequest, cancellationToken).ConfigureAwait(false);
var dbResult = await sqlApi.QueryEntityOrAbsentAsync<DbChangeLogId>(DbChangeLogIdLastGetQuery, cancellationToken).ConfigureAwait(false);
return dbResult.Fold(AsNullable, GetNull);

static DbChangeLogId? AsNullable(DbChangeLogId dbChangeLogId)
Expand Down
Loading