|
| 1 | +-- |
| 2 | +-- Copyright (C) 2025 Intel Corporation |
| 3 | +-- |
| 4 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +-- you may not use this file except in compliance with the License. |
| 6 | +-- You may obtain a copy of the License at |
| 7 | +-- |
| 8 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +-- |
| 10 | +-- Unless required by applicable law or agreed to in writing, software |
| 11 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +-- See the License for the specific language governing permissions and |
| 14 | +-- limitations under the License. |
| 15 | +-- |
| 16 | + |
| 17 | +-- ============================================================ |
| 18 | +-- ROLLBACK MIGRATION: Remove Dynamic Profiling Feature |
| 19 | +-- ============================================================ |
| 20 | +-- This rollback script removes the dynamic profiling feature |
| 21 | +-- and restores the database to its pre-migration state. |
| 22 | +-- |
| 23 | +-- WARNING: This will permanently delete all dynamic profiling data! |
| 24 | +-- |
| 25 | +-- Usage: |
| 26 | +-- psql -U performance_studio -d performance_studio -f add_dynamic_profiling.down.sql |
| 27 | +-- |
| 28 | +-- To restore the feature: |
| 29 | +-- psql -U performance_studio -d performance_studio -f add_dynamic_profiling.up.sql |
| 30 | +-- ============================================================ |
| 31 | + |
| 32 | +BEGIN; |
| 33 | + |
| 34 | +-- ============================================================ |
| 35 | +-- STEP 1: Backup Warning |
| 36 | +-- ============================================================ |
| 37 | + |
| 38 | +DO $$ |
| 39 | +BEGIN |
| 40 | + RAISE NOTICE '========================================'; |
| 41 | + RAISE NOTICE 'ROLLBACK: Removing Dynamic Profiling Feature'; |
| 42 | + RAISE NOTICE '========================================'; |
| 43 | + RAISE NOTICE 'This will permanently delete:'; |
| 44 | + RAISE NOTICE ' - All profiling requests'; |
| 45 | + RAISE NOTICE ' - All profiling commands'; |
| 46 | + RAISE NOTICE ' - All profiling executions'; |
| 47 | + RAISE NOTICE ' - All host heartbeat data'; |
| 48 | + RAISE NOTICE ''; |
| 49 | + RAISE NOTICE 'Ensure you have a backup if you need this data!'; |
| 50 | + RAISE NOTICE '========================================'; |
| 51 | +END $$; |
| 52 | + |
| 53 | + |
| 54 | +-- ============================================================ |
| 55 | +-- STEP 2: Drop Indexes |
| 56 | +-- ============================================================ |
| 57 | +-- Drop indexes first for cleaner table drops |
| 58 | + |
| 59 | +-- ProfilingExecutions indexes |
| 60 | +DROP INDEX IF EXISTS idx_profilingexecutions_status; |
| 61 | +DROP INDEX IF EXISTS idx_profilingexecutions_profiling_request_id; |
| 62 | +DROP INDEX IF EXISTS idx_profilingexecutions_hostname; |
| 63 | +DROP INDEX IF EXISTS idx_profilingexecutions_command_id; |
| 64 | + |
| 65 | +-- ProfilingCommands indexes |
| 66 | +DROP INDEX IF EXISTS idx_profilingcommands_hostname_service; |
| 67 | +DROP INDEX IF EXISTS idx_profilingcommands_status; |
| 68 | +DROP INDEX IF EXISTS idx_profilingcommands_service_name; |
| 69 | +DROP INDEX IF EXISTS idx_profilingcommands_hostname; |
| 70 | +DROP INDEX IF EXISTS idx_profilingcommands_command_id; |
| 71 | + |
| 72 | +-- ProfilingRequests indexes |
| 73 | +DROP INDEX IF EXISTS idx_profilingrequests_created_at; |
| 74 | +DROP INDEX IF EXISTS idx_profilingrequests_request_type; |
| 75 | +DROP INDEX IF EXISTS idx_profilingrequests_status; |
| 76 | +DROP INDEX IF EXISTS idx_profilingrequests_service_name; |
| 77 | +DROP INDEX IF EXISTS idx_profilingrequests_request_id; |
| 78 | + |
| 79 | +-- HostHeartbeats indexes |
| 80 | +DROP INDEX IF EXISTS idx_hostheartbeats_heartbeat_timestamp; |
| 81 | +DROP INDEX IF EXISTS idx_hostheartbeats_status; |
| 82 | +DROP INDEX IF EXISTS idx_hostheartbeats_service_name; |
| 83 | +DROP INDEX IF EXISTS idx_hostheartbeats_hostname; |
| 84 | + |
| 85 | + |
| 86 | +-- ============================================================ |
| 87 | +-- STEP 3: Drop Tables (in correct order for foreign keys) |
| 88 | +-- ============================================================ |
| 89 | + |
| 90 | +-- Drop ProfilingExecutions first (has FK to ProfilingRequests) |
| 91 | +DROP TABLE IF EXISTS ProfilingExecutions CASCADE; |
| 92 | + |
| 93 | +-- Drop ProfilingCommands (no dependencies) |
| 94 | +DROP TABLE IF EXISTS ProfilingCommands CASCADE; |
| 95 | + |
| 96 | +-- Drop ProfilingRequests (has FK to Services, referenced by ProfilingExecutions) |
| 97 | +DROP TABLE IF EXISTS ProfilingRequests CASCADE; |
| 98 | + |
| 99 | +-- Drop HostHeartbeats (no dependencies) |
| 100 | +DROP TABLE IF EXISTS HostHeartbeats CASCADE; |
| 101 | + |
| 102 | + |
| 103 | +-- ============================================================ |
| 104 | +-- STEP 4: Drop ENUM Types |
| 105 | +-- ============================================================ |
| 106 | +-- Drop types after tables that use them |
| 107 | + |
| 108 | +DROP TYPE IF EXISTS HostStatus CASCADE; |
| 109 | +DROP TYPE IF EXISTS CommandStatus CASCADE; |
| 110 | +DROP TYPE IF EXISTS ProfilingRequestStatus CASCADE; |
| 111 | +DROP TYPE IF EXISTS ProfilingMode CASCADE; |
| 112 | + |
| 113 | + |
| 114 | +-- ============================================================ |
| 115 | +-- STEP 5: Verify Rollback Success |
| 116 | +-- ============================================================ |
| 117 | + |
| 118 | +DO $$ |
| 119 | +DECLARE |
| 120 | + remaining_tables integer; |
| 121 | + remaining_types integer; |
| 122 | +BEGIN |
| 123 | + -- Count remaining tables |
| 124 | + SELECT COUNT(*) INTO remaining_tables |
| 125 | + FROM information_schema.tables |
| 126 | + WHERE table_schema = 'public' |
| 127 | + AND table_name IN ('hostheartbeats', 'profilingrequests', 'profilingcommands', 'profilingexecutions'); |
| 128 | + |
| 129 | + -- Count remaining types |
| 130 | + SELECT COUNT(*) INTO remaining_types |
| 131 | + FROM pg_type |
| 132 | + WHERE typname IN ('profilingmode', 'profilingrequeststatus', 'commandstatus', 'hoststatus'); |
| 133 | + |
| 134 | + IF remaining_tables > 0 THEN |
| 135 | + RAISE WARNING 'Some tables still exist: %', remaining_tables; |
| 136 | + END IF; |
| 137 | + |
| 138 | + IF remaining_types > 0 THEN |
| 139 | + RAISE WARNING 'Some types still exist: %', remaining_types; |
| 140 | + END IF; |
| 141 | + |
| 142 | + IF remaining_tables = 0 AND remaining_types = 0 THEN |
| 143 | + RAISE NOTICE '========================================'; |
| 144 | + RAISE NOTICE 'Rollback completed successfully!'; |
| 145 | + RAISE NOTICE '========================================'; |
| 146 | + RAISE NOTICE 'Removed:'; |
| 147 | + RAISE NOTICE ' - 4 tables (HostHeartbeats, ProfilingRequests, ProfilingCommands, ProfilingExecutions)'; |
| 148 | + RAISE NOTICE ' - 4 ENUM types (ProfilingMode, ProfilingRequestStatus, CommandStatus, HostStatus)'; |
| 149 | + RAISE NOTICE ' - 14 indexes'; |
| 150 | + RAISE NOTICE ''; |
| 151 | + RAISE NOTICE 'Database restored to pre-dynamic-profiling state.'; |
| 152 | + RAISE NOTICE '========================================'; |
| 153 | + ELSE |
| 154 | + RAISE EXCEPTION 'Rollback incomplete: % tables, % types remain', remaining_tables, remaining_types; |
| 155 | + END IF; |
| 156 | +END $$; |
| 157 | + |
| 158 | +COMMIT; |
| 159 | + |
| 160 | +-- ============================================================ |
| 161 | +-- Rollback Complete |
| 162 | +-- ============================================================ |
| 163 | +-- The dynamic profiling feature has been successfully removed. |
| 164 | +-- All related tables, types, and indexes have been dropped. |
| 165 | +-- ============================================================ |
| 166 | + |
0 commit comments