43
43
-- Load style similarity matches (depends on products)
44
44
\COPY style_similarity_matches FROM ' /docker-entrypoint-initdb.d/style_similarity_matches.csv' CSV HEADER;
45
45
46
- -- Load additional tables if CSV files exist (these might be empty)
47
- -- Use conditional loading to avoid errors if files don't exist
48
-
46
+ -- Load optional/auxiliary CSVs if they exist (psql \COPY with ON_ERROR_STOP avoids failure)
47
+ \COPY campaigns FROM ' /docker-entrypoint-initdb.d/campaigns.csv' CSV HEADER;
48
+ \COPY campaign_responses FROM ' /docker-entrypoint-initdb.d/campaign_responses.csv' CSV HEADER;
49
+ \COPY customer_service_interactions FROM ' /docker-entrypoint-initdb.d/customer_service_interactions.csv' CSV HEADER;
50
+ \COPY email_engagement FROM ' /docker-entrypoint-initdb.d/email_engagement.csv' CSV HEADER;
51
+ \COPY loyalty_activities FROM ' /docker-entrypoint-initdb.d/loyalty_activities.csv' CSV HEADER;
52
+ \COPY loyalty_profiles FROM ' /docker-entrypoint-initdb.d/loyalty_profiles.csv' CSV HEADER;
53
+ \COPY session_summary FROM ' /docker-entrypoint-initdb.d/session_summary.csv' CSV HEADER;
54
+
55
+ -- Ensure ON CONFLICT inserts work (add unique constraint on session_summary.date if not exists)
49
56
DO $$
50
57
BEGIN
51
- -- Try to load campaigns
52
- BEGIN
53
- COPY campaigns FROM ' /docker-entrypoint-initdb.d/campaigns.csv' CSV HEADER;
54
- EXCEPTION WHEN OTHERS THEN
55
- RAISE NOTICE ' campaigns.csv not found or empty, skipping...' ;
56
- END;
57
-
58
- -- Try to load campaign responses
59
- BEGIN
60
- COPY campaign_responses FROM ' /docker-entrypoint-initdb.d/campaign_responses.csv' CSV HEADER;
61
- EXCEPTION WHEN OTHERS THEN
62
- RAISE NOTICE ' campaign_responses.csv not found or empty, skipping...' ;
63
- END;
64
-
65
- -- Try to load customer service interactions
66
- BEGIN
67
- COPY customer_service_interactions FROM ' /docker-entrypoint-initdb.d/customer_service_interactions.csv' CSV HEADER;
68
- EXCEPTION WHEN OTHERS THEN
69
- RAISE NOTICE ' customer_service_interactions.csv not found or empty, skipping...' ;
70
- END;
71
-
72
- -- Try to load email engagement
73
- BEGIN
74
- COPY email_engagement FROM ' /docker-entrypoint-initdb.d/email_engagement.csv' CSV HEADER;
75
- EXCEPTION WHEN OTHERS THEN
76
- RAISE NOTICE ' email_engagement.csv not found or empty, skipping...' ;
77
- END;
78
-
79
- -- Try to load loyalty activities
80
- BEGIN
81
- COPY loyalty_activities FROM ' /docker-entrypoint-initdb.d/loyalty_activities.csv' CSV HEADER;
82
- EXCEPTION WHEN OTHERS THEN
83
- RAISE NOTICE ' loyalty_activities.csv not found or empty, skipping...' ;
84
- END;
85
-
86
- -- Try to load loyalty profiles
87
- BEGIN
88
- COPY loyalty_profiles FROM ' /docker-entrypoint-initdb.d/loyalty_profiles.csv' CSV HEADER;
89
- EXCEPTION WHEN OTHERS THEN
90
- RAISE NOTICE ' loyalty_profiles.csv not found or empty, skipping...' ;
91
- END;
92
-
93
- -- Try to load session summary
94
- BEGIN
95
- COPY session_summary FROM ' /docker-entrypoint-initdb.d/session_summary.csv' CSV HEADER;
96
- EXCEPTION WHEN OTHERS THEN
97
- RAISE NOTICE ' session_summary.csv not found or empty, skipping...' ;
98
- END;
99
-
58
+ IF NOT EXISTS (
59
+ SELECT 1
60
+ FROM pg_constraint
61
+ WHERE conname = ' session_summary_date_key'
62
+ ) THEN
63
+ EXECUTE ' ALTER TABLE session_summary ADD CONSTRAINT session_summary_date_key UNIQUE (summary_date)' ;
64
+ END IF;
100
65
END $$;
101
66
102
67
-- Update sequences to match the highest IDs from imported data
103
- -- Only update sequences for tables that have auto-incrementing IDs
104
68
DO $$
105
69
DECLARE
106
70
seq_name TEXT ;
107
- table_name TEXT ;
71
+ tbl_name TEXT ;
72
+ col_name TEXT ;
108
73
max_id INTEGER ;
109
74
BEGIN
110
- -- Update sequences for tables with serial columns
111
- FOR seq_name, table_name IN
112
- SELECT sequence_name, table_name
75
+ -- Find each sequence and its owning table/column
76
+ FOR seq_name, tbl_name, col_name IN
77
+ SELECT s . sequence_name , c . table_name , c . column_name
113
78
FROM information_schema .sequences s
114
- JOIN information_schema .columns c ON c .column_default LIKE ' %' || s .sequence_name || ' %'
79
+ JOIN information_schema .columns c
80
+ ON c .column_default LIKE (' %' || s .sequence_name || ' %' )
115
81
WHERE s .sequence_schema = ' public'
116
82
LOOP
117
- EXECUTE format(' SELECT COALESCE(MAX(%I), 0) FROM %I' ,
118
- replace(seq_name, ' _seq' , ' ' ), table_name) INTO max_id;
83
+ EXECUTE format(' SELECT COALESCE(MAX(%I), 0) FROM %I' , col_name, tbl_name)
84
+ INTO max_id;
85
+
119
86
IF max_id > 0 THEN
120
87
EXECUTE format(' SELECT setval(%L, %s, true)' , seq_name, max_id);
121
88
RAISE NOTICE ' Updated sequence % to %' , seq_name, max_id;
@@ -147,17 +114,17 @@ BEGIN
147
114
148
115
FOR rec IN
149
116
SELECT
150
- t .tablename ,
117
+ t .table_name ,
151
118
COALESCE(s .n_tup_ins , 0 ) as rows_loaded
152
119
FROM information_schema .tables t
153
- LEFT JOIN pg_stat_user_tables s ON s .tablename = t .table_name AND s .schemaname = ' public'
120
+ LEFT JOIN pg_stat_user_tables s
121
+ ON s .relname = t .table_name AND s .schemaname = ' public'
154
122
WHERE t .table_schema = ' public'
155
- AND t .table_type = ' BASE TABLE'
156
- -- Exclude staging and utility tables
157
- AND t .table_name NOT LIKE ' staging_%'
123
+ AND t .table_type = ' BASE TABLE'
124
+ AND t .table_name NOT LIKE ' staging_%'
158
125
ORDER BY t .table_name
159
126
LOOP
160
- RAISE NOTICE ' Table: % - Rows: %' , rec .tablename , rec .rows_loaded ;
127
+ RAISE NOTICE ' Table: % - Rows: %' , rec .table_name , rec .rows_loaded ;
161
128
total_tables := total_tables + 1 ;
162
129
total_rows := total_rows + rec .rows_loaded ;
163
130
END LOOP;
@@ -176,17 +143,13 @@ SELECT
176
143
s .last_analyze ,
177
144
pg_size_pretty(pg_total_relation_size(' public.' || t .table_name )) as table_size
178
145
FROM information_schema .tables t
179
- LEFT JOIN pg_stat_user_tables s ON s .tablename = t .table_name AND s .schemaname = ' public'
146
+ LEFT JOIN pg_stat_user_tables s ON s .relname = t .table_name AND s .schemaname = ' public'
180
147
WHERE t .table_schema = ' public'
181
- AND t .table_type = ' BASE TABLE'
182
- AND t .table_name NOT LIKE ' staging_%'
148
+ AND t .table_type = ' BASE TABLE'
149
+ AND t .table_name NOT LIKE ' staging_%'
183
150
ORDER BY s .n_tup_ins DESC NULLS LAST, t .table_name ;
184
151
185
- -- Grant permissions (if needed)
186
- -- GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO your_app_user;
187
- -- GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO your_app_user;
188
-
189
152
ANALYZE; -- Update table statistics for query optimization
190
153
191
154
\echo ' C360 Retail Fashion Database loading completed successfully!'
192
- \echo ' Use: SELECT * FROM table_row_counts; to see loaded data summary'
155
+ \echo ' Use: SELECT * FROM table_row_counts; to see loaded data summary'
0 commit comments