-
Notifications
You must be signed in to change notification settings - Fork 15
refactor: improve Atlas migration management with better baseline handling #507
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
jumski
merged 1 commit into
main
from
12-05-refactor_atlas_baseline_to_use_fresh_hosted_supabase_schema
Dec 8, 2025
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| atlas/schema.sql | ||
| atlas/.supabase-baseline-schema.sql |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
| # Temp Supabase project for baseline dumping | ||
| supabase/ |
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,82 @@ | ||
| # Atlas Migration Management | ||
|
|
||
| This directory contains configuration for generating pgflow database migrations using [Atlas](https://atlasgo.io/). | ||
|
|
||
| ## Files | ||
|
|
||
| - `atlas.hcl` - Atlas configuration | ||
| - `Dockerfile` - Custom Postgres image with required extensions | ||
| - `supabase-baseline-schema.sql` - Schema baseline representing a fresh hosted Supabase project | ||
| - `fresh-extensions.txt` - Record of extensions in fresh Supabase (for reference) | ||
| - `dump-fresh-baseline.sh` - Script to regenerate baseline | ||
|
|
||
| ## Docker Image | ||
|
|
||
| The `Dockerfile` builds a custom Postgres 17 image with extensions matching hosted Supabase. | ||
|
|
||
| **Reference:** [Supabase Postgres 17.6.1.054 Dockerfile](https://github.com/supabase/postgres/blob/17.6.1.054/Dockerfile-17) | ||
|
|
||
| ### Installed Extensions | ||
|
|
||
| | Extension | Git Tag | Ext Version | Notes | | ||
| |-----------|---------|-------------|-------| | ||
| | pgmq | v1.5.1 | 1.5.1 | pgflow dependency (not in Supabase image) | | ||
| | pg_cron | v1.6.4 | 1.6 | Scheduled jobs (v1.6.3+ for PG17) | | ||
| | pg_net | v0.7.1 | 0.20.2 | HTTP requests from SQL | | ||
| | pgsodium | v3.1.6 | 3.1.9 | Cryptography primitives | | ||
| | supabase_vault | v0.2.8 | 0.3.1 | Secrets management (depends on pgsodium) | | ||
|
|
||
| ### Building the Image | ||
|
|
||
| ```bash | ||
| cd pkgs/core | ||
| ./scripts/build-atlas-postgres-image | ||
| ./scripts/push-atlas-postgres-image | ||
| ``` | ||
|
|
||
| ## Baseline Schema | ||
|
|
||
| The baseline represents what a **fresh hosted Supabase project** looks like before pgflow migrations. | ||
|
|
||
| ### Pre-installed Extensions (hosted Supabase) | ||
|
|
||
| | Extension | Status | | ||
| |-----------|--------| | ||
| | `supabase_vault` | Pre-installed | | ||
| | `pgmq` | Available, not installed | | ||
| | `pg_cron` | Available, not installed | | ||
| | `pg_net` | Available, not installed | | ||
|
|
||
| Our migrations must `CREATE EXTENSION` for pgmq, pg_cron, and pg_net. | ||
|
|
||
| ### Local CLI Difference | ||
|
|
||
| The local Supabase CLI pre-installs `pg_net`, but hosted Supabase does not. The `dump-fresh-baseline.sh` script accounts for this by dropping `pg_net` before dumping to match hosted behavior. | ||
|
|
||
| ## Regenerating the Baseline | ||
|
|
||
| Run this when: | ||
| - Supabase CLI version changes significantly | ||
| - Adding new Supabase-provided schemas to baseline | ||
|
|
||
| ```bash | ||
| cd pkgs/core/atlas | ||
| ./dump-fresh-baseline.sh | ||
| ``` | ||
|
|
||
| The script will: | ||
| 1. Create a fresh Supabase project in `supabase/` (temp, gitignored) | ||
| 2. Drop `pg_net` to match hosted Supabase defaults | ||
| 3. Verify extension assumptions match hosted Supabase | ||
| 4. Dump schema and extension list | ||
| 5. Clean up temp project | ||
|
|
||
| ## Generating Migrations | ||
|
|
||
| After updating SQL in `supabase/migrations/`, run from `pkgs/core/`: | ||
|
|
||
| ```bash | ||
| pnpm nx atlas:migrate:diff core --name="description_of_change" | ||
| ``` | ||
|
|
||
| This compares the current schema against the baseline + existing migrations. | ||
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,118 @@ | ||
| #!/bin/bash | ||
| # Dump baseline schema from a fresh Supabase instance | ||
| # | ||
| # This script: | ||
| # 1. Creates a fresh Supabase project (no pgflow) | ||
| # 2. Verifies extension availability matches our assumptions | ||
| # 3. Dumps the schema as our baseline | ||
| # 4. Cleans up | ||
| # | ||
| # Run this when: | ||
| # - Supabase CLI version changes | ||
| # - Adding new Supabase-provided schemas to baseline | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| cd "$SCRIPT_DIR" | ||
|
|
||
| # Use npx to run supabase directly in this directory (not via pnpm which runs from pkgs/core) | ||
| SUPA="npx supabase" | ||
|
|
||
| echo "=== Cleaning up any previous temp project ===" | ||
| rm -rf supabase/ || true | ||
|
|
||
| echo "=== Initializing fresh Supabase project ===" | ||
| $SUPA init --force | ||
|
|
||
| echo "=== Stopping any running Supabase instances ===" | ||
| $SUPA stop --all || true | ||
|
|
||
| echo "=== Starting fresh Supabase ===" | ||
| $SUPA start | ||
|
|
||
| echo "=== Getting database URL ===" | ||
| DB_URL=$($SUPA status --output json | jq -r '.DB_URL') | ||
| echo "DB_URL: $DB_URL" | ||
|
|
||
| echo "=== Matching hosted Supabase defaults ===" | ||
| # Local CLI pre-installs pg_net, but hosted Supabase doesn't | ||
| # Drop it to match hosted behavior (our migrations will create it) | ||
| echo "Dropping pg_net to match hosted Supabase defaults..." | ||
| psql "$DB_URL" -c "DROP EXTENSION IF EXISTS pg_net CASCADE;" 2>/dev/null || true | ||
|
|
||
| echo "=== Verifying extension assumptions ===" | ||
| # Query extension status (using --no-psqlrc to avoid extra output) | ||
| EXTENSION_STATUS=$(PAGER='' psql "$DB_URL" --no-psqlrc -t -A -F'|' -c " | ||
| SELECT | ||
| name, | ||
| CASE WHEN installed_version IS NOT NULL THEN 'installed' ELSE 'available' END as status | ||
| FROM pg_available_extensions | ||
| WHERE name IN ('pgmq', 'pg_cron', 'pg_net', 'supabase_vault') | ||
| ORDER BY name; | ||
| ") | ||
|
|
||
| echo "Extension status:" | ||
| echo "$EXTENSION_STATUS" | ||
|
|
||
| # Expected state matching HOSTED Supabase (not local CLI) | ||
| # - supabase_vault: pre-installed | ||
| # - pgmq, pg_cron, pg_net: available but NOT installed | ||
| EXPECTED="pg_cron|available | ||
| pg_net|available | ||
| pgmq|available | ||
| supabase_vault|installed" | ||
|
|
||
| if [ "$EXTENSION_STATUS" != "$EXPECTED" ]; then | ||
| echo "" | ||
| echo "ERROR: Extension status does not match hosted Supabase assumptions!" | ||
| echo "" | ||
| echo "Expected (matching hosted Supabase):" | ||
| echo "$EXPECTED" | ||
| echo "" | ||
| echo "Got:" | ||
| echo "$EXTENSION_STATUS" | ||
| echo "" | ||
| echo "If hosted Supabase changed defaults, update this script's expectations." | ||
| $SUPA stop | ||
| rm -rf supabase/ | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "" | ||
| echo "Extension assumptions verified (matches hosted Supabase)!" | ||
|
|
||
| echo "=== Dumping full \\dx output ===" | ||
| psql "$DB_URL" -c '\dx' > fresh-extensions.txt | ||
| echo "Saved to fresh-extensions.txt" | ||
|
|
||
| echo "=== Dumping baseline schema ===" | ||
| # Only dump schemas we need: | ||
| # - realtime: we reference realtime.send() | ||
| # - vault: supabase_vault is pre-installed here | ||
| # Skip extensions schema - it contains Supabase internal event triggers | ||
| # that reference roles not available in Atlas dev container | ||
| atlas schema inspect \ | ||
| --schema realtime,vault \ | ||
jumski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -u "$DB_URL?sslmode=disable" \ | ||
| --format "{{ sql . }}" > supabase-baseline-schema.sql | ||
graphite-app[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Strip VERSION strings (they change between Supabase versions) | ||
| sed -i 's/ VERSION "[^"]*"//g' supabase-baseline-schema.sql | ||
|
|
||
| # Strip date-specific partitions (they change daily, we don't reference them) | ||
| # e.g., messages_2025_12_04, messages_2025_12_05, etc. | ||
| sed -i '/messages_20[0-9]\{2\}_[0-9]\{2\}_[0-9]\{2\}/d' supabase-baseline-schema.sql | ||
jumski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| echo "Saved to supabase-baseline-schema.sql" | ||
|
|
||
| echo "=== Stopping Supabase ===" | ||
| $SUPA stop | ||
|
|
||
| echo "=== Cleaning up temp project ===" | ||
| rm -rf supabase/ | ||
|
|
||
| echo "" | ||
| echo "=== Done! ===" | ||
| echo "Baseline regenerated from fresh Supabase." | ||
| echo "Commit the updated supabase-baseline-schema.sql and fresh-extensions.txt" | ||
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,13 @@ | ||
| Null display is "[NULL]". | ||
| Expanded display is used automatically. | ||
| List of installed extensions | ||
| Name | Version | Schema | Description | ||
| --------------------+---------+------------+------------------------------------------------------------------------ | ||
| pg_graphql | 1.5.11 | graphql | pg_graphql: GraphQL support | ||
| pg_stat_statements | 1.11 | extensions | track planning and execution statistics of all SQL statements executed | ||
| pgcrypto | 1.3 | extensions | cryptographic functions | ||
| plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language | ||
| supabase_vault | 0.3.1 | vault | Supabase Vault Extension | ||
| uuid-ossp | 1.1 | extensions | generate universally unique identifiers (UUIDs) | ||
| (6 rows) | ||
|
|
Oops, something went wrong.
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.