|
Bogdan Timofte
authored
2 weeks ago
|
1
|
-- autoSMART — Rename v1 tables to archive
|
|
|
2
|
-- Run on-demand after validating collectors work with schema v2
|
|
|
3
|
-- Date: 2026-05-20
|
|
|
4
|
-- Safe: tables are read-only at this point
|
|
|
5
|
|
|
|
6
|
BEGIN;
|
|
|
7
|
|
|
|
8
|
-- Rename tables
|
|
|
9
|
ALTER TABLE IF EXISTS smart_readings RENAME TO smart_readings_archive_v1;
|
|
|
10
|
ALTER TABLE IF EXISTS smart_thresholds RENAME TO smart_thresholds_archive_v1;
|
|
|
11
|
ALTER TABLE IF EXISTS alert_history RENAME TO alert_history_archive_v1;
|
|
|
12
|
|
|
|
13
|
-- Rename sequences
|
|
|
14
|
ALTER SEQUENCE IF EXISTS smart_readings_id_seq RENAME TO smart_readings_archive_v1_id_seq;
|
|
|
15
|
ALTER SEQUENCE IF EXISTS smart_thresholds_id_seq RENAME TO smart_thresholds_archive_v1_id_seq;
|
|
|
16
|
ALTER SEQUENCE IF EXISTS alert_history_id_seq RENAME TO alert_history_archive_v1_id_seq;
|
|
|
17
|
|
|
|
18
|
-- Rename indexes
|
|
|
19
|
ALTER INDEX IF EXISTS smart_readings_pkey RENAME TO smart_readings_archive_v1_pkey;
|
|
|
20
|
ALTER INDEX IF EXISTS idx_smart_readings_hdd_id RENAME TO idx_smart_readings_archive_v1_hdd_id;
|
|
|
21
|
ALTER INDEX IF EXISTS idx_smart_readings_timestamp RENAME TO idx_smart_readings_archive_v1_timestamp;
|
|
|
22
|
ALTER INDEX IF EXISTS idx_smart_readings_serial RENAME TO idx_smart_readings_archive_v1_serial;
|
|
|
23
|
ALTER INDEX IF EXISTS idx_smart_readings_device_path RENAME TO idx_smart_readings_archive_v1_device_path;
|
|
|
24
|
ALTER INDEX IF EXISTS idx_smart_readings_type RENAME TO idx_smart_readings_archive_v1_type;
|
|
|
25
|
ALTER INDEX IF EXISTS idx_smart_readings_checksum RENAME TO idx_smart_readings_archive_v1_checksum;
|
|
|
26
|
ALTER INDEX IF EXISTS idx_smart_readings_previous RENAME TO idx_smart_readings_archive_v1_previous;
|
|
|
27
|
ALTER INDEX IF EXISTS idx_smart_readings_parameters RENAME TO idx_smart_readings_archive_v1_parameters;
|
|
|
28
|
ALTER INDEX IF EXISTS idx_smart_readings_changed_params RENAME TO idx_smart_readings_archive_v1_changed_params;
|
|
|
29
|
|
|
|
30
|
ALTER INDEX IF EXISTS smart_thresholds_pkey RENAME TO smart_thresholds_archive_v1_pkey;
|
|
|
31
|
ALTER INDEX IF EXISTS uq_parameter_name RENAME TO uq_archive_v1_parameter_name;
|
|
|
32
|
|
|
|
33
|
ALTER INDEX IF EXISTS alert_history_pkey RENAME TO alert_history_archive_v1_pkey;
|
|
|
34
|
ALTER INDEX IF EXISTS idx_alert_history_hdd_id RENAME TO idx_alert_history_archive_v1_hdd_id;
|
|
|
35
|
ALTER INDEX IF EXISTS idx_alert_history_sent_at RENAME TO idx_alert_history_archive_v1_sent_at;
|
|
|
36
|
ALTER INDEX IF EXISTS idx_alert_history_severity RENAME TO idx_alert_history_archive_v1_severity;
|
|
|
37
|
ALTER INDEX IF EXISTS idx_alert_history_serial RENAME TO idx_alert_history_archive_v1_serial;
|
|
|
38
|
|
|
|
39
|
-- Rename constraints
|
|
|
40
|
ALTER TABLE smart_readings_archive_v1 RENAME CONSTRAINT smart_readings_hdd_id_fkey TO smart_readings_archive_v1_hdd_id_fkey;
|
|
|
41
|
ALTER TABLE smart_readings_archive_v1 RENAME CONSTRAINT smart_readings_previous_reading_id_fkey TO smart_readings_archive_v1_previous_reading_id_fkey;
|
|
|
42
|
ALTER TABLE alert_history_archive_v1 RENAME CONSTRAINT alert_history_hdd_id_fkey TO alert_history_archive_v1_hdd_id_fkey;
|
|
|
43
|
ALTER TABLE alert_history_archive_v1 RENAME CONSTRAINT alert_history_related_reading_id_fkey TO alert_history_archive_v1_related_reading_id_fkey;
|
|
|
44
|
ALTER TABLE alert_history_archive_v1 RENAME CONSTRAINT alert_history_related_prediction_id_fkey TO alert_history_archive_v1_related_prediction_id_fkey;
|
|
|
45
|
|
|
|
46
|
COMMIT;
|
|
|
47
|
|
|
|
48
|
-- Summary
|
|
|
49
|
DO $$
|
|
|
50
|
BEGIN
|
|
|
51
|
RAISE NOTICE '✅ Archive rename complete!';
|
|
|
52
|
RAISE NOTICE 'Tables renamed:';
|
|
|
53
|
RAISE NOTICE ' • smart_readings → smart_readings_archive_v1';
|
|
|
54
|
RAISE NOTICE ' • smart_thresholds → smart_thresholds_archive_v1';
|
|
|
55
|
RAISE NOTICE ' • alert_history → alert_history_archive_v1';
|
|
|
56
|
RAISE NOTICE '';
|
|
|
57
|
RAISE NOTICE 'These tables are now read-only archives.';
|
|
|
58
|
RAISE NOTICE 'All new data flows through schema v2.';
|
|
|
59
|
RAISE NOTICE '';
|
|
|
60
|
RAISE NOTICE 'To delete archives (after 4-6 week validation):';
|
|
|
61
|
RAISE NOTICE ' DROP TABLE smart_readings_archive_v1 CASCADE;';
|
|
|
62
|
RAISE NOTICE ' DROP TABLE smart_thresholds_archive_v1 CASCADE;';
|
|
|
63
|
RAISE NOTICE ' DROP TABLE alert_history_archive_v1 CASCADE;';
|
|
|
64
|
END $$;
|