-
Notifications
You must be signed in to change notification settings - Fork 485
feat(cli): add declarative schema apply command #5694
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
Open
ametel01
wants to merge
6
commits into
supabase:develop
Choose a base branch
from
ametel01:feat/declarative-apply-local
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e74f3aa
feat(cli): add declarative schema apply command
ametel01 18966bf
fix(cli): flush telemetry for declarative apply
ametel01 35ad0a4
Merge branch 'develop' into feat/declarative-apply-local
ametel01 6c405ae
fix(cli): parse pg-delta apply failures
ametel01 41971b4
Merge branch 'develop' into feat/declarative-apply-local
ametel01 dc85486
fix(cli): preserve declarative apply diagnostics
ametel01 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
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,7 @@ | ||
| ## supabase-db-schema-declarative-apply | ||
|
|
||
| Apply declarative schema files directly to the local database. | ||
|
|
||
| Reads SQL files from the configured declarative schema directory and applies them to the local database using pg-delta without creating a timestamped migration. This is intended for local or CI bootstrapping, not as a replacement for migrations in controlled schema evolution. | ||
|
|
||
| Requires `--experimental` flag or `[experimental.pgdelta] enabled = true` in config. |
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
79 changes: 79 additions & 0 deletions
79
apps/cli/src/legacy/commands/db/schema/declarative/apply/SIDE_EFFECTS.md
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,79 @@ | ||
| # `supabase db schema declarative apply` | ||
|
|
||
| Applies existing declarative schema files directly to the local database using | ||
| pg-delta. It does not create a timestamped migration file and does not update | ||
| local migration history. | ||
|
|
||
| ## Files Read | ||
|
|
||
| | Path | Format | When | | ||
| | --------------------------------------------------------------------------------------------------------------------------- | ---------- | -------------------------------------------------- | | ||
| | `<workdir>/supabase/config.toml` | TOML | always — pg-delta gate, local DB port/password | | ||
| | `<workdir>/supabase/.temp/pgdelta-version` | plain text | always — pins the `@supabase/pg-delta` npm version | | ||
| | `<workdir>/supabase/.temp/edge-runtime-version` | plain text | always — pins the edge-runtime image tag | | ||
| | `<workdir>/supabase/database/**/*.sql` (declarative dir; configurable via `[experimental.pgdelta] declarative_schema_path`) | SQL | always — must exist and is mounted read-only | | ||
|
|
||
| ## Files Written | ||
|
|
||
| | Path | Format | When | | ||
| | ---------------------------- | ------ | ----------------------------------------------- | | ||
| | `~/.supabase/telemetry.json` | JSON | always (in `Effect.ensuring`) at end of command | | ||
|
|
||
| This command does **not** write `supabase/migrations/*.sql` and does **not** | ||
| update migration history. | ||
|
|
||
| ## Subprocesses / Containers | ||
|
|
||
| | Process | Condition | | ||
| | --------------------------------------------------------------------------------------------------- | ------------------------------------------------ | | ||
| | `supabase-go db start` via the declarative seam | when the local database container is not running | | ||
| | Edge-runtime container (`supabase/edge-runtime`) running the pg-delta declarative-apply Deno script | always after validation | | ||
|
|
||
| ## API Routes | ||
|
|
||
| None. | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| | Variable | Purpose | Required? | | ||
| | ---------------------------- | ------------------------------------------------ | --------- | | ||
| | `PGDELTA_NPM_REGISTRY` | private `@supabase` npm registry for pg-delta | no | | ||
| | `PGDELTA_DEBUG` | verbose pg-delta diagnostics | no | | ||
| | `SUPABASE_GO_BINARY` | override the `supabase-go` seam binary | no | | ||
| | `SUPABASE_SERVICES_HOSTNAME` | local DB host (Go `GetHostname`) | no | | ||
| | `DOCKER_HOST` | tcp daemon host used as the local DB host backup | no | | ||
|
|
||
| ## Exit Codes | ||
|
|
||
| | Exit | Meaning | | ||
| | ---- | ------------------------------------------------------------------------------------------------- | | ||
| | `0` | declarative schema applied successfully | | ||
| | `1` | pg-delta disabled; no declarative files found; local database start failed; pg-delta apply failed | | ||
|
|
||
| ## Telemetry Events Fired | ||
|
|
||
| | Event | When | Notable properties / groups | | ||
| | ---------------------- | ------------------------------------------ | ----------------------------------- | | ||
| | `cli_command_executed` | post-run, success or failure (via wrapper) | `exit_code`, `duration_ms`, `flags` | | ||
|
|
||
| ## Output | ||
|
|
||
| Text mode only; the command has no command-specific machine envelope. Global | ||
| `--output-format json` / `stream-json` error handling still emits the standard | ||
| wrapper error format on failures, but successful progress output remains stderr | ||
| text. | ||
|
|
||
| ### `--output-format text` (Go CLI compatible) | ||
|
|
||
| Progress output is written to stderr: | ||
|
|
||
| - `Applying declarative schemas via pg-delta...` | ||
| - `Applied <n> statements in <r> round(s).` | ||
|
|
||
| Apply failures include pg-delta's structured status summary before returning an | ||
| error. | ||
|
|
||
| ### `--output-format json` / `stream-json` | ||
|
|
||
| No success payload is emitted. Successful output remains the stderr text above. | ||
| On failure, the shared output wrapper emits its normal JSON / stream-json error. |
33 changes: 33 additions & 0 deletions
33
apps/cli/src/legacy/commands/db/schema/declarative/apply/apply.command.ts
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,33 @@ | ||
| import { Effect } from "effect"; | ||
| import { Command } from "effect/unstable/cli"; | ||
| import type * as CliCommand from "effect/unstable/cli/Command"; | ||
|
|
||
| import { withJsonErrorHandling } from "../../../../../../shared/output/json-error-handling.ts"; | ||
| import { withLegacyCommandInstrumentation } from "../../../../../telemetry/legacy-command-instrumentation.ts"; | ||
| import { legacyDbSchemaDeclarativeSharedBase } from "../declarative.shared.ts"; | ||
| import { legacyDbSchemaDeclarativeApply } from "./apply.handler.ts"; | ||
| import { legacyDbSchemaDeclarativeApplyRuntimeLayer } from "./apply.layers.ts"; | ||
|
|
||
| const config = {} as const; | ||
|
|
||
| export type LegacyDbSchemaDeclarativeApplyFlags = CliCommand.Command.Config.Infer<typeof config> & { | ||
| readonly noCache: boolean; | ||
| }; | ||
|
|
||
| export const legacyDbSchemaDeclarativeApplyCommand = Command.make("apply", config).pipe( | ||
| Command.withDescription("Apply declarative schema to the local database."), | ||
| Command.withShortDescription("Apply declarative schema to the local database"), | ||
| Command.withHandler((flags) => | ||
| Effect.gen(function* () { | ||
| const shared = yield* legacyDbSchemaDeclarativeSharedBase; | ||
| const merged: LegacyDbSchemaDeclarativeApplyFlags = { ...flags, noCache: shared.noCache }; | ||
| return yield* legacyDbSchemaDeclarativeApply(merged).pipe( | ||
| withLegacyCommandInstrumentation({ | ||
| flags: { "no-cache": merged.noCache }, | ||
| }), | ||
| withJsonErrorHandling, | ||
| ); | ||
| }), | ||
| ), | ||
| Command.provide(legacyDbSchemaDeclarativeApplyRuntimeLayer), | ||
| ); |
Oops, something went wrong.
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.