From b5fb4395929bf31259f0e4c22778ce537fee8e32 Mon Sep 17 00:00:00 2001 From: Antigravity Date: Sun, 17 May 2026 08:53:29 +0000 Subject: [PATCH] fix(ci): migrations idempotentes et deploy prod sans toucher Postgres MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les migrations échouaient sur une base vide (contraintes/index déjà créés par init). Le workflow deploy ne recrée plus Postgres ni ne force les entrées _prisma_migrations. Co-authored-by: Cursor --- .gitea/workflows/ci.yaml | 7 +++- .gitea/workflows/deploy.yaml | 8 +--- .../migration.sql | 9 ++++- .../migration.sql | 40 +++++++++++++------ 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index ab58fdd..275c96d 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -51,7 +51,12 @@ jobs: - name: Setup test database run: | - apt-get update && apt-get install -y postgresql-client + sudo apt-get update -qq && sudo apt-get install -y -qq postgresql-client + for i in $(seq 1 30); do + PGPASSWORD=memento_test pg_isready -h postgres -U memento_test -d memento_test && break + [ "$i" -eq 30 ] && exit 1 + sleep 2 + done PGPASSWORD=memento_test psql -h postgres -U memento_test -d memento_test -c "CREATE EXTENSION IF NOT EXISTS vector;" npx prisma migrate deploy diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index 5491fb8..6e87452 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -121,9 +121,8 @@ jobs: git fetch origin main git reset --hard origin/main - echo "=== Pull & recreate postgres with pgvector ===" - docker compose pull postgres - docker compose up -d --force-recreate postgres + echo "=== Ensure postgres is running (no recreate — prod data) ===" + docker compose up -d postgres echo "=== Waiting for postgres healthy ===" for i in $(seq 1 30); do @@ -153,9 +152,6 @@ jobs: fi echo "Backup saved: $DUMP_FILE ($(( DUMP_SIZE / 1024 ))KB)" - echo "=== Resolve failed prisma migrations ===" - docker compose exec -T postgres psql -U "${POSTGRES_USER:-memento}" -d "${POSTGRES_DB:-memento}" -c "UPDATE \"_prisma_migrations\" SET \"finished_at\"=NOW(), \"rolled_back_at\"=NULL WHERE \"finished_at\" IS NULL AND \"rolled_back_at\" IS NULL;" || true - echo "=== Building app images ===" docker compose build memento-note docker compose build mcp-server diff --git a/memento-note/prisma/migrations/20260426150000_add_missing_settings_and_fix_constraints/migration.sql b/memento-note/prisma/migrations/20260426150000_add_missing_settings_and_fix_constraints/migration.sql index bc7925b..3221ddc 100644 --- a/memento-note/prisma/migrations/20260426150000_add_missing_settings_and_fix_constraints/migration.sql +++ b/memento-note/prisma/migrations/20260426150000_add_missing_settings_and_fix_constraints/migration.sql @@ -41,15 +41,20 @@ CREATE INDEX IF NOT EXISTS "NoteShare_status_idx" ON "NoteShare"("status"); CREATE INDEX IF NOT EXISTS "NoteShare_sharedBy_idx" ON "NoteShare"("sharedBy"); -- Add unique constraint for MemoryEchoInsight (idempotent) +-- init migration may already have created these as UNIQUE INDEXes (same relation name) DO $$ BEGIN ALTER TABLE "MemoryEchoInsight" ADD CONSTRAINT "MemoryEchoInsight_userId_insightDate_key" UNIQUE ("userId", "insightDate"); -EXCEPTION WHEN duplicate_object THEN NULL; +EXCEPTION + WHEN duplicate_object THEN NULL; + WHEN duplicate_table THEN NULL; END $$; -- Add unique constraint for NoteShare (idempotent) DO $$ BEGIN ALTER TABLE "NoteShare" ADD CONSTRAINT "NoteShare_noteId_userId_key" UNIQUE ("noteId", "userId"); -EXCEPTION WHEN duplicate_object THEN NULL; +EXCEPTION + WHEN duplicate_object THEN NULL; + WHEN duplicate_table THEN NULL; END $$; -- Fix Note.notebookId foreign key to use ON DELETE SET NULL (idempotent) diff --git a/memento-note/prisma/migrations/20260510123000_add_notebook_hierarchy_and_trash/migration.sql b/memento-note/prisma/migrations/20260510123000_add_notebook_hierarchy_and_trash/migration.sql index ced0154..bc5897b 100644 --- a/memento-note/prisma/migrations/20260510123000_add_notebook_hierarchy_and_trash/migration.sql +++ b/memento-note/prisma/migrations/20260510123000_add_notebook_hierarchy_and_trash/migration.sql @@ -1,18 +1,32 @@ --- AlterTable -ALTER TABLE "Label" ADD COLUMN "type" TEXT NOT NULL DEFAULT 'user'; +-- AlterTable Label (may already exist from 20260509160000_add_missing_columns) +DO $$ BEGIN + ALTER TABLE "Label" ADD COLUMN "type" TEXT NOT NULL DEFAULT 'user'; +EXCEPTION WHEN duplicate_column THEN NULL; +END $$; --- AlterTable -ALTER TABLE "UserAISettings" ADD COLUMN "autoSave" BOOLEAN NOT NULL DEFAULT true; +-- AlterTable UserAISettings +DO $$ BEGIN + ALTER TABLE "UserAISettings" ADD COLUMN "autoSave" BOOLEAN NOT NULL DEFAULT true; +EXCEPTION WHEN duplicate_column THEN NULL; +END $$; --- AlterTable -ALTER TABLE "Notebook" ADD COLUMN "parentId" TEXT, -ADD COLUMN "trashedAt" TIMESTAMP(3); +-- AlterTable Notebook +DO $$ BEGIN + ALTER TABLE "Notebook" ADD COLUMN "parentId" TEXT; +EXCEPTION WHEN duplicate_column THEN NULL; +END $$; --- CreateIndex -CREATE INDEX "Notebook_parentId_idx" ON "Notebook"("parentId"); +DO $$ BEGIN + ALTER TABLE "Notebook" ADD COLUMN "trashedAt" TIMESTAMP(3); +EXCEPTION WHEN duplicate_column THEN NULL; +END $$; --- CreateIndex -CREATE INDEX "Notebook_trashedAt_idx" ON "Notebook"("trashedAt"); +CREATE INDEX IF NOT EXISTS "Notebook_parentId_idx" ON "Notebook"("parentId"); +CREATE INDEX IF NOT EXISTS "Notebook_trashedAt_idx" ON "Notebook"("trashedAt"); --- AddForeignKey -ALTER TABLE "Notebook" ADD CONSTRAINT "Notebook_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Notebook"("id") ON DELETE CASCADE ON UPDATE CASCADE; +DO $$ BEGIN + ALTER TABLE "Notebook" ADD CONSTRAINT "Notebook_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Notebook"("id") ON DELETE CASCADE ON UPDATE CASCADE; +EXCEPTION + WHEN duplicate_object THEN NULL; + WHEN duplicate_table THEN NULL; +END $$;