diff --git a/.github/workflows/db-migration-backwards-compatibility.yaml b/.github/workflows/db-migration-backwards-compatibility.yaml new file mode 100644 index 000000000..142a8af96 --- /dev/null +++ b/.github/workflows/db-migration-backwards-compatibility.yaml @@ -0,0 +1,213 @@ +name: DB migrations are backwards-compatible with main branch + +on: + push: + branches: + - main + - dev + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev' }} + +jobs: + check-migrations-changed: + name: Check if migrations changed + runs-on: ubuntu-latest + outputs: + migrations_changed: ${{ steps.check-diff.outputs.migrations_changed }} + steps: + - name: Checkout current branch + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Check for migration changes + id: check-diff + run: | + # Get the merge base with main + git fetch origin main + MERGE_BASE=$(git merge-base HEAD origin/main) + + # Check if there are any changes in the migrations folder + if git diff --quiet "$MERGE_BASE" HEAD -- apps/backend/prisma/migrations/; then + echo "No changes in migrations folder" + echo "migrations_changed=false" >> $GITHUB_OUTPUT + else + echo "Migrations have changed" + echo "migrations_changed=true" >> $GITHUB_OUTPUT + fi + + backwards-compatibility: + name: Test migrations with main branch code + needs: check-migrations-changed + if: needs.check-migrations-changed.outputs.migrations_changed == 'true' + runs-on: ubicloud-standard-8 + env: + NODE_ENV: test + STACK_ENABLE_HARDCODED_PASSKEY_CHALLENGE_FOR_TESTING: yes + STACK_DATABASE_CONNECTION_STRING: "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:8128/stackframe" + + steps: + # First, checkout the current branch to get its migrations + - name: Checkout current branch + uses: actions/checkout@v6 + with: + path: current-branch + + # Save the migrations folder from the current branch + - name: Save current branch migrations + run: | + mkdir -p saved-migrations + cp -r current-branch/apps/backend/prisma/migrations/* saved-migrations/ + + # Now checkout main branch + - name: Checkout main branch + uses: actions/checkout@v6 + with: + ref: main + path: main-branch + + # Replace main's migrations with current branch's migrations + - name: Replace migrations with current branch migrations + run: | + rm -rf main-branch/apps/backend/prisma/migrations/* + cp -r saved-migrations/* main-branch/apps/backend/prisma/migrations/ + + # Move main-branch to the root for the rest of the workflow + - name: Setup working directory + run: | + shopt -s dotglob + mv main-branch/* . + rm -rf main-branch current-branch saved-migrations + + - name: Setup Node.js + uses: actions/setup-node@v6 + with: + node-version: 22.x + + - name: Setup pnpm + uses: pnpm/action-setup@v4 + + # Start Docker Compose in the background + - name: Start Docker Compose in background + uses: JarvusInnovations/background-action@v1.0.7 + with: + run: docker compose -f docker/dependencies/docker.compose.yaml up --pull always -d & + wait-on: /dev/null + tail: true + wait-for: 3s + log-output-if: true + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Create .env.test.local file for apps/backend + run: cp apps/backend/.env.development apps/backend/.env.test.local + + - name: Create .env.test.local file for apps/dashboard + run: cp apps/dashboard/.env.development apps/dashboard/.env.test.local + + - name: Create .env.test.local file for apps/e2e + run: cp apps/e2e/.env.development apps/e2e/.env.test.local + + - name: Create .env.test.local file for docs + run: cp docs/.env.development docs/.env.test.local + + - name: Create .env.test.local file for examples/cjs-test + run: cp examples/cjs-test/.env.development examples/cjs-test/.env.test.local + + - name: Create .env.test.local file for examples/demo + run: cp examples/demo/.env.development examples/demo/.env.test.local + + - name: Create .env.test.local file for examples/docs-examples + run: cp examples/docs-examples/.env.development examples/docs-examples/.env.test.local + + - name: Create .env.test.local file for examples/e-commerce + run: cp examples/e-commerce/.env.development examples/e-commerce/.env.test.local + + - name: Create .env.test.local file for examples/middleware + run: cp examples/middleware/.env.development examples/middleware/.env.test.local + + - name: Create .env.test.local file for examples/supabase + run: cp examples/supabase/.env.development examples/supabase/.env.test.local + + - name: Create .env.test.local file for examples/convex + run: cp examples/convex/.env.development examples/convex/.env.test.local + + - name: Build + run: pnpm build + + - name: Wait on Postgres + run: pnpm run wait-until-postgres-is-ready:pg_isready + + - name: Wait on Inbucket + run: pnpx wait-on tcp:localhost:8129 + + - name: Wait on Svix + run: pnpx wait-on tcp:localhost:8113 + + - name: Initialize database + run: pnpm run db:init + + - name: Start stack-backend in background + uses: JarvusInnovations/background-action@v1.0.7 + with: + run: pnpm run start:backend --log-order=stream & + wait-on: | + http://localhost:8102 + tail: true + wait-for: 30s + log-output-if: true + + - name: Start stack-dashboard in background + uses: JarvusInnovations/background-action@v1.0.7 + with: + run: pnpm run start:dashboard --log-order=stream & + wait-on: | + http://localhost:8101 + tail: true + wait-for: 30s + log-output-if: true + + - name: Start mock-oauth-server in background + uses: JarvusInnovations/background-action@v1.0.7 + with: + run: pnpm run start:mock-oauth-server --log-order=stream & + wait-on: | + http://localhost:8102 + tail: true + wait-for: 30s + log-output-if: true + + - name: Start run-email-queue in background + uses: JarvusInnovations/background-action@v1.0.7 + with: + run: pnpm -C apps/backend run run-email-queue --log-order=stream & + wait-on: | + http://localhost:8102 + tail: true + wait-for: 30s + log-output-if: true + + - name: Wait 10 seconds + run: sleep 10 + + - name: Run tests + run: pnpm test + + - name: Print Docker Compose logs + if: always() + run: docker compose -f docker/dependencies/docker.compose.yaml logs + + # This job runs when migrations haven't changed, ensuring the workflow succeeds + skip-unchanged: + name: No migration changes (skipped) + needs: check-migrations-changed + if: needs.check-migrations-changed.outputs.migrations_changed == 'false' + runs-on: ubuntu-latest + steps: + - name: No migration changes detected + run: echo "No changes to migrations folder detected. Skipping backwards compatibility test." +