diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index 271a0aa45..a45f9dff7 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -59,6 +59,75 @@ jobs: - name: Test run: dotnet test --configuration Release --no-build --blame-hang-timeout "2m" --verbosity normal + migrations: + name: EF migration integrity (SQLite + MySql) + runs-on: ubuntu-latest + # Independent gate (not a 'needs' of build yet) so the new MySql-service dependency + # can't block image builds until it's proven reliable on the runner. Promote to a + # required check / build dependency once green. (ersatztv#13) + services: + mysql: + image: mysql:8.4 + env: + MYSQL_ROOT_PASSWORD: ersatztv + MYSQL_DATABASE: ersatztv_migrations + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -h 127.0.0.1 -uroot -persatztv --silent" + --health-interval=5s + --health-timeout=5s + --health-retries=30 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + + - name: Restore + run: dotnet restore + + - name: Build + run: dotnet build --configuration Release --no-restore + + - name: Install dotnet-ef + run: dotnet tool install --global dotnet-ef --version 9.0.12 + + # SQLite is the prod provider; both checks validated locally. + - name: SQLite — model drift + apply all migrations to a fresh DB + run: | + set -euo pipefail + export PATH="$PATH:$HOME/.dotnet/tools" + echo "::group::SQLite model drift (has-pending-model-changes)" + dotnet ef migrations has-pending-model-changes --no-build --configuration Release \ + --context TvContext --startup-project ErsatzTV --project ErsatzTV.Infrastructure.Sqlite -- --provider Sqlite + echo "::endgroup::" + echo "::group::SQLite apply all migrations to a fresh DB" + export ETV_CONFIG_FOLDER="$(mktemp -d)" ETV_TRANSCODE_FOLDER="$(mktemp -d)" + dotnet ef database update --no-build --configuration Release \ + --context TvContext --startup-project ErsatzTV --project ErsatzTV.Infrastructure.Sqlite -- --provider Sqlite + echo "::endgroup::" + + # MySql uses ServerVersion.AutoDetect (connects at config time), so it runs against the + # service container above. MySql__ConnectionString maps to config key "MySql:ConnectionString". + - name: MySql — model drift + apply all migrations to a fresh DB + env: + MySql__ConnectionString: "Server=mysql;Port=3306;Database=ersatztv_migrations;Uid=root;Pwd=ersatztv;" + run: | + set -euo pipefail + export PATH="$PATH:$HOME/.dotnet/tools" + echo "::group::MySql model drift (has-pending-model-changes)" + dotnet ef migrations has-pending-model-changes --no-build --configuration Release \ + --context TvContext --startup-project ErsatzTV --project ErsatzTV.Infrastructure.MySql -- --provider MySql + echo "::endgroup::" + echo "::group::MySql apply all migrations to a fresh DB" + dotnet ef database update --no-build --configuration Release \ + --context TvContext --startup-project ErsatzTV --project ErsatzTV.Infrastructure.MySql -- --provider MySql + echo "::endgroup::" + build: name: Build & push image (amd64) runs-on: ubuntu-latest diff --git a/CLAUDE.md b/CLAUDE.md index 0320b3f10..50f23fd4e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -58,6 +58,7 @@ docker build -f docker/Dockerfile -t ersatztv:dev . - Keep Blazor pages thin — delegate to MediatR handlers - Test with **NUnit** + Shouldly + NSubstitute (the existing `*.Tests` projects); xUnit is **not** used here - **Dependencies use Central Package Management**: versions live in the repo-root `Directory.Packages.props`; csproj reference packages by name only. Add/upgrade by editing the central `` — never put `Version=` back on a `` (trips `NU1008`). See `docs/ci-cd.md` → Dependency management. +- **DB migrations target BOTH providers**: a `TvContext` model change needs a migration in `ErsatzTV.Infrastructure.Sqlite` **and** `ErsatzTV.Infrastructure.MySql` — run `scripts/add-migration.sh ` (does both). CI's `migrations` job enforces model-drift + apply-to-fresh-DB per provider. See `docs/ci-cd.md` → Migration integrity. - **Renovate** is live (`.gitea/workflows/renovate.yml`, weekly + `workflow_dispatch`): opens dependency-update + OSV vuln-fix PRs and a Dependency Dashboard issue; patch bumps to test/dev-only packages auto-merge once `Build & test` passes, the rest are manual. Cross-repo rollout: server-management#484. See `docs/ci-cd.md` → Dependency management. - **Versioning**: release tags are `vYY..` (year · sequential release-within-year · patch) — inherited from upstream, **not** year.month. `v26.3.1` = our infra rebuild of upstream 26.3.0 (no app changes); `v26.4.0` is reserved for the first release with app changes. Never `[skip ci]` a commit you'll tag (it suppresses the release build). Full policy: `docs/ci-cd.md` → Versioning & releases. - Backlog tracked via [Gitea Issues](http://192.168.1.95:3000/timothy/ersatztv/issues) diff --git a/docs/ci-cd.md b/docs/ci-cd.md index 4ad20d746..28a62e3d2 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -161,6 +161,42 @@ PR** (kept out of the analyzer work to stay reviewable); afterwards `dotnet form --verify-no-changes` (+ `style`) joins the `test` job so drift can't return. `.gitattributes` already pins line endings. +## Migration integrity (EF Core, both providers) + +`TvContext` (`ErsatzTV.Infrastructure/Data/TvContext.cs`) has **two** migration sets — one per +provider project: `ErsatzTV.Infrastructure.Sqlite/Migrations` and +`ErsatzTV.Infrastructure.MySql/Migrations`, each with its own `TvContextModelSnapshot`. **A model +change needs a migration in BOTH.** Add them with `scripts/add-migration.sh ` (runs the EF CLI +for each provider). The EF CLI pattern (provider selected by the post-`--` arg, which `Startup` +reads as the `provider` config key): + +``` +dotnet ef --context TvContext --startup-project ErsatzTV \ + --project ErsatzTV.Infrastructure.{Sqlite|MySql} -- --provider {Sqlite|MySql} +``` + +The **`migrations` job** in `docker-build.yml` runs on every push/PR and, for **each** provider: + +1. `dotnet ef migrations has-pending-model-changes` — fails if an entity changed without a matching + migration (**model drift**), so a forgotten migration can't merge. +2. `dotnet ef database update` against a **fresh empty DB** — applies *all* migrations in order and + fails on any broken/un-orderable one. + +- **SQLite** (the prod provider) uses a throwaway file (`ETV_CONFIG_FOLDER=$(mktemp -d)`); no service + needed. Validated: 787 migrations → 139 tables. +- **MySql** uses `ServerVersion.AutoDetect`, which **connects at config time**, so the job needs a + reachable server — provided by a `services: mysql:8.4` container (the act_runner uses Docker + execution on network `downloadswarm`, so the service is reachable as `mysql:3306`). Connection + string via `MySql__ConnectionString` (→ config key `MySql:ConnectionString`). Validated: 305 + migrations → 137 tables. It's an **independent gate** (not yet a `needs:` of the image build) so + the new MySql-service dependency can't block image builds until it's proven; promote it to a + required check once stable. + +**Caveat — non-transactional operations**: some migrations (e.g. SQLite `PRAGMA foreign_keys`) run +outside a transaction and warn at startup; they can't be rolled back mid-migration, so review such +migrations carefully (this is part of what motivated the apply-to-fresh check before the prod +cutover, server-management#481). + ## Registry Gitea Packages, HTTP-only at `192.168.1.95:3000`. jazz's Docker daemon has it as an