update migration
This commit is contained in:
parent
e11d717222
commit
518c9fdd19
@ -128,46 +128,82 @@ CREATE TABLE IF NOT EXISTS internal_comments (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
-- 4. Migrate data (using INSERT IGNORE to allow re-running partially failed migrations)
|
-- 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)
|
-- We wrap these in conditional checks to ensure they only run if 'mailbox' exists.
|
||||||
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 IGNORE INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
SET @mailbox_exists = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox');
|
||||||
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 IGNORE INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
-- Inbound Mail
|
||||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'inbound';
|
"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'",
|
||||||
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
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)
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_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'",
|
||||||
FROM mailbox WHERE type = 'outbound';
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
INSERT IGNORE INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.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'",
|
||||||
FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'outbound';
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
INSERT IGNORE INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
-- Outbound Mail
|
||||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'outbound';
|
"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'",
|
||||||
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
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)
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
SELECT 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_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'",
|
||||||
FROM mailbox WHERE type = 'internal';
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
INSERT IGNORE INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at)
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.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'",
|
||||||
FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'internal';
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
INSERT IGNORE INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at)
|
-- Internal Mail
|
||||||
SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'internal';
|
"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'",
|
||||||
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
|
"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'",
|
||||||
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
|
SET @sql = IF(@mailbox_exists > 0,
|
||||||
|
"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'",
|
||||||
|
"SELECT 1");
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
-- 5. Rename old tables instead of dropping for safety
|
-- 5. Rename old tables instead of dropping for safety
|
||||||
-- Using a check because RENAME TABLE fails if the target exists
|
-- Only rename if 'mailbox' exists and 'mailbox_old' does not
|
||||||
SET @old_mailbox = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox_old');
|
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');
|
SET @mailbox_exists = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox');
|
||||||
|
|
||||||
|
SET @sql_rename = IF(@old_mailbox = 0 AND @mailbox_exists > 0, 'RENAME TABLE mailbox TO mailbox_old, attachments TO attachments_old, comments TO comments_old', 'SELECT 1');
|
||||||
PREPARE stmt FROM @sql_rename;
|
PREPARE stmt FROM @sql_rename;
|
||||||
EXECUTE stmt;
|
EXECUTE stmt;
|
||||||
DEALLOCATE PREPARE stmt;
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user