diff --git a/mise.toml b/mise.toml index 5a6555d1..d165f5f0 100644 --- a/mise.toml +++ b/mise.toml @@ -32,6 +32,14 @@ run = "cd internal/handlers && go generate" description = "Generate SQL code" run = "sqlc generate" +[tasks.db-up] +description = "Intialize and Migrate Database" +run = "scripts/db-up.sh" + +[tasks.db-down] +description = "Drop Database" +run = "scripts/db-down.sh" + [tasks.dev] description = "Start development environment" depends = [ @@ -45,6 +53,7 @@ depends = [ [tasks.setup] description = "Setup development environment" depends = [ + "//:db-up", "//:pnpm-install", "//:go-install", ] diff --git a/scripts/db-drop.sh b/scripts/db-drop.sh new file mode 100755 index 00000000..c2dbf59d --- /dev/null +++ b/scripts/db-drop.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +set -e + +PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +CONFIG_FILE="${PROJECT_ROOT}/config.toml" +MIGRATIONS_DIR="${PROJECT_ROOT}/db/migrations" + +GREEN='\033[0;32m' +RED='\033[0;31m' +YELLOW='\033[1;33m' +NC='\033[0m' + +if [ ! -f "$CONFIG_FILE" ]; then + echo -e "${RED}Error: Config file not found${NC}" + exit 1 +fi + +if [ ! -d "$MIGRATIONS_DIR" ]; then + echo -e "${RED}Error: Migrations directory not found${NC}" + exit 1 +fi + +parse_toml_value() { + local key=$1 + local section=$2 + grep -A 20 "^\[$section\]" "$CONFIG_FILE" | grep "^$key" | head -1 | sed 's/.*=[ ]*//' | tr -d '"' | tr -d "'" +} + +DB_HOST=$(parse_toml_value "host" "postgres") +DB_PORT=$(parse_toml_value "port" "postgres") +DB_USER=$(parse_toml_value "user" "postgres") +DB_PASSWORD=$(parse_toml_value "password" "postgres") +DB_NAME=$(parse_toml_value "database" "postgres") +DB_SSLMODE=$(parse_toml_value "sslmode" "postgres") + +if [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ] || [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then + echo -e "${RED}Error: Invalid database configuration${NC}" + exit 1 +fi + +DB_SSLMODE=${DB_SSLMODE:-disable} + +echo -e "${YELLOW}WARNING: This will drop all database tables!${NC}" +read -p "Type 'yes' to confirm: " confirmation + +if [ "$confirmation" != "yes" ]; then + echo "Cancelled" + exit 0 +fi + +export PGPASSWORD="$DB_PASSWORD" +PSQL_CMD="psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME" + +if ! $PSQL_CMD -c "SELECT 1;" > /dev/null 2>&1; then + echo -e "${RED}Error: Cannot connect to database${NC}" + exit 1 +fi + +for migration_file in $(ls -r "$MIGRATIONS_DIR"/*.down.sql 2>/dev/null); do + if [ -f "$migration_file" ]; then + if ! $PSQL_CMD -f "$migration_file" > /dev/null 2>&1; then + echo -e "${RED}Error: Drop failed - $(basename "$migration_file")${NC}" + exit 1 + fi + fi +done + +echo -e "${GREEN}✓ Database tables dropped${NC}" + +unset PGPASSWORD + diff --git a/scripts/db-up.sh b/scripts/db-up.sh new file mode 100755 index 00000000..99ef2434 --- /dev/null +++ b/scripts/db-up.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +set -e + +PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +CONFIG_FILE="${PROJECT_ROOT}/config.toml" +MIGRATIONS_DIR="${PROJECT_ROOT}/db/migrations" + +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' + +if [ ! -f "$CONFIG_FILE" ]; then + echo -e "${RED}Error: Config file not found${NC}" + exit 1 +fi + +if [ ! -d "$MIGRATIONS_DIR" ]; then + echo -e "${RED}Error: Migrations directory not found${NC}" + exit 1 +fi + +parse_toml_value() { + local key=$1 + local section=$2 + grep -A 20 "^\[$section\]" "$CONFIG_FILE" | grep "^$key" | head -1 | sed 's/.*=[ ]*//' | tr -d '"' | tr -d "'" +} + +DB_HOST=$(parse_toml_value "host" "postgres") +DB_PORT=$(parse_toml_value "port" "postgres") +DB_USER=$(parse_toml_value "user" "postgres") +DB_PASSWORD=$(parse_toml_value "password" "postgres") +DB_NAME=$(parse_toml_value "database" "postgres") +DB_SSLMODE=$(parse_toml_value "sslmode" "postgres") + +if [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ] || [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then + echo -e "${RED}Error: Invalid database configuration${NC}" + exit 1 +fi + +DB_SSLMODE=${DB_SSLMODE:-disable} + +export PGPASSWORD="$DB_PASSWORD" +PSQL_CMD="psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME" + +if ! $PSQL_CMD -c "SELECT 1;" > /dev/null 2>&1; then + echo -e "${RED}Error: Cannot connect to database${NC}" + exit 1 +fi + +for migration_file in "$MIGRATIONS_DIR"/*.up.sql; do + if [ -f "$migration_file" ]; then + if ! $PSQL_CMD -f "$migration_file" > /dev/null 2>&1; then + echo -e "${RED}Error: Migration failed - $(basename "$migration_file")${NC}" + exit 1 + fi + fi +done + +echo -e "${GREEN}✓ Database migration completed${NC}" + +unset PGPASSWORD +