From 1eb2005d04095e44f175508c2553d2298e8298aa Mon Sep 17 00:00:00 2001 From: MohammedMaaz Date: Sat, 26 Oct 2024 01:48:19 +0500 Subject: [PATCH] fixed migration completion script --- .github/workflows/dev-deploy.yml | 40 +++++++++++----- .github/workflows/migrate-dev-to-main.yml | 8 ++-- .github/workflows/migrate-main-to-dev.yml | 8 ++-- .github/workflows/prod-deploy.yml | 4 -- scripts/helpers.ts | 14 ++++++ scripts/migration-complete.ts | 57 +++++++++++++++++------ scripts/migration-start.ts | 3 ++ 7 files changed, 96 insertions(+), 38 deletions(-) diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index db8927d..2331d3f 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -13,18 +13,28 @@ jobs: steps: - uses: actions/checkout@v3 - # - name: Load Secrets in Env - # run: | - # npx tsx ./scripts/load-secrets - # env: - # NODE_ENV: dev - # INFISICAL_CLIENT_ID: ${{ secrets.INFISICAL_CLIENT_ID }} - # INFISICAL_CLIENT_SECRET: ${{ secrets.INFISICAL_CLIENT_SECRET }} - # INFISICAL_PROJECT_ID: ${{ secrets.INFISICAL_PROJECT_ID }} - - # - name: Env Replace in app.yaml - # run: | - # npx tsx ./scripts/env-replace + - name: Load Secrets in Env + run: | + npx tsx ./scripts/load-secrets + env: + NODE_ENV: dev + INFISICAL_CLIENT_ID: ${{ secrets.INFISICAL_CLIENT_ID }} + INFISICAL_CLIENT_SECRET: ${{ secrets.INFISICAL_CLIENT_SECRET }} + INFISICAL_PROJECT_ID: ${{ secrets.INFISICAL_PROJECT_ID }} + + - name: Env Replace in app.yaml + run: | + npx tsx ./scripts/env-replace + + - name: Install Xata CLI + run: | + npm install -g @xata.io/cli@latest + + - name: Start Xata Migration from main to dev + run: | + npx tsx ./scripts/migration-start main dev + env: + COMMIT_MSG: "${{ github.event.head_commit.message }}" # - id: "auth" # uses: "google-github-actions/auth@v2" @@ -37,3 +47,9 @@ jobs: # - name: Deploy to Google App Engine # run: | # gcloud app deploy --appyaml=app.yaml --quiet -v=live --no-cache + + - name: Complete Xata Migration from main to dev + run: | + npx tsx ./scripts/migration-complete main dev + env: + COMMIT_MSG: "${{ github.event.head_commit.message }}" diff --git a/.github/workflows/migrate-dev-to-main.yml b/.github/workflows/migrate-dev-to-main.yml index 0a2e1b9..c20d9fe 100644 --- a/.github/workflows/migrate-dev-to-main.yml +++ b/.github/workflows/migrate-dev-to-main.yml @@ -19,16 +19,16 @@ jobs: INFISICAL_CLIENT_SECRET: ${{ secrets.INFISICAL_CLIENT_SECRET }} INFISICAL_PROJECT_ID: ${{ secrets.INFISICAL_PROJECT_ID }} + - name: Install Xata CLI + run: | + npm install -g @xata.io/cli@latest + - name: Start Xata Migration from dev to main run: | npx tsx ./scripts/migration-start dev main env: COMMIT_MSG: "${{ github.event.head_commit.message }}" - - name: Sleep for 15 seconds - run: | - sleep 15 - - name: Complete Xata Migration from dev to main run: | npx tsx ./scripts/migration-complete dev main diff --git a/.github/workflows/migrate-main-to-dev.yml b/.github/workflows/migrate-main-to-dev.yml index 45ae6e5..98a5577 100644 --- a/.github/workflows/migrate-main-to-dev.yml +++ b/.github/workflows/migrate-main-to-dev.yml @@ -19,16 +19,16 @@ jobs: INFISICAL_CLIENT_SECRET: ${{ secrets.INFISICAL_CLIENT_SECRET }} INFISICAL_PROJECT_ID: ${{ secrets.INFISICAL_PROJECT_ID }} + - name: Install Xata CLI + run: | + npm install -g @xata.io/cli@latest + - name: Start Xata Migration from main to dev run: | npx tsx ./scripts/migration-start main dev env: COMMIT_MSG: "${{ github.event.head_commit.message }}" - - name: Sleep for 15 seconds - run: | - sleep 15 - - name: Complete Xata Migration from main to dev run: | npx tsx ./scripts/migration-complete main dev diff --git a/.github/workflows/prod-deploy.yml b/.github/workflows/prod-deploy.yml index 871dc90..32ac881 100644 --- a/.github/workflows/prod-deploy.yml +++ b/.github/workflows/prod-deploy.yml @@ -50,10 +50,6 @@ jobs: # run: | # gcloud app deploy --appyaml=app.yaml --quiet -v=live --no-cache - - name: Sleep for 15 seconds - run: | - sleep 15 - - name: Complete Xata Migration from dev to main run: | npx tsx ./scripts/migration-complete dev main diff --git a/scripts/helpers.ts b/scripts/helpers.ts index 933d452..8e791e1 100644 --- a/scripts/helpers.ts +++ b/scripts/helpers.ts @@ -95,3 +95,17 @@ export function isPRMerged(commitMsg: string, fromBranch: string) { "gm" ).test(commitMsg); } + +export function shortPoll(fn: () => any, interval: number) { + return new Promise(async (resolve) => { + while (true) { + const result = await fn(); + if (typeof result === "boolean") { + resolve(result); + break; + } + + await new Promise((resolve) => setTimeout(resolve, interval)); + } + }); +} diff --git a/scripts/migration-complete.ts b/scripts/migration-complete.ts index 02888ee..62df44a 100644 --- a/scripts/migration-complete.ts +++ b/scripts/migration-complete.ts @@ -1,12 +1,17 @@ import { execSync, spawnSync } from "child_process"; import fs from "fs"; -import { importSecrets, isPRMerged, updateSecret } from "./helpers"; +import { importSecrets, isPRMerged, shortPoll, updateSecret } from "./helpers"; importSecrets(); const base = process.argv[2] || "dev"; const target = process.argv[3] || "main"; -console.log(`migration completing from ${base} to ${target}`); +console.log(`starting migration completion from ${base} to ${target}`); + +function getMigrationStatus() { + const output = execSync(`xata migrate status ${target}`).toString(); + return output.trim().split("\n")[1].split(/\s+/)[3]; +} (async () => { if (!isPRMerged(process.env.COMMIT_MSG || "", base)) { @@ -14,8 +19,7 @@ console.log(`migration completing from ${base} to ${target}`); return; } - const output = execSync(`xata migrate status ${target}`).toString(); - const status = output.trim().split("\n")[1].split(/\s+/)[3]; + const status = getMigrationStatus(); console.log("status: ", status); if (status === "active") { @@ -23,16 +27,41 @@ console.log(`migration completing from ${base} to ${target}`); stdio: "inherit", shell: true, }); - - const baseLedger = fs - .readFileSync(`.xata/migrations-${base}/.ledger`, "utf-8") - .trim() - .split("\n"); - const newLastMigrationId = baseLedger.at(-1) || ""; - console.log("newLastMigrationId: ", newLastMigrationId); - await updateSecret("_LAST_MIGRATION_ID", newLastMigrationId, base); } - execSync(`rm -rf .xata/migrations-${base}`); - execSync(`rm -rf .xata/migrations-${target}`); + const isCompleted = await shortPoll(() => { + const status = getMigrationStatus(); + if (status === "completed") return true; + if (status === "active") return undefined; //continue polling + return false; + }, 3000); + + if (isCompleted) { + console.log(`migration completed from ${base} to ${target}`); + + execSync(`xata pull ${base}`); + execSync(`mv .xata/migrations .xata/migrations-${base}`); + + execSync(`xata pull ${target}`); + execSync(`mv .xata/migrations .xata/migrations-${target}`); + + async function updateLastMigrationId(branch: string) { + const ledger = fs + .readFileSync(`.xata/migrations-${branch}/.ledger`, "utf-8") + .trim() + .split("\n"); + const newLastMigrationId = ledger.at(-1) || ""; + console.log(`${base} newLastMigrationId: `, newLastMigrationId); + + await updateSecret("_LAST_MIGRATION_ID", newLastMigrationId, branch); + } + + await updateLastMigrationId(base); + await updateLastMigrationId(target); + + execSync(`rm -rf .xata/migrations-${base}`); + execSync(`rm -rf .xata/migrations-${target}`); + + console.log("updated _LAST_MIGRATION_ID for both branches"); + } })(); diff --git a/scripts/migration-start.ts b/scripts/migration-start.ts index f0044de..da84912 100644 --- a/scripts/migration-start.ts +++ b/scripts/migration-start.ts @@ -76,4 +76,7 @@ async function getMigrationDiff() { } ); } + + execSync(`rm -rf .xata/migrations-${base}`); + execSync(`rm -rf .xata/migrations-${target}`); })();