From 5a559b30553f195c6e078badd492e3f1a57b6fb3 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Tue, 15 Jul 2025 13:02:43 +0200 Subject: [PATCH] warehouseoffer v2 --- ...50715110000_warehouse_offer_versioning.php | 132 ++++++++++++------ 1 file changed, 87 insertions(+), 45 deletions(-) diff --git a/db/migrations/20250715110000_warehouse_offer_versioning.php b/db/migrations/20250715110000_warehouse_offer_versioning.php index 32441a5aa..a60a7200f 100644 --- a/db/migrations/20250715110000_warehouse_offer_versioning.php +++ b/db/migrations/20250715110000_warehouse_offer_versioning.php @@ -1,52 +1,75 @@ -execute(" - CREATE TABLE IF NOT EXISTS `WarehouseOfferClosingText` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, - `text` text COLLATE utf8mb4_unicode_ci NOT NULL, - `createBy` int(11) NOT NULL, - `create` int(11) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - "); + // Use Phinx schema builder to create the WarehouseOfferClosingText table + $this->table('WarehouseOfferClosingText', [ + 'id' => false, + 'primary_key' => ['id'], + 'engine' => 'InnoDB', + 'encoding' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'comment' => 'Stores standard closing text snippets for offers', + ]) + ->addColumn('id', 'integer', ['identity' => true, 'signed' => true]) + ->addColumn('name', 'string', ['limit' => 255]) + ->addColumn('text', 'text') + ->addColumn('createBy', 'integer') + ->addColumn('create', 'integer') + ->create(); - $this->execute(" - CREATE TABLE IF NOT EXISTS `WarehouseOfferJournal` ( - `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, - `offerId` int(11) DEFAULT NULL, - `fileIds` text COLLATE utf8mb4_unicode_ci, - `message` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - `create` int(11) DEFAULT NULL, - `createBy` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `offerId` (`offerId`), - KEY `createBy` (`createBy`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - "); + // Use Phinx schema builder to create the WarehouseOfferJournal table + $this->table('WarehouseOfferJournal', [ + 'id' => false, + 'primary_key' => ['id'], + 'engine' => 'InnoDB', + 'encoding' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'comment' => 'Journal for tracking actions on warehouse offers', + ]) + ->addColumn('id', 'integer', ['identity' => true, 'signed' => false]) + ->addColumn('offerId', 'integer', ['null' => true]) + ->addColumn('fileIds', 'text', ['null' => true]) + ->addColumn('message', 'string', ['limit' => 255, 'null' => true]) + ->addColumn('create', 'integer', ['null' => true]) + ->addColumn('createBy', 'integer', ['null' => true]) + ->addIndex(['offerId'], ['name' => 'offerId']) + ->addIndex(['createBy'], ['name' => 'createBy']) + ->create(); - $this->execute(" - ALTER TABLE `WarehouseOffer` - ADD COLUMN IF NOT EXISTS `contactPersonEmail` VARCHAR(255) NULL DEFAULT NULL AFTER `contactPerson`, - ADD COLUMN IF NOT EXISTS `lastSentDate` INT(11) NULL DEFAULT NULL AFTER `status`, - ADD COLUMN IF NOT EXISTS `version` INT(11) NOT NULL DEFAULT 1 AFTER `id`, - ADD COLUMN IF NOT EXISTS `history_id` INT(11) NULL DEFAULT NULL AFTER `version`; - "); + // Use Phinx schema builder to add columns to the WarehouseOffer table + $warehouseOffer = $this->table('WarehouseOffer'); + $warehouseOffer + ->addColumn('contactPersonEmail', 'string', ['limit' => 255, 'null' => true, 'after' => 'contactPerson']) + ->addColumn('lastSentDate', 'integer', ['null' => true, 'after' => 'status']) + ->addColumn('version', 'integer', ['default' => 1, 'after' => 'id']) + ->addColumn('history_id', 'integer', ['null' => true, 'after' => 'version']) + ->save(); - $this->execute(" - ALTER TABLE `WarehouseHistory` - ADD COLUMN IF NOT EXISTS `data` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL AFTER `note`; - "); + // Use Phinx schema builder to add the 'data' column to WarehouseHistory + $warehouseHistory = $this->table('WarehouseHistory'); + $warehouseHistory + ->addColumn('data', 'text', [ + 'limit' => MysqlAdapter::TEXT_LONG, + 'null' => true, + 'after' => 'note', + 'encoding' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci' + ]) + ->save(); + // Data migration steps remain as raw SQL execution due to their complexity. $this->execute("DELETE FROM `WarehouseHistory` WHERE `table` = 'WarehouseOffer';"); + // Create a baseline version history for all existing offers. $this->execute(" INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `data`, `user_id`, `create`) SELECT @@ -58,8 +81,8 @@ final class WarehouseOfferVersioning extends AbstractMigration 'Baseline Version 1 erstellt durch Migration.' AS `note`, JSON_OBJECT( 'id', wo.id, - 'version', wo.version, - 'history_id', wo.history_id, + 'version', 1, + 'history_id', NULL, 'offerNumber', wo.offerNumber, 'reference', wo.reference, 'customerNumber', wo.customerNumber, @@ -90,6 +113,7 @@ final class WarehouseOfferVersioning extends AbstractMigration FROM `WarehouseOffer` wo; "); + // Update the history_id in the WarehouseOffer table to link to the newly created history entry. $this->execute(" UPDATE `WarehouseOffer` wo JOIN `WarehouseHistory` wh ON wo.id = wh.row_id @@ -100,18 +124,36 @@ final class WarehouseOfferVersioning extends AbstractMigration public function down(): void { + // Clean up the history created by this migration $this->execute("DELETE FROM `WarehouseHistory` WHERE `note` = 'Baseline Version 1 erstellt durch Migration.' AND `table` = 'WarehouseOffer';"); + // Revert changes to the WarehouseOffer table $warehouseOfferTable = $this->table('WarehouseOffer'); - if ($warehouseOfferTable->hasColumn('history_id')) $warehouseOfferTable->removeColumn('history_id')->save(); - if ($warehouseOfferTable->hasColumn('version')) $warehouseOfferTable->removeColumn('version')->save(); - if ($warehouseOfferTable->hasColumn('lastSentDate')) $warehouseOfferTable->removeColumn('lastSentDate')->save(); - if ($warehouseOfferTable->hasColumn('contactPersonEmail')) $warehouseOfferTable->removeColumn('contactPersonEmail')->save(); + if ($warehouseOfferTable->hasColumn('history_id')) { + $warehouseOfferTable->removeColumn('history_id')->save(); + } + if ($warehouseOfferTable->hasColumn('version')) { + $warehouseOfferTable->removeColumn('version')->save(); + } + if ($warehouseOfferTable->hasColumn('lastSentDate')) { + $warehouseOfferTable->removeColumn('lastSentDate')->save(); + } + if ($warehouseOfferTable->hasColumn('contactPersonEmail')) { + $warehouseOfferTable->removeColumn('contactPersonEmail')->save(); + } + // Revert changes to the WarehouseHistory table $warehouseHistoryTable = $this->table('WarehouseHistory'); - if ($warehouseHistoryTable->hasColumn('data')) $warehouseHistoryTable->removeColumn('data')->save(); + if ($warehouseHistoryTable->hasColumn('data')) { + $warehouseHistoryTable->removeColumn('data')->save(); + } - if ($this->hasTable('WarehouseOfferJournal')) $this->table('WarehouseOfferJournal')->drop()->save(); - if ($this->hasTable('WarehouseOfferClosingText')) $this->table('WarehouseOfferClosingText')->drop()->save(); + // Drop the newly created tables + if ($this->hasTable('WarehouseOfferJournal')) { + $this->table('WarehouseOfferJournal')->drop()->save(); + } + if ($this->hasTable('WarehouseOfferClosingText')) { + $this->table('WarehouseOfferClosingText')->drop()->save(); + } } -} \ No newline at end of file +}