Use a service to run migrations (#358)

This commit is contained in:
Zoe Roux 2024-03-26 00:07:12 +01:00 committed by GitHub
commit bd7991942a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 79 additions and 81 deletions

View File

@ -17,18 +17,32 @@ jobs:
matrix:
include:
- context: ./back
dockerfile: Dockerfile
label: back
image: zoriya/kyoo_back
- context: ./back
dockerfile: Dockerfile.migrations
label: migrations
image: zoriya/kyoo_migrations
- context: ./front
dockerfile: Dockerfile
label: front
image: zoriya/kyoo_front
- context: ./scanner
dockerfile: Dockerfile
label: scanner
image: zoriya/kyoo_scanner
- context: ./autosync
dockerfile: Dockerfile
label: autosync
image: zoriya/kyoo_autosync
- context: ./transcoder
dockerfile: Dockerfile
label: transcoder
image: zoriya/kyoo_transcoder
name: Build ${{matrix.label}}
@ -77,6 +91,7 @@ jobs:
if: steps.filter.outputs.should_run == 'true' || github.event_name == 'workflow_dispatch'
with:
context: ${{matrix.context}}
file: ${{matrix.context}}/${{matrix.dockerfile}}
platforms: linux/amd64,linux/arm64
build-args: |
VERSION=0.0.0

View File

@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "7.0.9",
"version": "8.0.3",
"commands": [
"dotnet-ef"
]

View File

@ -1,18 +1,17 @@
Dockerfile
Dockerfile.dev
Dockerfile.*
.dockerignore
.gitignore
docker-compose.yml
README.md
**/build
**/dist
src/Kyoo.WebApp/Front/nodes_modules
**/bin
**/obj
out
docs
tests
!tests/Kyoo.Tests/Kyoo.Tests.csproj
front
video
nginx.conf.template

View File

@ -19,7 +19,7 @@ COPY . .
ARG VERSION
RUN dotnet publish -a $TARGETARCH --no-restore -c Release -o /app "-p:Version=${VERSION:-"0.0.0-dev"}" src/Kyoo.Host
FROM mcr.microsoft.com/dotnet/aspnet:7.0
FROM mcr.microsoft.com/dotnet/aspnet:8.0
RUN apt-get update && apt-get install -y curl
COPY --from=builder /app /app

View File

@ -0,0 +1,13 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 as builder
ARG TARGETARCH
WORKDIR /kyoo
COPY .config/dotnet-tools.json .config/dotnet-tools.json
RUN dotnet tool restore
COPY . .
RUN dotnet ef migrations bundle --self-contained -f -o /app/migrate -p src/Kyoo.Postgresql --verbose
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0
COPY --from=builder /app/migrate /app/migrate
ENTRYPOINT /app/migrate --connection "USER ID=${POSTGRES_USER};PASSWORD=${POSTGRES_PASSWORD};SERVER=postgres;PORT=5432;DATABASE=${POSTGRES_DB};"

View File

@ -23,11 +23,6 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup>
<BaseIntermediateOutputPath>$(MsBuildThisFileDirectory)/../out/obj/$(MSBuildProjectName)</BaseIntermediateOutputPath>
<BaseOutputPath>$(MsBuildThisFileDirectory)/../out/bin/$(MSBuildProjectName)</BaseOutputPath>
</PropertyGroup>
<PropertyGroup>
<CheckCodingStyle Condition="$(CheckCodingStyle) == ''">true</CheckCodingStyle>
</PropertyGroup>

View File

@ -103,7 +103,6 @@ public class Application
await using (AsyncServiceScope scope = host.Services.CreateAsyncScope())
{
PostgresModule.Initialize(scope.ServiceProvider);
await MeilisearchModule.Initialize(scope.ServiceProvider);
}

View File

@ -26,13 +26,11 @@ using static System.Text.Json.JsonNamingPolicy;
namespace Kyoo.Meiliseach;
public class MeilisearchModule : IPlugin
public class MeilisearchModule(IConfiguration configuration) : IPlugin
{
/// <inheritdoc />
public string Name => "Meilisearch";
private readonly IConfiguration _configuration;
public static Dictionary<string, Settings> IndexSettings =>
new()
{
@ -122,11 +120,6 @@ public class MeilisearchModule : IPlugin
},
};
public MeilisearchModule(IConfiguration configuration)
{
_configuration = configuration;
}
/// <summary>
/// Init meilisearch indexes.
/// </summary>
@ -139,27 +132,6 @@ public class MeilisearchModule : IPlugin
await _CreateIndex(client, "items", true);
await _CreateIndex(client, nameof(Episode), false);
await _CreateIndex(client, nameof(Studio), false);
IndexStats info = await client.Index("items").GetStatsAsync();
// If there is no documents in meilisearch, if a db exist and is not empty, add items to meilisearch.
if (info.NumberOfDocuments == 0)
{
ILibraryManager database = provider.GetRequiredService<ILibraryManager>();
MeiliSync search = provider.GetRequiredService<MeiliSync>();
// This is a naive implementation that absolutly does not care about performances.
// This will run only once on users that already had a database when they upgrade.
foreach (Movie movie in await database.Movies.GetAll(limit: 0))
await search.CreateOrUpdate("items", movie, nameof(Movie));
foreach (Show show in await database.Shows.GetAll(limit: 0))
await search.CreateOrUpdate("items", show, nameof(Show));
foreach (Collection collection in await database.Collections.GetAll(limit: 0))
await search.CreateOrUpdate("items", collection, nameof(Collection));
foreach (Episode episode in await database.Episodes.GetAll(limit: 0))
await search.CreateOrUpdate(nameof(Episode), episode);
foreach (Studio studio in await database.Studios.GetAll(limit: 0))
await search.CreateOrUpdate(nameof(Studio), studio);
}
}
private static async Task _CreateIndex(MeilisearchClient client, string index, bool hasKind)
@ -178,8 +150,8 @@ public class MeilisearchModule : IPlugin
builder
.RegisterInstance(
new MeilisearchClient(
_configuration.GetValue("MEILI_HOST", "http://meilisearch:7700"),
_configuration.GetValue<string?>("MEILI_MASTER_KEY")
configuration.GetValue("MEILI_HOST", "http://meilisearch:7700"),
configuration.GetValue<string?>("MEILI_MASTER_KEY")
)
)
.SingleInstance();

View File

@ -38,45 +38,13 @@ namespace Kyoo.Postgresql;
/// <summary>
/// A module to add postgresql capacity to the app.
/// </summary>
public class PostgresModule : IPlugin
public class PostgresModule(IConfiguration configuration, IWebHostEnvironment environment) : IPlugin
{
/// <inheritdoc />
public string Name => "Postgresql";
/// <summary>
/// The configuration to use. The database connection string is pulled from it.
/// </summary>
private readonly IConfiguration _configuration;
/// <summary>
/// The host environment to check if the app is in debug mode.
/// </summary>
private readonly IWebHostEnvironment _environment;
/// <summary>
/// Create a new postgres module instance and use the given configuration and environment.
/// </summary>
/// <param name="configuration">The configuration to use</param>
/// <param name="env">The environment that will be used (if the env is in development mode, more information will be displayed on errors.</param>
public PostgresModule(IConfiguration configuration, IWebHostEnvironment env)
static PostgresModule()
{
_configuration = configuration;
_environment = env;
}
/// <summary>
/// Migrate the database.
/// </summary>
/// <param name="provider">The service list to retrieve the database context</param>
public static void Initialize(IServiceProvider provider)
{
DatabaseContext context = provider.GetRequiredService<DatabaseContext>();
context.Database.Migrate();
using NpgsqlConnection conn = (NpgsqlConnection)context.Database.GetDbConnection();
conn.Open();
conn.ReloadTypes();
SqlMapper.TypeMapProvider = (type) =>
{
return new CustomPropertyTypeMap(
@ -118,11 +86,11 @@ public class PostgresModule : IPlugin
DbConnectionStringBuilder builder =
new()
{
["USER ID"] = _configuration.GetValue("POSTGRES_USER", "KyooUser"),
["PASSWORD"] = _configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"),
["SERVER"] = _configuration.GetValue("POSTGRES_SERVER", "db"),
["PORT"] = _configuration.GetValue("POSTGRES_PORT", "5432"),
["DATABASE"] = _configuration.GetValue("POSTGRES_DB", "kyooDB"),
["USER ID"] = configuration.GetValue("POSTGRES_USER", "KyooUser"),
["PASSWORD"] = configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"),
["SERVER"] = configuration.GetValue("POSTGRES_SERVER", "db"),
["PORT"] = configuration.GetValue("POSTGRES_PORT", "5432"),
["DATABASE"] = configuration.GetValue("POSTGRES_DB", "kyooDB"),
["POOLING"] = "true",
["MAXPOOLSIZE"] = "95",
["TIMEOUT"] = "30"
@ -132,7 +100,7 @@ public class PostgresModule : IPlugin
x =>
{
x.UseNpgsql(builder.ConnectionString).UseProjectables();
if (_environment.IsDevelopment())
if (environment.IsDevelopment())
x.EnableDetailedErrors().EnableSensitiveDataLogging();
},
ServiceLifetime.Transient

View File

@ -35,11 +35,24 @@ services:
condition: service_healthy
rabbitmq:
condition: service_healthy
migrations:
condition: service_completed_successfully
volumes:
- ./back:/app
- /app/out/
- kyoo:/kyoo
migrations:
build:
context: ./back
dockerfile: Dockerfile.migrations
restart: "no"
depends_on:
postgres:
condition: service_healthy
env_file:
- ./.env
front:
build:
context: ./front

View File

@ -26,9 +26,20 @@ services:
condition: service_healthy
rabbitmq:
condition: service_healthy
migrations:
condition: service_completed_successfully
volumes:
- kyoo:/kyoo
migrations:
image: zoriya/kyoo_migrations:edge
restart: "no"
depends_on:
postgres:
condition: service_healthy
env_file:
- ./.env
front:
image: zoriya/kyoo_front:edge
restart: unless-stopped

View File

@ -25,9 +25,22 @@ services:
condition: service_healthy
rabbitmq:
condition: service_healthy
migrations:
condition: service_completed_successfully
volumes:
- kyoo:/kyoo
migrations:
build:
context: ./back
dockerfile: Dockerfile.migrations
restart: "no"
depends_on:
postgres:
condition: service_healthy
env_file:
- ./.env
front:
build: ./front
restart: on-failure