Skip to content

Instantly share code, notes, and snippets.

@darko-mesaros
Created July 1, 2025 14:01
Show Gist options
  • Save darko-mesaros/95d6c4f978dde95268711ef0c18d4ad6 to your computer and use it in GitHub Desktop.
Save darko-mesaros/95d6c4f978dde95268711ef0c18d4ad6 to your computer and use it in GitHub Desktop.
sqlx migration with Postgres and DSQL
# Database migration targets
.PHONY: migrate clean-migrations
# Default target
help:
@echo "Available targets:"
@echo " make migrate dsql - Run migrations for DSQL"
@echo " make migrate pgres - Run migrations for PostgreSQL"
@echo " make clean-migrations - Clean the migrations directory"
# Clean migrations directory
clean-migrations:
@echo "Cleaning migrations directory..."
@rm -rf migrations/*
@mkdir -p migrations
# Copy variant-specific migration files
copy-migrations-%: clean-migrations
@echo "Copying $* migration files..."
@if [ "$*" = "dsql" ]; then \
for file in mig_variants/DSQL_*; do \
if [ -f "$$file" ]; then \
basename="$$(basename "$$file")"; \
newname="$${basename#DSQL_}"; \
cp "$$file" "migrations/$$newname"; \
fi; \
done; \
if [ -z "$$(ls migrations/ 2>/dev/null)" ]; then \
echo "Warning: No DSQL_ files found in mig_variants/"; \
fi; \
elif [ "$*" = "pgres" ]; then \
for file in mig_variants/PGRES_*; do \
if [ -f "$$file" ]; then \
basename="$$(basename "$$file")"; \
newname="$${basename#PGRES_}"; \
cp "$$file" "migrations/$$newname"; \
fi; \
done; \
if [ -z "$$(ls migrations/ 2>/dev/null)" ]; then \
echo "Warning: No PGRES_ files found in mig_variants/"; \
fi; \
else \
echo "Error: Unknown variant '$*'. Use 'dsql' or 'pgres'"; \
exit 1; \
fi
@echo "Migration files copied:"
@ls -la migrations/ || echo "No files copied"
# Main migration target
migrate: copy-migrations-$(filter dsql pgres,$(MAKECMDGOALS))
@echo "Running sqlx migrate run..."
sqlx migrate run
# Handle the variant arguments
dsql pgres:
@# This target does nothing, it's just to capture the argument
# Prevent make from looking for files named 'dsql' or 'pgres'
dsql pgres: ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment