From 4e5bb30d9437a23f55de13604263856a4cda0894 Mon Sep 17 00:00:00 2001 From: jmgasper Date: Thu, 3 Sep 2026 13:15:04 +1000 Subject: [PATCH] fix: apply forum migrations before startup --- Dockerfile | 9 +++++++-- README.md | 2 ++ appStartUp.sh | 8 ++++++++ package.json | 2 +- pnpm-lock.yaml | 6 +++--- 5 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 appStartUp.sh diff --git a/Dockerfile b/Dockerfile index ee15d77..ae90b71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,8 @@ RUN DATABASE_URL="postgresql://user:password@localhost:5432/forums" pnpm prisma: && pnpm lint \ && pnpm build \ && rm -rf node_modules \ - && pnpm install --prod --frozen-lockfile --ignore-scripts + && pnpm install --prod --frozen-lockfile --ignore-scripts \ + && pnpm rebuild @prisma/engines FROM node:${NODE_VERSION}-alpine AS production @@ -52,7 +53,11 @@ WORKDIR /usr/src/app COPY --from=build --chown=node:node /usr/src/app/dist ./dist COPY --from=build --chown=node:node /usr/src/app/node_modules ./node_modules COPY --from=build --chown=node:node /usr/src/app/prisma/generated ./prisma/generated +COPY --from=build --chown=node:node /usr/src/app/prisma/migrations ./prisma/migrations +COPY --from=build --chown=node:node /usr/src/app/prisma/schema.prisma ./prisma/schema.prisma +COPY --from=build --chown=node:node /usr/src/app/prisma.config.ts ./prisma.config.ts COPY --from=build --chown=node:node /usr/src/app/package.json ./package.json +COPY --from=build --chown=node:node --chmod=0555 /usr/src/app/appStartUp.sh ./appStartUp.sh COPY --from=external-clients --chown=node:node /clients/challenge-api-v6/packages/challenge-prisma-client /usr/src/challenge-api-v6/packages/challenge-prisma-client COPY --from=external-clients --chown=node:node /clients/identity-api-v6/packages/identity-prisma-client /usr/src/identity-api-v6/packages/identity-prisma-client COPY --from=external-clients --chown=node:node /clients/member-api-v6/packages/member-prisma-client /usr/src/member-api-v6/packages/member-prisma-client @@ -61,4 +66,4 @@ COPY --from=external-clients --chown=node:node /clients/resource-api-v6/packages USER node EXPOSE 3000 -CMD ["node", "dist/main.js"] +CMD ["./appStartUp.sh"] diff --git a/README.md b/README.md index eeddda6..ce151ab 100644 --- a/README.md +++ b/README.md @@ -230,4 +230,6 @@ interrupted, wipe the target forums dataset and rerun the full import. The forums Prisma schema defines `Topic`, `Post`, `PostReaction`, `TopicClosure`, `TopicWatch`, `TopicReadState`, `MemberBan`, and `IpBan` in the dedicated `forums` schema. `PostReaction` uses a composite post/member key so each member has at most one thumbs-up or thumbs-down value per post; deleting a post cascades its reactions. Topics store an explicit lock state plus nullable lock timestamp and lock actor member id. Ban rows keep active and removed audit metadata; the migration enforces one active row per member or exact IP value and validates IP bans as single IPv4/IPv6 host values. `pnpm prisma:generate` emits the local client at `prisma/generated/client` and the reusable exported client at `packages/forums-prisma-client`. +The production image includes the Prisma schema, migration history, configuration, and CLI. Its startup script runs `prisma migrate deploy` before starting NestJS, so a deployment cannot begin serving requests against an older forums schema. Migration failure stops the container instead of leaving feature-dependent reads or writes partially available. + Topic creation is transactional: it creates the topic, starter post, closure rows, and, for human authors, author watch and read-state rows together. M2M topic creation uses the system author and skips member watch/read-state side effects. Topic deletion is soft deletion, and post deletion preserves the post row while setting content to null for placeholder reads. Topic summary reads use side-effect-free raw queries for topic lock state, visible-post counts, nullable latest visible activity, and unread derivation, then apply centralized forums policy filtering before pagination. diff --git a/appStartUp.sh b/appStartUp.sh new file mode 100644 index 0000000..26f17b1 --- /dev/null +++ b/appStartUp.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu + +printf '%s\n' 'Applying forums database migrations.' +./node_modules/.bin/prisma migrate deploy + +printf '%s\n' 'Starting Forums API v6.' +exec node dist/main.js diff --git a/package.json b/package.json index 2bba3da..ec7528b 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "express": "^5.1.0", "mysql2": "^3.14.3", "pg": "^8.16.3", + "prisma": "^7.9.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.2", "tc-bus-api-wrapper": "github:topcoder-platform/tc-bus-api-wrapper", @@ -51,7 +52,6 @@ "globals": "^16.3.0", "jest": "^29.7.0", "prettier": "^3.4.2", - "prisma": "^7.9.0", "supertest": "^7.0.0", "ts-jest": "^29.1.1", "ts-node": "^10.9.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ddd803..0f7146b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -75,6 +75,9 @@ importers: pg: specifier: ^8.16.3 version: 8.21.0 + prisma: + specifier: ^7.9.0 + version: 7.9.0(@types/react@19.2.17)(typescript@5.9.3) reflect-metadata: specifier: ^0.2.2 version: 0.2.2 @@ -133,9 +136,6 @@ importers: prettier: specifier: ^3.4.2 version: 3.8.3 - prisma: - specifier: ^7.9.0 - version: 7.9.0(@types/react@19.2.17)(typescript@5.9.3) supertest: specifier: ^7.0.0 version: 7.2.2(supports-color@8.1.1)