Add persistence
This commit is contained in:
parent
c9da4cc805
commit
5cef1dcb4c
@ -18,7 +18,9 @@
|
||||
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftVersion)" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="$(MicrosoftVersion)" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftVersion)" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftVersion)" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftVersion)" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftVersion)" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Label="EntityFramework">
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
|
||||
|
||||
@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Contracts", "Contracts", "{
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocr.Users.Persistence", "src\Nocr.Users.Persistence\Nocr.Users.Persistence.csproj", "{51C01BA8-E3E1-45F4-9DDF-6E08DAF5BB46}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocr.Users.Migrator", "src\Nocr.Users.Migrator\Nocr.Users.Migrator.csproj", "{BA022CCE-FCAA-41E6-993D-B0794BBD0ECA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -46,6 +48,10 @@ Global
|
||||
{51C01BA8-E3E1-45F4-9DDF-6E08DAF5BB46}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{51C01BA8-E3E1-45F4-9DDF-6E08DAF5BB46}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{51C01BA8-E3E1-45F4-9DDF-6E08DAF5BB46}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{BA022CCE-FCAA-41E6-993D-B0794BBD0ECA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BA022CCE-FCAA-41E6-993D-B0794BBD0ECA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BA022CCE-FCAA-41E6-993D-B0794BBD0ECA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BA022CCE-FCAA-41E6-993D-B0794BBD0ECA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{4DDFB05F-DFC7-4BD6-B593-AD52CF1B002E} = {CA3AA87B-9217-4A2D-9EBA-BFC415B63818}
|
||||
|
||||
2
src/Nocr.Users.Migrator/AddMigration.sh
Executable file
2
src/Nocr.Users.Migrator/AddMigration.sh
Executable file
@ -0,0 +1,2 @@
|
||||
export DOTNET_ENVIRONMENT=Development
|
||||
dotnet ef migrations add $1
|
||||
@ -0,0 +1,37 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Nocr.Users.Persistence;
|
||||
|
||||
namespace Nocr.Users.Migrator;
|
||||
|
||||
public class DesignTimeTextMatcherContextFactory : IDesignTimeDbContextFactory<UsersContext>
|
||||
{
|
||||
private const string EnvironmentVariableKey = "DOTNET_ENVIRONMENT";
|
||||
|
||||
public UsersContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<UsersContext>();
|
||||
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
var environment = configuration.GetValue<string>(EnvironmentVariableKey);
|
||||
if (string.IsNullOrWhiteSpace(environment))
|
||||
{
|
||||
throw new InvalidOperationException($"Отсутствует переменная окружения {EnvironmentVariableKey}");
|
||||
}
|
||||
|
||||
var connectionString = configuration.GetConnectionString(environment);
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
throw new InvalidOperationException($"ConnectionString for environment `{environment}` not found");
|
||||
|
||||
optionsBuilder.UseMySql(connectionString, new MariaDbServerVersion(MariaDbServerVersion.LatestSupportedServerVersion),
|
||||
builder => builder.MigrationsAssembly("Nocr.Users.Migrator"));
|
||||
|
||||
return new UsersContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
19
src/Nocr.Users.Migrator/Dockerfile
Normal file
19
src/Nocr.Users.Migrator/Dockerfile
Normal file
@ -0,0 +1,19 @@
|
||||
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
|
||||
RUN dotnet nuget add source -n "musk.fun.nocr" "https://gitea.musk.fun/api/packages/nocr/nuget/index.json"
|
||||
RUN dotnet restore "src/Nocr.Users.Migrator/Nocr.Users.Migrator.csproj"
|
||||
WORKDIR "/src/src/Nocr.Users.Migrator"
|
||||
RUN dotnet build "Nocr.Users.Migrator.csproj" -c Release -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "Nocr.Users.Migrator.csproj" -c Release -o /app/publish
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Nocr.Users.Migrator.dll"]
|
||||
@ -8,10 +8,10 @@ using Nocr.Users.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Nocr.Users.Persistence.Migrations
|
||||
namespace Nocr.Users.Migrator.Migrations
|
||||
{
|
||||
[DbContext(typeof(UsersContext))]
|
||||
[Migration("20240330070727_InitialMigration")]
|
||||
[Migration("20240330083951_InitialMigration")]
|
||||
partial class InitialMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Nocr.Users.Persistence.Migrations
|
||||
namespace Nocr.Users.Migrator.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialMigration : Migration
|
||||
@ -7,7 +7,7 @@ using Nocr.Users.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Nocr.Users.Persistence.Migrations
|
||||
namespace Nocr.Users.Migrator.Migrations
|
||||
{
|
||||
[DbContext(typeof(UsersContext))]
|
||||
partial class UsersContextModelSnapshot : ModelSnapshot
|
||||
37
src/Nocr.Users.Migrator/Nocr.Users.Migrator.csproj
Normal file
37
src/Nocr.Users.Migrator/Nocr.Users.Migrator.csproj
Normal file
@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<IsPackable>false</IsPackable>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nocr.Users.Persistence\Nocr.Users.Persistence.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
22
src/Nocr.Users.Migrator/Program.cs
Normal file
22
src/Nocr.Users.Migrator/Program.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Nocr.Users.Migrator;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var contextFactory = new DesignTimeTextMatcherContextFactory();
|
||||
using (var context = contextFactory.CreateDbContext(Array.Empty<string>()))
|
||||
{
|
||||
await context.Database.MigrateAsync();
|
||||
await context.SaveChangesAsync();
|
||||
var appliedMigrations = await context.Database.GetAppliedMigrationsAsync();
|
||||
Console.WriteLine("Applied migrations:");
|
||||
foreach (var appliedMigration in appliedMigrations)
|
||||
{
|
||||
Console.WriteLine(appliedMigration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/Nocr.Users.Migrator/Readme.md
Normal file
10
src/Nocr.Users.Migrator/Readme.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Nocr.TextMatcher.Migrator
|
||||
|
||||
Проект содержит консольное приложение для применения миграций к БД.
|
||||
Для выбора строки подключения при запуске необходимо передать переменную окружения `DOTNET_ENVIRONMENT`. Для локальной разработки следует использовать `DOTNET_ENVIRONMENT=Development`.
|
||||
|
||||
## Добавление миграций
|
||||
|
||||
В приложении реализована логика выбора строки подключения, а dotnet ef не позволяет передать переменную окружения. Поэтому был создан скрипт AddMigration, который устанавливает значение `DOTNET_ENVIRONMENT=Development`.
|
||||
|
||||
Пример использования: `./AddMigration MyMigration`
|
||||
6
src/Nocr.Users.Migrator/appsettings.json
Normal file
6
src/Nocr.Users.Migrator/appsettings.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"Development": "server=localhost;port=3307;database=nocr_users;uid=root;pwd=toor",
|
||||
"DockerCompose": "server=nocr-users-db;port=3306;database=nocr_users;uid=root;pwd=toor"
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Nocr.Users.Persistence;
|
||||
|
||||
public class DesignTimeTextMatcherContextFactory : IDesignTimeDbContextFactory<UsersContext>
|
||||
{
|
||||
public UsersContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<UsersContext>();
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
var connectionString = configuration.GetConnectionString("MariaLocal");
|
||||
optionsBuilder.UseMySql(connectionString, new MariaDbServerVersion(MariaDbServerVersion.LatestSupportedServerVersion));
|
||||
|
||||
return new UsersContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
@ -9,18 +9,10 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nocr.Users.AppServices\Nocr.Users.AppServices.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="appsettings.json" />
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"MariaLocal": "server=localhost;port=3307;database=nocr_users;uid=root;pwd=toor"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user