'use strict'; module.exports = { async up(queryInterface, Sequelize) { // Live `subject`/`html_body` become nullable — a brand-new template can // now exist as a pure draft with no live content at all until it's sent. await queryInterface.changeColumn('email_templates', 'subject', { type: Sequelize.STRING(255), allowNull: true, }); await queryInterface.changeColumn('email_templates', 'html_body', { type: Sequelize.TEXT, allowNull: true, }); await queryInterface.addColumn('email_templates', 'status', { type: Sequelize.ENUM('draft', 'sent'), allowNull: false, defaultValue: 'draft', }); // Edits to a 'sent' template land here first — sendEmail() only ever reads // the live subject/html_body columns, never these — until an admin // explicitly re-sends (publishes), the pending edit can't affect real mail. await queryInterface.addColumn('email_templates', 'draft_subject', { type: Sequelize.STRING(255), allowNull: true, }); await queryInterface.addColumn('email_templates', 'draft_html_body', { type: Sequelize.TEXT, allowNull: true, }); await queryInterface.addColumn('email_templates', 'last_sent_at', { type: Sequelize.DATE, allowNull: true, }); // Every row created before this migration already had required subject/ // html_body — meaning it was already "operating" in the old single-state // world. Backfill them all as sent. await queryInterface.sequelize.query(` UPDATE email_templates SET status = 'sent', last_sent_at = "updatedAt"; `); }, async down(queryInterface) { await queryInterface.removeColumn('email_templates', 'last_sent_at'); await queryInterface.removeColumn('email_templates', 'draft_html_body'); await queryInterface.removeColumn('email_templates', 'draft_subject'); await queryInterface.removeColumn('email_templates', 'status'); await queryInterface.sequelize.query(`DROP TYPE IF EXISTS "enum_email_templates_status";`); }, };