-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_migration.sh
More file actions
113 lines (96 loc) · 2.79 KB
/
validate_migration.sh
File metadata and controls
113 lines (96 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
source db_config.sh
VALIDATION_DIR="migrations/validation"
MIGRATION_NAME="$1"
MIGRATION_BASENAME="${MIGRATION_NAME%.sql}"
if [ -z "$MIGRATION_NAME" ]; then
echo "Usage: $0 <migration_name>"
exit 1
fi
if [ "$MIGRATION_BASENAME" = "001_create_products" ]; then
VALIDATION_FILE="${VALIDATION_DIR}/001_validate_products.sql"
else
VALIDATION_FILE="${VALIDATION_DIR}/${MIGRATION_BASENAME}.sql"
fi
echo "=== Validation Suite for $MIGRATION_NAME ==="
validate_schema() {
echo "Checking schema objects..."
if [[ "$MIGRATION_BASENAME" == "001_create_products" ]]; then
psql -c "
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'products'
) AS products_table_exists;
"
psql -c "
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'products'
ORDER BY ordinal_position;
"
else
echo "No specific schema validation implemented for ${MIGRATION_NAME}."
fi
}
validate_constraints() {
echo "Checking constraints..."
if [[ "$MIGRATION_BASENAME" == "001_create_products" ]]; then
psql -c "
SELECT constraint_name, constraint_type
FROM information_schema.table_constraints
WHERE table_name = 'products'
ORDER BY constraint_name;
"
else
echo "No specific constraint validation implemented for ${MIGRATION_NAME}."
fi
}
validate_indexes() {
echo "Checking indexes..."
if [[ "$MIGRATION_BASENAME" == "001_create_products" ]]; then
psql -c "
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'products'
ORDER BY indexname;
"
else
echo "No specific index validation implemented for ${MIGRATION_NAME}."
fi
}
validate_data() {
echo "Checking data integrity..."
if [[ "$MIGRATION_BASENAME" == "001_create_products" ]]; then
psql -c "SELECT COUNT(*) AS product_count FROM products;"
psql -c "SELECT * FROM products ORDER BY id LIMIT 5;"
else
echo "No specific data validation implemented for ${MIGRATION_NAME}."
fi
}
validate_performance() {
echo "Checking basic query performance..."
if [[ "$MIGRATION_BASENAME" == "001_create_products" ]]; then
psql -c "EXPLAIN ANALYZE SELECT * FROM products WHERE sku = 'TEST-SKU-001';"
else
echo "No specific performance validation implemented for ${MIGRATION_NAME}."
fi
}
echo "1. Schema Validation..."
validate_schema
echo "2. Constraint Validation..."
validate_constraints
echo "3. Index Validation..."
validate_indexes
echo "4. Data Validation..."
validate_data
echo "5. Performance Validation..."
validate_performance
if [ -f "$VALIDATION_FILE" ]; then
echo ""
echo "Running SQL validation file: $VALIDATION_FILE"
psql -f "$VALIDATION_FILE"
fi
echo ""
echo "=== Validation Complete ==="