-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(run-ops): automatically migrate the dedicated run-ops database #4150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: feature | ||
| --- | ||
|
|
||
| Automatically migrate the dedicated run-ops database on deploy (entrypoint + `@internal/run-ops-database` deploy/status scripts), and standardize the run-ops DB connection on a single `RUN_OPS_DATABASE_URL` family (dropping the `TASK_RUN_DATABASE_URL` aliases) so migrations always target the DB the app connects to. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Run Prisma migrations against the dedicated NEW run-ops database (the second physical DB in the | ||
| // split). It owns its own migration history, so it is migrated independently of the control-plane | ||
| // DB. Connects via RUN_OPS_DATABASE_URL — the same var the webapp uses — so migrations always | ||
| // target the DB the app connects to. | ||
| // | ||
| // Usage: node scripts/migrate.mjs [deploy|status] (defaults to deploy) | ||
| import { spawnSync } from "node:child_process"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { dirname, resolve } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); | ||
|
|
||
| // Read from local .env files so dev works without an exported env; deploy environments inject vars directly. | ||
| function readFromEnvFiles(key) { | ||
| for (const file of [resolve(packageRoot, ".env"), resolve(packageRoot, "../../.env")]) { | ||
| let contents; | ||
| try { | ||
| contents = readFileSync(file, "utf8"); | ||
| } catch { | ||
| continue; | ||
| } | ||
| for (const line of contents.split("\n")) { | ||
| const match = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*?)\s*$/); | ||
| if (!match || match[1] !== key) continue; | ||
| let value = match[2]; | ||
| if ( | ||
| (value.startsWith('"') && value.endsWith('"')) || | ||
| (value.startsWith("'") && value.endsWith("'")) | ||
| ) { | ||
| value = value.slice(1, -1); | ||
| } | ||
| if (value) return value; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Expand `${VAR}` refs in env-file values (our manual reader loads them literally, unlike Prisma's | ||
| // dotenv-expand), so a `.env` like RUN_OPS_DATABASE_URL=${DATABASE_URL} still resolves. | ||
| const expand = (value) => | ||
| value?.replace(/\$\{(\w+)\}/g, (_, k) => process.env[k] ?? readFromEnvFiles(k) ?? ""); | ||
| const resolveVar = (key) => expand(process.env[key] || readFromEnvFiles(key)); | ||
| const redact = (url) => url.replace(/:\/\/[^@]*@/, "://***@"); | ||
|
|
||
| const subcommand = process.argv[2] === "status" ? "status" : "deploy"; | ||
|
|
||
| const databaseUrl = resolveVar("RUN_OPS_DATABASE_URL"); | ||
|
|
||
| if (!databaseUrl) { | ||
| // Single-DB installs never set it — safe no-op. A genuinely-expected DB is gated on by the caller. | ||
| console.log( | ||
| `run-ops migrate ${subcommand}: RUN_OPS_DATABASE_URL is not set (checked env and .env). ` + | ||
| "No dedicated run-ops database configured — skipping." | ||
| ); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| console.log( | ||
| `Running \`prisma migrate ${subcommand}\` against the run-ops database (${redact(databaseUrl)})` | ||
| ); | ||
|
|
||
| const result = spawnSync("prisma", ["migrate", subcommand, "--schema", "prisma/schema.prisma"], { | ||
| cwd: packageRoot, | ||
| stdio: "inherit", | ||
| env: { | ||
| ...process.env, | ||
| RUN_OPS_DATABASE_URL: databaseUrl, | ||
| }, | ||
| }); | ||
|
|
||
| process.exit(result.status ?? 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.