Files
gochat/scripts/migrate.sh
T
2026-06-04 15:44:48 +08:00

43 lines
1.2 KiB
Bash

#!/bin/bash
# GoChat Database Migration Script
# Reference: Chatwoot Rails db:migrate pattern
# Uses golang-migrate for SQL migrations
set -e
MIGRATIONS_DIR="./migrations"
DB_HOST="${POSTGRES_HOST:-localhost}"
DB_PORT="${POSTGRES_PORT:-5432}"
DB_NAME="${POSTGRES_DATABASE:-gochat_dev}"
DB_USER="${POSTGRES_USERNAME:-postgres}"
DB_PASS="${POSTGRES_PASSWORD:-}"
DSN="postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable"
case "$1" in
create)
echo "Creating database ${DB_NAME}..."
createdb -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" "${DB_NAME}" 2>/dev/null || echo "Database already exists"
;;
up)
echo "Running migrations up..."
migrate -path "${MIGRATIONS_DIR}" -database "${DSN}" up
;;
down)
echo "Running migrations down..."
migrate -path "${MIGRATIONS_DIR}" -database "${DSN}" down
;;
force)
echo "Force migration version..."
migrate -path "${MIGRATIONS_DIR}" -database "${DSN}" force "$2"
;;
version)
echo "Current migration version:"
migrate -path "${MIGRATIONS_DIR}" -database "${DSN}" version
;;
*)
echo "Usage: $0 {create|up|down|force|version}"
exit 1
;;
esac