Skip to content

Commit e8a6a85

Browse files
committed
chore: improve verify-schemas-synced and regenerate-temp-migration
1 parent 2b27c54 commit e8a6a85

File tree

2 files changed

+88
-19
lines changed

2 files changed

+88
-19
lines changed

pkgs/core/scripts/atlas-verify-schemas-synced

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
#!/bin/bash
22
set -e
33

4+
# Create a temporary directory for migrations
5+
TEMP_DIR=$(mktemp -d)
6+
TEMP_MIGRATIONS_DIR="${TEMP_DIR}/migrations"
7+
8+
# Function to cleanup on exit
9+
cleanup() {
10+
local exit_code=$?
11+
12+
# Clean up temporary directory
13+
if [ -d "$TEMP_DIR" ]; then
14+
rm -rf "$TEMP_DIR"
15+
fi
16+
17+
# Clean up any existing Atlas dev container
18+
docker rm -f atlas-dev-postgres-pgflow 2>/dev/null || true
19+
20+
exit $exit_code
21+
}
22+
23+
# Set trap to cleanup on exit
24+
trap cleanup EXIT
25+
426
# Clean up any existing Atlas dev container to avoid "schema already exists" errors
527
# Container name from docker "postgres" "pgflow" block
628
docker rm -f atlas-dev-postgres-pgflow 2>/dev/null || true
729

30+
# Copy current migrations to temp directory
31+
echo "Creating temporary migrations directory..."
32+
cp -r supabase/migrations "$TEMP_MIGRATIONS_DIR"
33+
834
# First, ensure the baseline schema file is updated
935
echo "Running atlas-dump-realtime-schema..."
1036
./scripts/atlas-dump-realtime-schema
1137

1238
# Use a simple fixed temporary name
1339
temp_migration="temp_verify_schemas"
1440

15-
# Run atlas migrate diff and capture output
41+
# Run atlas migrate diff using the temporary directory
1642
echo "Running atlas migrate diff..."
17-
output=$(atlas migrate diff --config file://atlas/atlas.hcl --env local "$temp_migration" 2>&1) || {
43+
output=$(atlas migrate diff \
44+
--config file://atlas/atlas.hcl \
45+
--env local \
46+
--dir "file://${TEMP_MIGRATIONS_DIR}" \
47+
"$temp_migration" 2>&1) || {
1848
echo "Atlas migrate diff command failed with error: $?"
1949
echo "$output"
2050
exit 1
@@ -24,12 +54,21 @@ output=$(atlas migrate diff --config file://atlas/atlas.hcl --env local "$temp_m
2454
echo "Atlas migrate diff output:"
2555
echo "$output"
2656

27-
# Check if migration file was created (indicates changes exist)
28-
if ls supabase/migrations/*_"$temp_migration".sql 1> /dev/null 2>&1; then
57+
# Check if migration file was created in temp directory (indicates changes exist)
58+
if ls "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql 1> /dev/null 2>&1; then
2959
echo "Found temporary migration file, schemas are not synced"
30-
# Remove temporary migration file
31-
rm supabase/migrations/*_"$temp_migration".sql
3260
echo "Error: Schemas are not synced. Please update migrations or schemas."
61+
62+
# Optionally show what the diff would be
63+
echo ""
64+
echo "Generated migration diff:"
65+
echo "------------------------"
66+
cat "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql | head -50
67+
if [ $(wc -l < "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql) -gt 50 ]; then
68+
echo "... (truncated)"
69+
fi
70+
echo "------------------------"
71+
3372
exit 1
3473
else
3574
# No migration created means schemas are synced

pkgs/core/scripts/regenerate-temp-migration

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,36 @@ MIGRATIONS_DIR="${SCRIPT_DIR}/../supabase/migrations"
6969

7070
print_header "pgflow Temporary Migration Regenerator"
7171

72-
# Step 1: Find the newest migration containing "pgflow_temp_"
72+
# Step 1: Check if schemas are already synced
73+
print_info "Checking if schemas are in sync..."
74+
75+
# Run atlas-verify-schemas-synced and capture its exit code
76+
set +e # Temporarily disable exit on error
77+
"${SCRIPT_DIR}/atlas-verify-schemas-synced" > /dev/null 2>&1
78+
SYNC_STATUS=$?
79+
set -e # Re-enable exit on error
80+
81+
if [ $SYNC_STATUS -eq 0 ]; then
82+
print_success "Schemas are already in sync!"
83+
echo
84+
print_info "No migration regeneration needed. The schemas in the database"
85+
print_info "already match what's defined in the schema files."
86+
echo
87+
print_info "This typically means:"
88+
print_info " • The temporary migration was already applied successfully"
89+
print_info " • All schema changes are captured in existing migrations"
90+
print_info " • No new changes have been made to schema files"
91+
echo
92+
print_info "If you've made changes to schema files, they should appear"
93+
print_info "after running 'pnpm nx verify-schemas-synced core'."
94+
echo
95+
exit 0
96+
fi
97+
98+
print_warning "Schemas are not in sync, proceeding with regeneration..."
99+
echo
100+
101+
# Step 2: Find the newest migration containing "pgflow_temp_"
73102
print_info "Searching for newest temporary migration..."
74103

75104
# Find all migrations with pgflow_temp_ and get the newest one
@@ -99,10 +128,11 @@ print_info "Extracted migration name: ${BOLD}${TEMP_NAME}${NC}"
99128
# Show what will be done
100129
echo
101130
print_header "This script will perform the following actions:"
102-
echo -e " 1. ${RED}Remove${NC} migration: ${MIGRATION_BASENAME}"
103-
echo -e " 2. ${BLUE}Rehash${NC} migrations using atlas-migrate-hash"
104-
echo -e " 3. ${GREEN}Generate${NC} new migration: ${TEMP_NAME}"
105-
echo -e " 4. ${CYAN}Verify${NC} the migration by running:"
131+
echo -e " 1. ${YELLOW}Check${NC} if schemas are already in sync"
132+
echo -e " 2. ${RED}Remove${NC} migration: ${MIGRATION_BASENAME}"
133+
echo -e " 3. ${BLUE}Rehash${NC} migrations using atlas-migrate-hash"
134+
echo -e " 4. ${GREEN}Generate${NC} new migration: ${TEMP_NAME}"
135+
echo -e " 5. ${CYAN}Verify${NC} the migration by running:"
106136
echo -e " • pnpm nx verify-migrations core"
107137
echo -e " • pnpm nx gen-types core"
108138
echo -e " • pnpm nx test:pgtap core"
@@ -117,27 +147,27 @@ else
117147
print_info "Skipping confirmation (--yes flag provided)"
118148
fi
119149

120-
# Step 2: Remove the migration
121-
print_header "Step 1: Removing temporary migration"
150+
# Step 3: Remove the migration
151+
print_header "Step 2: Removing temporary migration"
122152
print_info "Removing: ${MIGRATION_BASENAME}"
123153
rm "$NEWEST_MIGRATION"
124154
print_success "Migration removed"
125155

126-
# Step 3: Rehash migrations
127-
print_header "Step 2: Rehashing migrations"
156+
# Step 4: Rehash migrations
157+
print_header "Step 3: Rehashing migrations"
128158
print_info "Running atlas-migrate-hash..."
129159
cd "${SCRIPT_DIR}/.."
130160
./scripts/atlas-migrate-hash --yes
131161
print_success "Migrations rehashed"
132162

133-
# Step 4: Generate new migration
134-
print_header "Step 3: Generating new migration"
163+
# Step 5: Generate new migration
164+
print_header "Step 4: Generating new migration"
135165
print_info "Running atlas-migrate-diff with name: ${BOLD}${TEMP_NAME}${NC}"
136166
./scripts/atlas-migrate-diff "$TEMP_NAME"
137167
print_success "New migration generated"
138168

139-
# Step 5: Verify the migration
140-
print_header "Step 4: Verifying migration"
169+
# Step 6: Verify the migration
170+
print_header "Step 5: Verifying migration"
141171

142172
# 5a: Verify migrations
143173
echo

0 commit comments

Comments
 (0)