From 942522458f39be6e1e988c89badbc3dcb41de83a Mon Sep 17 00:00:00 2001 From: Finley Ghosh Date: Sun, 7 Dec 2025 16:13:40 +1100 Subject: [PATCH] Moving fully to goose migration --- .../migrations/002_create_quotes_tables.sql | 42 ++++++++++++ ... 003_add_reminders_disabled_to_quotes.sql} | 0 scripts/deploy/scripts/run_migrations.sh | 68 +++++++++++-------- 3 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 go/sql/migrations/002_create_quotes_tables.sql rename go/sql/migrations/{002_add_reminders_disabled_to_quotes.sql => 003_add_reminders_disabled_to_quotes.sql} (100%) diff --git a/go/sql/migrations/002_create_quotes_tables.sql b/go/sql/migrations/002_create_quotes_tables.sql new file mode 100644 index 00000000..65118d1a --- /dev/null +++ b/go/sql/migrations/002_create_quotes_tables.sql @@ -0,0 +1,42 @@ +-- +goose Up +-- cmc.quotes definition (matches existing database) + +CREATE TABLE IF NOT EXISTS `quotes` ( + `created` datetime NOT NULL, + `modified` datetime NOT NULL, + `id` int(11) NOT NULL AUTO_INCREMENT, + `enquiry_id` int(50) NOT NULL, + `currency_id` int(11) NOT NULL, + `revision` int(5) NOT NULL COMMENT 'limited at 5 digits. Really, you''re not going to have more revisions of a single quote than that', + `delivery_time` varchar(400) NOT NULL COMMENT 'estimated delivery time for quote', + `delivery_time_frame` varchar(100) NOT NULL, + `payment_terms` varchar(400) NOT NULL, + `days_valid` int(3) NOT NULL, + `date_issued` date NOT NULL, + `valid_until` date NOT NULL, + `reminders_disabled` tinyint(1) DEFAULT 0, + `reminders_disabled_at` datetime DEFAULT NULL, + `reminders_disabled_by` varchar(100) DEFAULT NULL, + `delivery_point` varchar(400) NOT NULL, + `exchange_rate` varchar(255) NOT NULL, + `customs_duty` varchar(255) NOT NULL, + `document_id` int(11) NOT NULL, + `commercial_comments` text DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=18245 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci; + +-- cmc.quote_reminders definition +CREATE TABLE IF NOT EXISTS `quote_reminders` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `quote_id` int(11) NOT NULL, + `reminder_type` int(3) NOT NULL COMMENT '1=1st, 2=2nd, 3=3rd reminder', + `date_sent` datetime NOT NULL, + `username` varchar(100) DEFAULT NULL COMMENT 'User who manually (re)sent the reminder', + PRIMARY KEY (`id`), + KEY `quote_id` (`quote_id`), + CONSTRAINT `quote_reminders_ibfk_1` FOREIGN KEY (`quote_id`) REFERENCES `quotes` (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci; + +-- +goose Down +DROP TABLE IF EXISTS `quote_reminders`; +DROP TABLE IF EXISTS `quotes`; diff --git a/go/sql/migrations/002_add_reminders_disabled_to_quotes.sql b/go/sql/migrations/003_add_reminders_disabled_to_quotes.sql similarity index 100% rename from go/sql/migrations/002_add_reminders_disabled_to_quotes.sql rename to go/sql/migrations/003_add_reminders_disabled_to_quotes.sql diff --git a/scripts/deploy/scripts/run_migrations.sh b/scripts/deploy/scripts/run_migrations.sh index d1c4215f..775e2b75 100755 --- a/scripts/deploy/scripts/run_migrations.sh +++ b/scripts/deploy/scripts/run_migrations.sh @@ -11,36 +11,50 @@ if [[ "$TARGET" != "stg" && "$TARGET" != "prod" ]]; then exit 1 fi -if [[ "$TARGET" == "prod" ]]; then - COMPOSE_FILE="docker-compose.prod.yml" - ENV_FILE="--env-file .env.prod" - WORK_DIR="cmc-sales-prod" - GO_SERVICE="cmc-prod-go" -else - COMPOSE_FILE="docker-compose.stg.yml" - ENV_FILE="" - WORK_DIR="cmc-sales-staging" - GO_SERVICE="cmc-stg-go" +# Check if goose is installed +if ! command -v goose &> /dev/null; then + echo "ERROR: goose is not installed." + echo "Install with: go install github.com/pressly/goose/v3/cmd/goose@latest" + exit 1 fi echo "Running goose migrations for $TARGET environment..." -SERVER="cmc" -ssh $SERVER \ - "COMPOSE_FILE='$COMPOSE_FILE' ENV_FILE='$ENV_FILE' WORK_DIR='$WORK_DIR' GO_SERVICE='$GO_SERVICE' bash -s" << 'ENDSSH' - set -e - cd /home/cmc/$WORK_DIR - echo "Running migrations in $WORK_DIR..." - - # Check if Go service is running - if ! docker ps --filter "name=$GO_SERVICE" --filter "status=running" --format "{{.Names}}" | grep -q "$GO_SERVICE"; then - echo "ERROR: Go service ($GO_SERVICE) is not running." - exit 1 - fi - - echo "Go service is running. Executing migrations..." - docker compose $ENV_FILE -f "$COMPOSE_FILE" exec -T "$GO_SERVICE" sh -c "cd /app && make migrate" - echo "Migrations completed successfully." -ENDSSH +# Set up SSH tunnel to database +if [[ "$TARGET" == "prod" ]]; then + DB_PORT=3307 + SSH_PORT=13307 +else + DB_PORT=3308 + SSH_PORT=13308 +fi + +# Create SSH tunnel +echo "Creating SSH tunnel to $TARGET database on localhost:$SSH_PORT..." +ssh -f -N -L $SSH_PORT:localhost:$DB_PORT cmc +sleep 2 + +# Load goose.env and override the port +GOOSE_ENV_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../go" && pwd)/goose.env" +if [ -f "$GOOSE_ENV_FILE" ]; then + echo "Loading configuration from goose.env..." + export $(grep -v '^#' "$GOOSE_ENV_FILE" | xargs) + # Override the port in GOOSE_DBSTRING + export GOOSE_DBSTRING=$(echo "$GOOSE_DBSTRING" | sed "s/:3306/:$SSH_PORT/") + # Make migration dir absolute + export GOOSE_MIGRATION_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../go" && pwd)/$GOOSE_MIGRATION_DIR" +else + echo "ERROR: goose.env file not found at $GOOSE_ENV_FILE" + pkill -f "ssh.*-L $SSH_PORT:localhost:$DB_PORT" || true + exit 1 +fi + +# Run goose migrations +echo "Running migrations from $GOOSE_MIGRATION_DIR..." +goose up + +# Close SSH tunnel +echo "Closing SSH tunnel..." +pkill -f "ssh.*-L $SSH_PORT:localhost:$DB_PORT" || true echo "Done."