add scripts to migrate the data

This commit is contained in:
Ruidy 2025-01-20 17:49:06 +01:00
parent b8d5907f36
commit e5ad26c8e3
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 34 additions and 0 deletions

View file

@ -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

View file

@ -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;