diff --git a/db/migrations/014_add_display_name_to_attachments.sql b/db/migrations/014_add_display_name_to_attachments.sql new file mode 100644 index 0000000..5af6d56 --- /dev/null +++ b/db/migrations/014_add_display_name_to_attachments.sql @@ -0,0 +1,4 @@ +-- Migration: Add display_name to attachments table +-- This column is needed for the split_mailbox migration (015) to work correctly + +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS display_name VARCHAR(255) AFTER mail_id; diff --git a/db/migrations/015_split_mailbox_tables.sql b/db/migrations/015_split_mailbox_tables.sql index 30ee704..3de8ae4 100644 --- a/db/migrations/015_split_mailbox_tables.sql +++ b/db/migrations/015_split_mailbox_tables.sql @@ -127,50 +127,47 @@ CREATE TABLE IF NOT EXISTS internal_comments ( FOREIGN KEY (referred_user_id) REFERENCES users(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; --- 4. Migrate data from old mailbox table --- We'll use a temporary mapping for IDs if we were strict, but since we are splitting, we can just insert. --- Note: Original IDs will change, which means attachments and comments must be migrated carefully. - --- Migrate Inbound -INSERT INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +-- 4. Migrate data (using INSERT IGNORE to allow re-running partially failed migrations) +INSERT IGNORE INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'inbound'; -INSERT INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'inbound'; -INSERT INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'inbound'; --- Migrate Outbound -INSERT INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +INSERT IGNORE INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'outbound'; -INSERT INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'outbound'; -INSERT INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'outbound'; --- Migrate Internal -INSERT INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +INSERT IGNORE INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'internal'; -INSERT INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'internal'; -INSERT INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'internal'; -- 5. Rename old tables instead of dropping for safety -RENAME TABLE mailbox TO mailbox_old; -RENAME TABLE attachments TO attachments_old; -RENAME TABLE comments TO comments_old; +-- Using a check because RENAME TABLE fails if the target exists +SET @old_mailbox = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox_old'); +SET @sql_rename = IF(@old_mailbox = 0, 'RENAME TABLE mailbox TO mailbox_old, attachments TO attachments_old, comments TO comments_old', 'SELECT 1'); +PREPARE stmt FROM @sql_rename; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; \ No newline at end of file diff --git a/inbound.php b/inbound.php index d056af8..5135632 100644 --- a/inbound.php +++ b/inbound.php @@ -264,6 +264,9 @@ if (isset($_GET['id'])) {
|
+
+
+
+
+
+ = htmlspecialchars($site_name) ?>
+
+
+
+ = htmlspecialchars($site_name) ?>
+ = htmlspecialchars($site_address) ?>
+ |
+
|
+
+
+
+ = $mail['description'] ?>
+
+ |
+
| + + | +