Files
backupsidecar/generate_sql_test_data.sh
Timo Behrendt ed8121a7eb
Some checks failed
CI / Build Docker image (pull_request) Failing after 15s
sync
2025-09-04 20:20:01 +02:00

125 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# Script to generate test data for PostgreSQL backup testing
# This script creates two tables with a foreign key relationship and populates them with test data
# Database connection parameters
PGHOST="${PGHOST:-localhost}"
PGPORT="${PGPORT:-5432}"
PGDATABASE="${PGDATABASE:-testdb}"
PGUSER="${PGUSER:-testuser}"
PGPASSWORD="${PGPASSWORD:-testpass}"
# Export password for psql
export PGPASSWORD
echo "Generating test data for PostgreSQL database..."
# Create tables
echo "Creating tables..."
psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" << 'EOF'
-- Drop tables if they exist
DROP TABLE IF EXISTS orders CASCADE;
DROP TABLE IF EXISTS customers CASCADE;
-- Create customers table
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create orders table with foreign key to customers
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id) ON DELETE CASCADE,
product_name VARCHAR(100) NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
price DECIMAL(10,2) NOT NULL CHECK (price >= 0),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for better performance
CREATE INDEX idx_orders_customer_id ON orders(customer_id);
CREATE INDEX idx_customers_email ON customers(email);
CREATE INDEX idx_orders_order_date ON orders(order_date);
EOF
# Insert test data
echo "Inserting test data..."
psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" << 'EOF'
-- Insert customers
INSERT INTO customers (name, email) VALUES
('John Doe', 'john.doe@example.com'),
('Jane Smith', 'jane.smith@example.com'),
('Bob Johnson', 'bob.johnson@example.com'),
('Alice Brown', 'alice.brown@example.com'),
('Charlie Wilson', 'charlie.wilson@example.com'),
('Diana Davis', 'diana.davis@example.com'),
('Eve Miller', 'eve.miller@example.com'),
('Frank Garcia', 'frank.garcia@example.com'),
('Grace Lee', 'grace.lee@example.com'),
('Henry Taylor', 'henry.taylor@example.com');
-- Insert orders
INSERT INTO orders (customer_id, product_name, quantity, price) VALUES
(1, 'Laptop', 1, 999.99),
(1, 'Mouse', 2, 25.50),
(2, 'Keyboard', 1, 75.00),
(2, 'Monitor', 1, 299.99),
(3, 'Headphones', 1, 150.00),
(3, 'Webcam', 1, 89.99),
(4, 'Tablet', 1, 399.99),
(4, 'Stylus', 1, 49.99),
(5, 'Smartphone', 1, 699.99),
(5, 'Case', 1, 19.99),
(6, 'Desktop', 1, 1299.99),
(6, 'RAM', 2, 79.99),
(7, 'SSD', 1, 199.99),
(7, 'Graphics Card', 1, 599.99),
(8, 'Motherboard', 1, 199.99),
(8, 'CPU', 1, 399.99),
(9, 'Power Supply', 1, 149.99),
(9, 'Cooling Fan', 2, 29.99),
(10, 'Cable Set', 1, 39.99),
(10, 'USB Hub', 1, 24.99);
EOF
# Verify data
echo "Verifying test data..."
psql -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" << 'EOF'
-- Show table counts
SELECT 'customers' as table_name, COUNT(*) as row_count FROM customers
UNION ALL
SELECT 'orders' as table_name, COUNT(*) as row_count FROM orders;
-- Show sample data
SELECT 'Sample customers:' as info;
SELECT id, name, email FROM customers LIMIT 5;
SELECT 'Sample orders:' as info;
SELECT o.id, c.name as customer_name, o.product_name, o.quantity, o.price
FROM orders o
JOIN customers c ON o.customer_id = c.id
LIMIT 5;
-- Show foreign key relationship
SELECT 'Foreign key relationship check:' as info;
SELECT
c.name as customer_name,
COUNT(o.id) as order_count,
SUM(o.price * o.quantity) as total_spent
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name
ORDER BY total_spent DESC;
EOF
echo "Test data generation completed successfully!"
echo "Database contains:"
echo "- 10 customers"
echo "- 20 orders with foreign key relationships"
echo "- Various data types and constraints"