@@ -17,20 +17,37 @@ WITH new_chars AS (
1717 schema_name,
1818 table_name,
1919 run_date
20+ ),
21+ updated_records AS (
22+ UPDATE data_table_chars
23+ SET approx_record_ct = n .approx_record_ct ,
24+ record_ct = n .record_ct ,
25+ column_ct = n .column_ct ,
26+ last_refresh_date = n .run_date ,
27+ drop_date = NULL
28+ FROM new_chars n
29+ INNER JOIN data_table_chars d ON (
30+ n .table_groups_id = d .table_groups_id
31+ AND n .schema_name = d .schema_name
32+ AND n .table_name = d .table_name
33+ )
34+ WHERE data_table_chars .table_id = d .table_id
35+ RETURNING data_table_chars.* , d .drop_date as old_drop_date
36+ )
37+ INSERT INTO data_structure_log (
38+ table_groups_id,
39+ table_id,
40+ table_name,
41+ change_date,
42+ change
2043)
21- UPDATE data_table_chars
22- SET approx_record_ct = n .approx_record_ct ,
23- record_ct = n .record_ct ,
24- column_ct = n .column_ct ,
25- last_refresh_date = n .run_date ,
26- drop_date = NULL
27- FROM new_chars n
28- INNER JOIN data_table_chars d ON (
29- n .table_groups_id = d .table_groups_id
30- AND n .schema_name = d .schema_name
31- AND n .table_name = d .table_name
32- )
33- WHERE data_table_chars .table_id = d .table_id ;
44+ SELECT u .table_groups_id ,
45+ u .table_id ,
46+ u .table_name ,
47+ u .last_refresh_date ,
48+ ' A'
49+ FROM updated_records u
50+ WHERE u .old_drop_date IS NOT NULL ;
3451
3552-- Add new records
3653WITH new_chars AS (
@@ -170,7 +187,7 @@ update_chars AS (
170187 )
171188 WHERE data_column_chars .table_id = d .table_id
172189 AND data_column_chars .column_name = d .column_name
173- RETURNING data_column_chars.* , d .db_data_type as old_data_type
190+ RETURNING data_column_chars.* , d .db_data_type as old_data_type, d . drop_date as old_drop_date, n . run_date as run_date
174191)
175192INSERT INTO data_structure_log (
176193 table_groups_id,
@@ -193,7 +210,20 @@ SELECT u.table_groups_id,
193210 u .old_data_type ,
194211 u .db_data_type
195212 FROM update_chars u
196- WHERE u .old_data_type <> u .db_data_type ;
213+ WHERE u .old_data_type <> u .db_data_type
214+ AND u .old_drop_date IS NULL
215+ UNION ALL
216+ SELECT u .table_groups_id ,
217+ u .table_id ,
218+ u .column_id ,
219+ u .table_name ,
220+ u .column_name ,
221+ u .run_date ,
222+ ' A' ,
223+ NULL ,
224+ u .db_data_type
225+ FROM update_chars u
226+ WHERE u .old_drop_date IS NOT NULL ;
197227
198228
199229-- Add new records
0 commit comments