warehouseoffer v2
This commit is contained in:
@@ -1,52 +1,75 @@
|
|||||||
<?php /** @noinspection ALL */
|
<?php
|
||||||
|
|
||||||
|
/** @noinspection ALL */
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Phinx\Migration\AbstractMigration;
|
use Phinx\Migration\AbstractMigration;
|
||||||
|
use Phinx\Db\Adapter\MysqlAdapter;
|
||||||
|
|
||||||
final class WarehouseOfferVersioning extends AbstractMigration
|
final class WarehouseOfferVersioning extends AbstractMigration
|
||||||
{
|
{
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
$this->execute("
|
// Use Phinx schema builder to create the WarehouseOfferClosingText table
|
||||||
CREATE TABLE IF NOT EXISTS `WarehouseOfferClosingText` (
|
$this->table('WarehouseOfferClosingText', [
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
'id' => false,
|
||||||
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
'primary_key' => ['id'],
|
||||||
`text` text COLLATE utf8mb4_unicode_ci NOT NULL,
|
'engine' => 'InnoDB',
|
||||||
`createBy` int(11) NOT NULL,
|
'encoding' => 'utf8mb4',
|
||||||
`create` int(11) NOT NULL,
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
PRIMARY KEY (`id`)
|
'comment' => 'Stores standard closing text snippets for offers',
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
])
|
||||||
");
|
->addColumn('id', 'integer', ['identity' => true, 'signed' => true])
|
||||||
|
->addColumn('name', 'string', ['limit' => 255])
|
||||||
|
->addColumn('text', 'text')
|
||||||
|
->addColumn('createBy', 'integer')
|
||||||
|
->addColumn('create', 'integer')
|
||||||
|
->create();
|
||||||
|
|
||||||
$this->execute("
|
// Use Phinx schema builder to create the WarehouseOfferJournal table
|
||||||
CREATE TABLE IF NOT EXISTS `WarehouseOfferJournal` (
|
$this->table('WarehouseOfferJournal', [
|
||||||
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
'id' => false,
|
||||||
`offerId` int(11) DEFAULT NULL,
|
'primary_key' => ['id'],
|
||||||
`fileIds` text COLLATE utf8mb4_unicode_ci,
|
'engine' => 'InnoDB',
|
||||||
`message` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
|
'encoding' => 'utf8mb4',
|
||||||
`create` int(11) DEFAULT NULL,
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
`createBy` int(11) DEFAULT NULL,
|
'comment' => 'Journal for tracking actions on warehouse offers',
|
||||||
PRIMARY KEY (`id`),
|
])
|
||||||
KEY `offerId` (`offerId`),
|
->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
|
||||||
KEY `createBy` (`createBy`)
|
->addColumn('offerId', 'integer', ['null' => true])
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
->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("
|
// Use Phinx schema builder to add columns to the WarehouseOffer table
|
||||||
ALTER TABLE `WarehouseOffer`
|
$warehouseOffer = $this->table('WarehouseOffer');
|
||||||
ADD COLUMN IF NOT EXISTS `contactPersonEmail` VARCHAR(255) NULL DEFAULT NULL AFTER `contactPerson`,
|
$warehouseOffer
|
||||||
ADD COLUMN IF NOT EXISTS `lastSentDate` INT(11) NULL DEFAULT NULL AFTER `status`,
|
->addColumn('contactPersonEmail', 'string', ['limit' => 255, 'null' => true, 'after' => 'contactPerson'])
|
||||||
ADD COLUMN IF NOT EXISTS `version` INT(11) NOT NULL DEFAULT 1 AFTER `id`,
|
->addColumn('lastSentDate', 'integer', ['null' => true, 'after' => 'status'])
|
||||||
ADD COLUMN IF NOT EXISTS `history_id` INT(11) NULL DEFAULT NULL AFTER `version`;
|
->addColumn('version', 'integer', ['default' => 1, 'after' => 'id'])
|
||||||
");
|
->addColumn('history_id', 'integer', ['null' => true, 'after' => 'version'])
|
||||||
|
->save();
|
||||||
|
|
||||||
$this->execute("
|
// Use Phinx schema builder to add the 'data' column to WarehouseHistory
|
||||||
ALTER TABLE `WarehouseHistory`
|
$warehouseHistory = $this->table('WarehouseHistory');
|
||||||
ADD COLUMN IF NOT EXISTS `data` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL AFTER `note`;
|
$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';");
|
$this->execute("DELETE FROM `WarehouseHistory` WHERE `table` = 'WarehouseOffer';");
|
||||||
|
|
||||||
|
// Create a baseline version history for all existing offers.
|
||||||
$this->execute("
|
$this->execute("
|
||||||
INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `data`, `user_id`, `create`)
|
INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `data`, `user_id`, `create`)
|
||||||
SELECT
|
SELECT
|
||||||
@@ -58,8 +81,8 @@ final class WarehouseOfferVersioning extends AbstractMigration
|
|||||||
'Baseline Version 1 erstellt durch Migration.' AS `note`,
|
'Baseline Version 1 erstellt durch Migration.' AS `note`,
|
||||||
JSON_OBJECT(
|
JSON_OBJECT(
|
||||||
'id', wo.id,
|
'id', wo.id,
|
||||||
'version', wo.version,
|
'version', 1,
|
||||||
'history_id', wo.history_id,
|
'history_id', NULL,
|
||||||
'offerNumber', wo.offerNumber,
|
'offerNumber', wo.offerNumber,
|
||||||
'reference', wo.reference,
|
'reference', wo.reference,
|
||||||
'customerNumber', wo.customerNumber,
|
'customerNumber', wo.customerNumber,
|
||||||
@@ -90,6 +113,7 @@ final class WarehouseOfferVersioning extends AbstractMigration
|
|||||||
FROM `WarehouseOffer` wo;
|
FROM `WarehouseOffer` wo;
|
||||||
");
|
");
|
||||||
|
|
||||||
|
// Update the history_id in the WarehouseOffer table to link to the newly created history entry.
|
||||||
$this->execute("
|
$this->execute("
|
||||||
UPDATE `WarehouseOffer` wo
|
UPDATE `WarehouseOffer` wo
|
||||||
JOIN `WarehouseHistory` wh ON wo.id = wh.row_id
|
JOIN `WarehouseHistory` wh ON wo.id = wh.row_id
|
||||||
@@ -100,18 +124,36 @@ final class WarehouseOfferVersioning extends AbstractMigration
|
|||||||
|
|
||||||
public function down(): void
|
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';");
|
$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');
|
$warehouseOfferTable = $this->table('WarehouseOffer');
|
||||||
if ($warehouseOfferTable->hasColumn('history_id')) $warehouseOfferTable->removeColumn('history_id')->save();
|
if ($warehouseOfferTable->hasColumn('history_id')) {
|
||||||
if ($warehouseOfferTable->hasColumn('version')) $warehouseOfferTable->removeColumn('version')->save();
|
$warehouseOfferTable->removeColumn('history_id')->save();
|
||||||
if ($warehouseOfferTable->hasColumn('lastSentDate')) $warehouseOfferTable->removeColumn('lastSentDate')->save();
|
}
|
||||||
if ($warehouseOfferTable->hasColumn('contactPersonEmail')) $warehouseOfferTable->removeColumn('contactPersonEmail')->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');
|
$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();
|
// Drop the newly created tables
|
||||||
if ($this->hasTable('WarehouseOfferClosingText')) $this->table('WarehouseOfferClosingText')->drop()->save();
|
if ($this->hasTable('WarehouseOfferJournal')) {
|
||||||
|
$this->table('WarehouseOfferJournal')->drop()->save();
|
||||||
|
}
|
||||||
|
if ($this->hasTable('WarehouseOfferClosingText')) {
|
||||||
|
$this->table('WarehouseOfferClosingText')->drop()->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user