From e5ad26c8e32f588d95b7bdd8f483ab6bdd531fe2 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 20 Jan 2025 17:49:06 +0100 Subject: [PATCH] add scripts to migrate the data --- scripts/init_payments_table.sh | 24 ++++++++++++++++++++++++ scripts/payment_migration.sql | 10 ++++++++++ 2 files changed, 34 insertions(+) create mode 100644 scripts/init_payments_table.sh create mode 100644 scripts/payment_migration.sql diff --git a/scripts/init_payments_table.sh b/scripts/init_payments_table.sh new file mode 100644 index 0000000..7513add --- /dev/null +++ b/scripts/init_payments_table.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +DB_NAME="your_database_name" +DB_USER="your_username" +DB_HOST="your_host" # e.g., localhost or an IP address +DB_PORT="your_port" # Default PostgreSQL port is 5432 +SQL_FILE="payment_migration.sql" # File containing the SQL script + +# Check if the SQL file exists +if [ ! -f "$SQL_FILE" ]; then + echo "Error: SQL file '$SQL_FILE' not found." + exit 1 +fi + +# Execute the SQL script +psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$SQL_FILE" + +# Check the result of the execution +if [ $? -eq 0 ]; then + echo "SQL script executed successfully." +else + echo "Error: Failed to execute SQL script." + exit 1 +fi diff --git a/scripts/payment_migration.sql b/scripts/payment_migration.sql new file mode 100644 index 0000000..dc0d99a --- /dev/null +++ b/scripts/payment_migration.sql @@ -0,0 +1,10 @@ +INSERT INTO payments (created_at, updated_at, deleted_at, booking_id, amount, payment_method) +SELECT MIN(i.created_at) AS created_at, -- Use the earliest created_at timestamp for this payment + MAX(i.updated_at) AS updated_at, -- Use the latest updated_at timestamp for this payment + i.deleted_at, -- Use the deleted_at timestamp from items + i.booking_id, -- The associated booking_id + SUM(i.price * i.quantity) AS amount, -- Calculate total amount from price * quantity + i.payment_method -- The payment method +FROM items i +WHERE i.deleted_at IS NULL -- Exclude soft-deleted items +GROUP BY i.booking_id, i.payment_method, i.deleted_at;