Moving fully to goose migration
This commit is contained in:
parent
8de7fc675b
commit
942522458f
42
go/sql/migrations/002_create_quotes_tables.sql
Normal file
42
go/sql/migrations/002_create_quotes_tables.sql
Normal file
|
|
@ -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`;
|
||||||
|
|
@ -11,36 +11,50 @@ if [[ "$TARGET" != "stg" && "$TARGET" != "prod" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$TARGET" == "prod" ]]; then
|
# Check if goose is installed
|
||||||
COMPOSE_FILE="docker-compose.prod.yml"
|
if ! command -v goose &> /dev/null; then
|
||||||
ENV_FILE="--env-file .env.prod"
|
echo "ERROR: goose is not installed."
|
||||||
WORK_DIR="cmc-sales-prod"
|
echo "Install with: go install github.com/pressly/goose/v3/cmd/goose@latest"
|
||||||
GO_SERVICE="cmc-prod-go"
|
exit 1
|
||||||
else
|
|
||||||
COMPOSE_FILE="docker-compose.stg.yml"
|
|
||||||
ENV_FILE=""
|
|
||||||
WORK_DIR="cmc-sales-staging"
|
|
||||||
GO_SERVICE="cmc-stg-go"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Running goose migrations for $TARGET environment..."
|
echo "Running goose migrations for $TARGET environment..."
|
||||||
|
|
||||||
SERVER="cmc"
|
# Set up SSH tunnel to database
|
||||||
ssh $SERVER \
|
if [[ "$TARGET" == "prod" ]]; then
|
||||||
"COMPOSE_FILE='$COMPOSE_FILE' ENV_FILE='$ENV_FILE' WORK_DIR='$WORK_DIR' GO_SERVICE='$GO_SERVICE' bash -s" << 'ENDSSH'
|
DB_PORT=3307
|
||||||
set -e
|
SSH_PORT=13307
|
||||||
cd /home/cmc/$WORK_DIR
|
else
|
||||||
echo "Running migrations in $WORK_DIR..."
|
DB_PORT=3308
|
||||||
|
SSH_PORT=13308
|
||||||
# Check if Go service is running
|
fi
|
||||||
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."
|
# Create SSH tunnel
|
||||||
exit 1
|
echo "Creating SSH tunnel to $TARGET database on localhost:$SSH_PORT..."
|
||||||
fi
|
ssh -f -N -L $SSH_PORT:localhost:$DB_PORT cmc
|
||||||
|
sleep 2
|
||||||
echo "Go service is running. Executing migrations..."
|
|
||||||
docker compose $ENV_FILE -f "$COMPOSE_FILE" exec -T "$GO_SERVICE" sh -c "cd /app && make migrate"
|
# Load goose.env and override the port
|
||||||
echo "Migrations completed successfully."
|
GOOSE_ENV_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../go" && pwd)/goose.env"
|
||||||
ENDSSH
|
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."
|
echo "Done."
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue