setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $log = []; // ----------------------------------------------------------------------- // 1. Create novena_groups table // ----------------------------------------------------------------------- $pdo->exec(" CREATE TABLE IF NOT EXISTS novena_groups ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, mystery_set VARCHAR(50) NOT NULL DEFAULT 'sorrowful', subject_name VARCHAR(255) NULL, subject_pronoun VARCHAR(10) NULL, subject_dates VARCHAR(150) NULL, photo_path VARCHAR(500) NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) "); $log[] = 'novena_groups table ready.'; // ----------------------------------------------------------------------- // 2. Add novena_group_id to sessions (silent if already present) // ----------------------------------------------------------------------- try { $pdo->exec('ALTER TABLE sessions ADD COLUMN novena_group_id INT NULL'); $log[] = 'Added novena_group_id column to sessions.'; } catch (PDOException $e) { $log[] = 'novena_group_id column already exists — skipped.'; } // ----------------------------------------------------------------------- // 3. Migrate existing novena sessions that have no group yet // ----------------------------------------------------------------------- $novenas = $pdo->query(" SELECT * FROM sessions WHERE occasion = 'novena_deceased' AND (novena_group_id IS NULL OR novena_group_id = 0) ORDER BY name, novena_day ")->fetchAll(); if (empty($novenas)) { $log[] = 'No ungrouped novena sessions found — nothing to migrate.'; } else { // Bucket sessions by the base name (strip trailing " — Day N") $buckets = []; foreach ($novenas as $n) { $base = preg_replace('/ — Day \d+$/', '', $n['name']); $buckets[$base][] = $n; } $ins_grp = $pdo->prepare(' INSERT INTO novena_groups (name, mystery_set, subject_name, subject_pronoun, subject_dates, photo_path) VALUES (?, ?, ?, ?, ?, ?) '); $upd_ses = $pdo->prepare('UPDATE sessions SET novena_group_id = ? WHERE id = ?'); foreach ($buckets as $base_name => $days) { $first = $days[0]; $ins_grp->execute([ $base_name, $first['mystery_set'], $first['subject_name'], $first['subject_pronoun'], $first['subject_dates'], $first['photo_path'], ]); $gid = (int)$pdo->lastInsertId(); foreach ($days as $day) { $upd_ses->execute([$gid, $day['id']]); } $log[] = 'Created group #' . $gid . ' "' . $base_name . '" — ' . count($days) . ' day(s) linked.'; } } ?> Migrate v2

Migration v2 Complete

⚠ Delete migrate_v2.php from your server now. It is no longer needed and should not be left publicly accessible.