|
| 1 | +/* |
| 2 | + Events Migration Script |
| 3 | + - Reads from legacy event tables in source DB: "Events", "EventDetails", "EventRepetition" |
| 4 | + - Flattens and writes to reporting DB table: "Events" (new schema) |
| 5 | + - One row per repetition in destination |
| 6 | +*/ |
| 7 | + |
| 8 | +const { Client } = require('pg'); |
| 9 | +const dbConfig = require('./db'); |
| 10 | + |
| 11 | +console.log('=== Loading events-migration.js ==='); |
| 12 | + |
| 13 | +function coerceString(v) { |
| 14 | + return v === undefined || v === null ? null : String(v); |
| 15 | +} |
| 16 | +function coerceDecimal(v) { |
| 17 | + return v === undefined || v === null || v === '' ? null : String(v); |
| 18 | +} |
| 19 | +function coerceDate(v) { |
| 20 | + return v ? new Date(v) : null; |
| 21 | +} |
| 22 | +// Always return a JSON TEXT representation (or null) for binding with ::jsonb |
| 23 | +function coerceJson(v) { |
| 24 | + if (v === undefined || v === null) return null; |
| 25 | + // Buffer -> try parse, else stringify text |
| 26 | + if (Buffer.isBuffer(v)) { |
| 27 | + const s = v.toString('utf8').trim(); |
| 28 | + if (!s || s.toLowerCase() === 'null') return null; |
| 29 | + try { |
| 30 | + JSON.parse(s); |
| 31 | + return s; // already JSON text |
| 32 | + } catch { |
| 33 | + return JSON.stringify(s); |
| 34 | + } |
| 35 | + } |
| 36 | + // Numbers/booleans |
| 37 | + if (typeof v === 'number' || typeof v === 'boolean') { |
| 38 | + return JSON.stringify(v); |
| 39 | + } |
| 40 | + // Objects/arrays |
| 41 | + if (typeof v === 'object') { |
| 42 | + try { |
| 43 | + return JSON.stringify(v); |
| 44 | + } catch { |
| 45 | + return JSON.stringify(String(v)); |
| 46 | + } |
| 47 | + } |
| 48 | + // Strings |
| 49 | + if (typeof v === 'string') { |
| 50 | + const s = v.trim(); |
| 51 | + if (!s || s.toLowerCase() === 'null') return null; |
| 52 | + try { |
| 53 | + JSON.parse(s); |
| 54 | + return s; // valid JSON text |
| 55 | + } catch { |
| 56 | + // not JSON => store as JSON string value |
| 57 | + return JSON.stringify(s); |
| 58 | + } |
| 59 | + } |
| 60 | + // Fallback |
| 61 | + return JSON.stringify(String(v)); |
| 62 | +} |
| 63 | + |
| 64 | +async function migrateEvents({ limit = 10000, offset = 0 } = {}) { |
| 65 | + console.log('=== STARTING EVENTS MIGRATION ==='); |
| 66 | + |
| 67 | + const sourceClient = new Client(dbConfig.event_source); |
| 68 | + const destClient = new Client(dbConfig.event_destination); |
| 69 | + |
| 70 | + try { |
| 71 | + await sourceClient.connect(); |
| 72 | + console.log('[EVENTS] Connected to source database'); |
| 73 | + await destClient.connect(); |
| 74 | + console.log('[EVENTS] Connected to destination database'); |
| 75 | + |
| 76 | + // Join legacy tables; one row per repetition via LEFT JOIN |
| 77 | + const sql = ` |
| 78 | + SELECT |
| 79 | + e."eventId" AS event_id, |
| 80 | + e."isRecurring" AS is_recurring, |
| 81 | + e."recurrenceEndDate" AS recurrence_end_date, |
| 82 | + e."recurrencePattern" AS recurrence_pattern, |
| 83 | + e."autoEnroll" AS auto_enroll, |
| 84 | + e."registrationStartDate" AS reg_start, |
| 85 | + e."registrationEndDate" AS reg_end, |
| 86 | + e."createdBy" AS created_by, |
| 87 | + e."updatedBy" AS updated_by, |
| 88 | + ed."eventDetailId" AS event_detail_id, |
| 89 | + ed."title" AS title, |
| 90 | + ed."shortDescription" AS short_description, |
| 91 | + ed."eventType" AS event_type, |
| 92 | + ed."isRestricted" AS is_restricted, |
| 93 | + ed."location" AS location, |
| 94 | + ed."longitude" AS longitude, |
| 95 | + ed."latitude" AS latitude, |
| 96 | + ed."onlineProvider" AS online_provider, |
| 97 | + ed."maxAttendees" AS max_attendees, |
| 98 | + ed."recordings" AS recordings, |
| 99 | + ed."status" AS status, |
| 100 | + ed."description" AS description, |
| 101 | + ed."meetingDetails" AS meeting_details, |
| 102 | + ed."idealTime" AS ideal_time, |
| 103 | + ed."metadata" AS metadata, |
| 104 | + ed."attendees" AS attendees, |
| 105 | + er."startDateTime" AS start_dt, |
| 106 | + er."endDateTime" AS end_dt, |
| 107 | + er."onlineDetails" AS online_details |
| 108 | + FROM public."Events" e |
| 109 | + JOIN public."EventDetails" ed ON ed."eventDetailId" = e."eventDetailId" |
| 110 | + LEFT JOIN public."EventRepetition" er ON er."eventId" = e."eventId" |
| 111 | + ORDER BY e."eventId" |
| 112 | + LIMIT $1 OFFSET $2 |
| 113 | + `; |
| 114 | + |
| 115 | + const res = await sourceClient.query(sql, [limit, offset]); |
| 116 | + console.log(`[EVENTS] Fetched ${res.rows.length} rows to migrate`); |
| 117 | + |
| 118 | + const insertSql = ` |
| 119 | + INSERT INTO public."Events" ( |
| 120 | + "eventDetailId", title, "shortDescription", "eventType", "isRestricted", |
| 121 | + location, longitude, latitude, "onlineProvider", "maxAttendees", |
| 122 | + recordings, status, description, "meetingDetails", "createdBy", "updatedBy", |
| 123 | + "idealTime", metadata, attendees, "eventId", |
| 124 | + "startDateTime", "endDateTime", "onlineDetails", |
| 125 | + "autoEnroll", "registrationStartDate", "registrationEndDate", extra |
| 126 | + ) VALUES ( |
| 127 | + $1,$2,$3,$4,$5, |
| 128 | + $6,$7,$8,$9,$10, |
| 129 | + $11::jsonb,$12,$13,$14::jsonb,$15,$16, |
| 130 | + $17,$18::jsonb,$19::jsonb,$20, |
| 131 | + $21,$22,$23::jsonb, |
| 132 | + $24,$25,$26,$27::jsonb |
| 133 | + ) |
| 134 | + `; |
| 135 | + |
| 136 | + for (const r of res.rows) { |
| 137 | + const params = [ |
| 138 | + coerceString(r.event_detail_id), |
| 139 | + coerceString(r.title), |
| 140 | + coerceString(r.short_description), |
| 141 | + coerceString(r.event_type), |
| 142 | + !!r.is_restricted, |
| 143 | + coerceString(r.location), |
| 144 | + coerceDecimal(r.longitude), |
| 145 | + coerceDecimal(r.latitude), |
| 146 | + coerceString(r.online_provider), |
| 147 | + r.max_attendees ?? null, |
| 148 | + coerceJson(r.recordings), |
| 149 | + coerceString(r.status), |
| 150 | + coerceString(r.description), |
| 151 | + coerceJson(r.meeting_details), |
| 152 | + coerceString(r.created_by), |
| 153 | + coerceString(r.updated_by), |
| 154 | + coerceString(r.ideal_time), |
| 155 | + coerceJson(r.metadata), |
| 156 | + coerceJson(r.attendees), |
| 157 | + coerceString(r.event_id), |
| 158 | + coerceDate(r.start_dt), |
| 159 | + coerceDate(r.end_dt), |
| 160 | + coerceJson(r.online_details), |
| 161 | + !!r.auto_enroll, |
| 162 | + coerceDate(r.reg_start), |
| 163 | + coerceDate(r.reg_end), |
| 164 | + coerceJson(null), |
| 165 | + ]; |
| 166 | + |
| 167 | + try { |
| 168 | + await destClient.query(insertSql, params); |
| 169 | + } catch (e) { |
| 170 | + console.error('[EVENTS] Insert error for eventId:', r.event_id, e.message); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + console.log('[EVENTS] Migration batch completed'); |
| 175 | + } catch (err) { |
| 176 | + console.error('[EVENTS] Critical error:', err); |
| 177 | + } finally { |
| 178 | + await sourceClient.end(); |
| 179 | + await destClient.end(); |
| 180 | + console.log('[EVENTS] Disconnected from databases'); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +if (require.main === module) { |
| 185 | + console.log('Running events-migration.js directly'); |
| 186 | + migrateEvents().catch((err) => { |
| 187 | + console.error('Events migration failed with unhandled error:', err); |
| 188 | + process.exit(1); |
| 189 | + }); |
| 190 | +} |
| 191 | + |
| 192 | +module.exports = { migrateEvents }; |
| 193 | + |
0 commit comments