Skip to content

Commit

Permalink
Merge pull request #20 from rayonstudios/dev
Browse files Browse the repository at this point in the history
fixed migration completion script
  • Loading branch information
MohammedMaaz authored Oct 25, 2024
2 parents d78baaf + 1eb2005 commit 51f4fbc
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 38 deletions.
40 changes: 28 additions & 12 deletions .github/workflows/dev-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }}"
8 changes: 4 additions & 4 deletions .github/workflows/migrate-dev-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/migrate-main-to-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/prod-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions scripts/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ export function isPRMerged(commitMsg: string, fromBranch: string) {
"gm"
).test(commitMsg);
}

export function shortPoll(fn: () => any, interval: number) {
return new Promise<boolean>(async (resolve) => {
while (true) {
const result = await fn();
if (typeof result === "boolean") {
resolve(result);
break;
}

await new Promise((resolve) => setTimeout(resolve, interval));
}
});
}
57 changes: 43 additions & 14 deletions scripts/migration-complete.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
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)) {
console.log(`No PR merge detected from ${base} to ${target}. Exiting...`);
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") {
spawnSync(`xata migrate complete ${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");
}
})();
3 changes: 3 additions & 0 deletions scripts/migration-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ async function getMigrationDiff() {
}
);
}

execSync(`rm -rf .xata/migrations-${base}`);
execSync(`rm -rf .xata/migrations-${target}`);
})();

0 comments on commit 51f4fbc

Please sign in to comment.