Update 20250715110000_warehouse_offer_versioning.php
This commit is contained in:
@@ -10,150 +10,154 @@ final class WarehouseOfferVersioning extends AbstractMigration
|
|||||||
{
|
{
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
// Use Phinx schema builder to create the WarehouseOfferClosingText table
|
if ($this->getEnvironment() == "thetool") {
|
||||||
$this->table('WarehouseOfferClosingText', [
|
// Use Phinx schema builder to create the WarehouseOfferClosingText table
|
||||||
'id' => false,
|
$this->table('WarehouseOfferClosingText', [
|
||||||
'primary_key' => ['id'],
|
'id' => false,
|
||||||
'engine' => 'InnoDB',
|
'primary_key' => ['id'],
|
||||||
'encoding' => 'utf8mb4',
|
'engine' => 'InnoDB',
|
||||||
'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();
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// 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',
|
'encoding' => 'utf8mb4',
|
||||||
'collation' => 'utf8mb4_unicode_ci'
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
|
'comment' => 'Stores standard closing text snippets for offers',
|
||||||
])
|
])
|
||||||
->save();
|
->addColumn('id', 'integer', ['identity' => true, 'signed' => true])
|
||||||
|
->addColumn('name', 'string', ['limit' => 255])
|
||||||
|
->addColumn('text', 'text')
|
||||||
|
->addColumn('createBy', 'integer')
|
||||||
|
->addColumn('create', 'integer')
|
||||||
|
->create();
|
||||||
|
|
||||||
// Data migration steps remain as raw SQL execution due to their complexity.
|
// Use Phinx schema builder to create the WarehouseOfferJournal table
|
||||||
$this->execute("DELETE FROM `WarehouseHistory` WHERE `table` = 'WarehouseOffer';");
|
$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();
|
||||||
|
|
||||||
// Create a baseline version history for all existing offers.
|
// Use Phinx schema builder to add columns to the WarehouseOffer table
|
||||||
$this->execute("
|
$warehouseOffer = $this->table('WarehouseOffer');
|
||||||
INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `data`, `user_id`, `create`)
|
$warehouseOffer
|
||||||
SELECT
|
->addColumn('contactPersonEmail', 'string', ['limit' => 255, 'null' => true, 'after' => 'contactPerson'])
|
||||||
'WarehouseOffer' AS `table`,
|
->addColumn('lastSentDate', 'integer', ['null' => true, 'after' => 'status'])
|
||||||
wo.id AS `row_id`,
|
->addColumn('version', 'integer', ['default' => 1, 'after' => 'id'])
|
||||||
'version' AS `key`,
|
->addColumn('history_id', 'integer', ['null' => true, 'after' => 'version'])
|
||||||
0 AS `old_value`,
|
->save();
|
||||||
1 AS `new_value`,
|
|
||||||
'Baseline Version 1 erstellt durch Migration.' AS `note`,
|
|
||||||
JSON_OBJECT(
|
|
||||||
'id', wo.id,
|
|
||||||
'version', 1,
|
|
||||||
'history_id', NULL,
|
|
||||||
'offerNumber', wo.offerNumber,
|
|
||||||
'reference', wo.reference,
|
|
||||||
'customerNumber', wo.customerNumber,
|
|
||||||
'customerName', wo.customerName,
|
|
||||||
'contactPerson', wo.contactPerson,
|
|
||||||
'contactPersonEmail', wo.contactPersonEmail,
|
|
||||||
'customerStreet', wo.customerStreet,
|
|
||||||
'customerCity', wo.customerCity,
|
|
||||||
'customerZip', wo.customerZip,
|
|
||||||
'customerVAT', wo.customerVAT,
|
|
||||||
'editor', wo.editor,
|
|
||||||
'purpose', wo.purpose,
|
|
||||||
'positions', wo.positions,
|
|
||||||
'alternativePositions', wo.alternativePositions,
|
|
||||||
'totalDiscount', wo.totalDiscount,
|
|
||||||
'paymentTerms', wo.paymentTerms,
|
|
||||||
'deliveryTerms', wo.deliveryTerms,
|
|
||||||
'closingText', wo.closingText,
|
|
||||||
'notes', wo.notes,
|
|
||||||
'status', wo.status,
|
|
||||||
'lastSentDate', wo.lastSentDate,
|
|
||||||
'totalAmount', wo.totalAmount,
|
|
||||||
'create', wo.create,
|
|
||||||
'createBy', wo.createBy
|
|
||||||
) AS `data`,
|
|
||||||
wo.createBy AS `user_id`,
|
|
||||||
UNIX_TIMESTAMP() AS `create`
|
|
||||||
FROM `WarehouseOffer` wo;
|
|
||||||
");
|
|
||||||
|
|
||||||
// Update the history_id in the WarehouseOffer table to link to the newly created history entry.
|
// Use Phinx schema builder to add the 'data' column to WarehouseHistory
|
||||||
$this->execute("
|
$warehouseHistory = $this->table('WarehouseHistory');
|
||||||
UPDATE `WarehouseOffer` wo
|
$warehouseHistory
|
||||||
JOIN `WarehouseHistory` wh ON wo.id = wh.row_id
|
->addColumn('data', 'text', [
|
||||||
SET wo.history_id = wh.id
|
'limit' => MysqlAdapter::TEXT_LONG,
|
||||||
WHERE wh.`table` = 'WarehouseOffer' AND wh.new_value = 1 AND wh.note = 'Baseline Version 1 erstellt durch Migration.';
|
'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
|
||||||
|
'WarehouseOffer' AS `table`,
|
||||||
|
wo.id AS `row_id`,
|
||||||
|
'version' AS `key`,
|
||||||
|
0 AS `old_value`,
|
||||||
|
1 AS `new_value`,
|
||||||
|
'Baseline Version 1 erstellt durch Migration.' AS `note`,
|
||||||
|
JSON_OBJECT(
|
||||||
|
'id', wo.id,
|
||||||
|
'version', 1,
|
||||||
|
'history_id', NULL,
|
||||||
|
'offerNumber', wo.offerNumber,
|
||||||
|
'reference', wo.reference,
|
||||||
|
'customerNumber', wo.customerNumber,
|
||||||
|
'customerName', wo.customerName,
|
||||||
|
'contactPerson', wo.contactPerson,
|
||||||
|
'contactPersonEmail', wo.contactPersonEmail,
|
||||||
|
'customerStreet', wo.customerStreet,
|
||||||
|
'customerCity', wo.customerCity,
|
||||||
|
'customerZip', wo.customerZip,
|
||||||
|
'customerVAT', wo.customerVAT,
|
||||||
|
'editor', wo.editor,
|
||||||
|
'purpose', wo.purpose,
|
||||||
|
'positions', wo.positions,
|
||||||
|
'alternativePositions', wo.alternativePositions,
|
||||||
|
'totalDiscount', wo.totalDiscount,
|
||||||
|
'paymentTerms', wo.paymentTerms,
|
||||||
|
'deliveryTerms', wo.deliveryTerms,
|
||||||
|
'closingText', wo.closingText,
|
||||||
|
'notes', wo.notes,
|
||||||
|
'status', wo.status,
|
||||||
|
'lastSentDate', wo.lastSentDate,
|
||||||
|
'totalAmount', wo.totalAmount,
|
||||||
|
'create', wo.create,
|
||||||
|
'createBy', wo.createBy
|
||||||
|
) AS `data`,
|
||||||
|
wo.createBy AS `user_id`,
|
||||||
|
UNIX_TIMESTAMP() AS `create`
|
||||||
|
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
|
||||||
|
SET wo.history_id = wh.id
|
||||||
|
WHERE wh.`table` = 'WarehouseOffer' AND wh.new_value = 1 AND wh.note = 'Baseline Version 1 erstellt durch Migration.';
|
||||||
|
");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
// Clean up the history created by this migration
|
if ($this->getEnvironment() == "thetool") {
|
||||||
$this->execute("DELETE FROM `WarehouseHistory` WHERE `note` = 'Baseline Version 1 erstellt durch Migration.' AND `table` = 'WarehouseOffer';");
|
// 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
|
// Revert changes to the WarehouseOffer table
|
||||||
$warehouseOfferTable = $this->table('WarehouseOffer');
|
$warehouseOfferTable = $this->table('WarehouseOffer');
|
||||||
if ($warehouseOfferTable->hasColumn('history_id')) {
|
if ($warehouseOfferTable->hasColumn('history_id')) {
|
||||||
$warehouseOfferTable->removeColumn('history_id')->save();
|
$warehouseOfferTable->removeColumn('history_id')->save();
|
||||||
}
|
}
|
||||||
if ($warehouseOfferTable->hasColumn('version')) {
|
if ($warehouseOfferTable->hasColumn('version')) {
|
||||||
$warehouseOfferTable->removeColumn('version')->save();
|
$warehouseOfferTable->removeColumn('version')->save();
|
||||||
}
|
}
|
||||||
if ($warehouseOfferTable->hasColumn('lastSentDate')) {
|
if ($warehouseOfferTable->hasColumn('lastSentDate')) {
|
||||||
$warehouseOfferTable->removeColumn('lastSentDate')->save();
|
$warehouseOfferTable->removeColumn('lastSentDate')->save();
|
||||||
}
|
}
|
||||||
if ($warehouseOfferTable->hasColumn('contactPersonEmail')) {
|
if ($warehouseOfferTable->hasColumn('contactPersonEmail')) {
|
||||||
$warehouseOfferTable->removeColumn('contactPersonEmail')->save();
|
$warehouseOfferTable->removeColumn('contactPersonEmail')->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert changes to the WarehouseHistory table
|
// Revert changes to the WarehouseHistory table
|
||||||
$warehouseHistoryTable = $this->table('WarehouseHistory');
|
$warehouseHistoryTable = $this->table('WarehouseHistory');
|
||||||
if ($warehouseHistoryTable->hasColumn('data')) {
|
if ($warehouseHistoryTable->hasColumn('data')) {
|
||||||
$warehouseHistoryTable->removeColumn('data')->save();
|
$warehouseHistoryTable->removeColumn('data')->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop the newly created tables
|
// Drop the newly created tables
|
||||||
if ($this->hasTable('WarehouseOfferJournal')) {
|
if ($this->hasTable('WarehouseOfferJournal')) {
|
||||||
$this->table('WarehouseOfferJournal')->drop()->save();
|
$this->table('WarehouseOfferJournal')->drop()->save();
|
||||||
}
|
}
|
||||||
if ($this->hasTable('WarehouseOfferClosingText')) {
|
if ($this->hasTable('WarehouseOfferClosingText')) {
|
||||||
$this->table('WarehouseOfferClosingText')->drop()->save();
|
$this->table('WarehouseOfferClosingText')->drop()->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user