Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions pkgs/core/scripts/atlas-verify-schemas-synced
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
#!/bin/bash
set -e

# Create a temporary directory for migrations
TEMP_DIR=$(mktemp -d)
TEMP_MIGRATIONS_DIR="${TEMP_DIR}/migrations"

# Function to cleanup on exit
cleanup() {
local exit_code=$?

# Clean up temporary directory
if [ -d "$TEMP_DIR" ]; then
rm -rf "$TEMP_DIR"
fi

# Clean up any existing Atlas dev container
docker rm -f atlas-dev-postgres-pgflow 2>/dev/null || true

exit $exit_code
}

# Set trap to cleanup on exit
trap cleanup EXIT

# Clean up any existing Atlas dev container to avoid "schema already exists" errors
# Container name from docker "postgres" "pgflow" block
docker rm -f atlas-dev-postgres-pgflow 2>/dev/null || true

# Copy current migrations to temp directory
echo "Creating temporary migrations directory..."
cp -r supabase/migrations "$TEMP_MIGRATIONS_DIR"

# First, ensure the baseline schema file is updated
echo "Running atlas-dump-realtime-schema..."
./scripts/atlas-dump-realtime-schema

# Use a simple fixed temporary name
temp_migration="temp_verify_schemas"

# Run atlas migrate diff and capture output
# Run atlas migrate diff using the temporary directory
echo "Running atlas migrate diff..."
output=$(atlas migrate diff --config file://atlas/atlas.hcl --env local "$temp_migration" 2>&1) || {
output=$(atlas migrate diff \
--config file://atlas/atlas.hcl \
--env local \
--dir "file://${TEMP_MIGRATIONS_DIR}" \
"$temp_migration" 2>&1) || {
echo "Atlas migrate diff command failed with error: $?"
echo "$output"
exit 1
Expand All @@ -24,12 +54,21 @@ output=$(atlas migrate diff --config file://atlas/atlas.hcl --env local "$temp_m
echo "Atlas migrate diff output:"
echo "$output"

# Check if migration file was created (indicates changes exist)
if ls supabase/migrations/*_"$temp_migration".sql 1> /dev/null 2>&1; then
# Check if migration file was created in temp directory (indicates changes exist)
if ls "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql 1> /dev/null 2>&1; then
echo "Found temporary migration file, schemas are not synced"
# Remove temporary migration file
rm supabase/migrations/*_"$temp_migration".sql
echo "Error: Schemas are not synced. Please update migrations or schemas."

# Optionally show what the diff would be
echo ""
echo "Generated migration diff:"
echo "------------------------"
cat "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql | head -50
if [ $(wc -l < "${TEMP_MIGRATIONS_DIR}"/*_"$temp_migration".sql) -gt 50 ]; then
echo "... (truncated)"
fi
echo "------------------------"

exit 1
else
# No migration created means schemas are synced
Expand Down
56 changes: 43 additions & 13 deletions pkgs/core/scripts/regenerate-temp-migration
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,36 @@ MIGRATIONS_DIR="${SCRIPT_DIR}/../supabase/migrations"

print_header "pgflow Temporary Migration Regenerator"

# Step 1: Find the newest migration containing "pgflow_temp_"
# Step 1: Check if schemas are already synced
print_info "Checking if schemas are in sync..."

# Run atlas-verify-schemas-synced and capture its exit code
set +e # Temporarily disable exit on error
"${SCRIPT_DIR}/atlas-verify-schemas-synced" > /dev/null 2>&1
SYNC_STATUS=$?
set -e # Re-enable exit on error

if [ $SYNC_STATUS -eq 0 ]; then
print_success "Schemas are already in sync!"
echo
print_info "No migration regeneration needed. The schemas in the database"
print_info "already match what's defined in the schema files."
echo
print_info "This typically means:"
print_info " • The temporary migration was already applied successfully"
print_info " • All schema changes are captured in existing migrations"
print_info " • No new changes have been made to schema files"
echo
print_info "If you've made changes to schema files, they should appear"
print_info "after running 'pnpm nx verify-schemas-synced core'."
echo
exit 0
fi

print_warning "Schemas are not in sync, proceeding with regeneration..."
echo

# Step 2: Find the newest migration containing "pgflow_temp_"
print_info "Searching for newest temporary migration..."

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

# Step 2: Remove the migration
print_header "Step 1: Removing temporary migration"
# Step 3: Remove the migration
print_header "Step 2: Removing temporary migration"
print_info "Removing: ${MIGRATION_BASENAME}"
rm "$NEWEST_MIGRATION"
print_success "Migration removed"

# Step 3: Rehash migrations
print_header "Step 2: Rehashing migrations"
# Step 4: Rehash migrations
print_header "Step 3: Rehashing migrations"
print_info "Running atlas-migrate-hash..."
cd "${SCRIPT_DIR}/.."
./scripts/atlas-migrate-hash --yes
print_success "Migrations rehashed"

# Step 4: Generate new migration
print_header "Step 3: Generating new migration"
# Step 5: Generate new migration
print_header "Step 4: Generating new migration"
print_info "Running atlas-migrate-diff with name: ${BOLD}${TEMP_NAME}${NC}"
./scripts/atlas-migrate-diff "$TEMP_NAME"
print_success "New migration generated"

# Step 5: Verify the migration
print_header "Step 4: Verifying migration"
# Step 6: Verify the migration
print_header "Step 5: Verifying migration"

# 5a: Verify migrations
echo
Expand Down
Loading