chore: add migration tooling
This commit is contained in:
Executable
+93
@@ -0,0 +1,93 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 up|down|force|version [argument]" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
if [ "$#" -lt 1 ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
command="$1"
|
||||
shift
|
||||
|
||||
case "$command" in
|
||||
up|down|force|version) ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
|
||||
repo_root="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
|
||||
migrations_dir="$repo_root/migrations"
|
||||
|
||||
env_file_value() {
|
||||
file="$1"
|
||||
key="$2"
|
||||
if [ ! -f "$file" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
awk -F= -v key="$key" '
|
||||
$1 == key {
|
||||
sub(/^[^=]*=/, "")
|
||||
gsub(/^[ \t047"]+|[ \t047"]+$/, "")
|
||||
print
|
||||
exit
|
||||
}
|
||||
' "$file"
|
||||
}
|
||||
|
||||
root_env="$repo_root/.env"
|
||||
supabase_env="$repo_root/deploy/supabase/.env"
|
||||
|
||||
env_value() {
|
||||
key="$1"
|
||||
eval "current=\${$key:-}"
|
||||
if [ -n "$current" ]; then
|
||||
printf '%s' "$current"
|
||||
return 0
|
||||
fi
|
||||
|
||||
value="$(env_file_value "$root_env" "$key")"
|
||||
if [ -n "$value" ]; then
|
||||
printf '%s' "$value"
|
||||
return 0
|
||||
fi
|
||||
|
||||
env_file_value "$supabase_env" "$key"
|
||||
}
|
||||
|
||||
pooler_tenant_id="$(env_value POOLER_TENANT_ID)"
|
||||
if [ -z "$pooler_tenant_id" ]; then
|
||||
pooler_tenant_id="proxui"
|
||||
fi
|
||||
|
||||
database_url="$(env_value MIGRATE_DATABASE_URL)"
|
||||
if [ -z "$database_url" ]; then
|
||||
database_url="$(env_value DATABASE_DIRECT_URL)"
|
||||
if [ -z "$database_url" ]; then
|
||||
postgres_password="$(env_value POSTGRES_PASSWORD)"
|
||||
fi
|
||||
|
||||
if [ -n "${postgres_password:-}" ]; then
|
||||
MIGRATE_DATABASE_URL="postgres://postgres.${pooler_tenant_id}:${postgres_password}@localhost:6543/postgres?sslmode=disable"
|
||||
else
|
||||
echo "MIGRATE_DATABASE_URL, DATABASE_DIRECT_URL or POSTGRES_PASSWORD must be set." >&2
|
||||
exit 1
|
||||
fi
|
||||
database_url="$MIGRATE_DATABASE_URL"
|
||||
fi
|
||||
|
||||
if command -v migrate >/dev/null 2>&1; then
|
||||
exec migrate -path "$migrations_dir" -database "$database_url" "$command" "$@"
|
||||
fi
|
||||
|
||||
docker_database_url="$(printf '%s' "$database_url" | sed 's/@localhost:/@host.docker.internal:/')"
|
||||
|
||||
exec docker run --rm \
|
||||
-v "$migrations_dir:/migrations:ro" \
|
||||
migrate/migrate:v4.18.3 \
|
||||
-path=/migrations \
|
||||
-database "$docker_database_url" \
|
||||
"$command" "$@"
|
||||
Reference in New Issue
Block a user