90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
/**
|
|
* Migrate constructeur associations from legacy ManyToMany join tables
|
|
* (_composantconstructeurs, _piececonstructeurs, _machineconstructeurs, _productconstructeurs)
|
|
* to the new ConstructeurLink entity tables.
|
|
*/
|
|
final class Version20260405_MigrateConstructeurLinks extends AbstractMigration
|
|
{
|
|
public function getDescription(): string
|
|
{
|
|
return 'Migrate constructeur links from legacy M2M tables to new link entity tables';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
// Composant links (a = composantId, b = constructeurId)
|
|
$this->addSql("
|
|
INSERT INTO composant_constructeur_links (id, composantid, constructeurid, supplierreference, createdat, updatedat)
|
|
SELECT
|
|
'clmig_cc_' || ROW_NUMBER() OVER (ORDER BY a, b),
|
|
a, b, NULL, NOW(), NOW()
|
|
FROM _composantconstructeurs
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM composant_constructeur_links
|
|
WHERE composantid = _composantconstructeurs.a
|
|
AND constructeurid = _composantconstructeurs.b
|
|
)
|
|
");
|
|
|
|
// Piece links
|
|
$this->addSql("
|
|
INSERT INTO piece_constructeur_links (id, pieceid, constructeurid, supplierreference, createdat, updatedat)
|
|
SELECT
|
|
'clmig_pc_' || ROW_NUMBER() OVER (ORDER BY a, b),
|
|
a, b, NULL, NOW(), NOW()
|
|
FROM _piececonstructeurs
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM piece_constructeur_links
|
|
WHERE pieceid = _piececonstructeurs.a
|
|
AND constructeurid = _piececonstructeurs.b
|
|
)
|
|
");
|
|
|
|
// Machine links
|
|
$this->addSql("
|
|
INSERT INTO machine_constructeur_links (id, machineid, constructeurid, supplierreference, createdat, updatedat)
|
|
SELECT
|
|
'clmig_mc_' || ROW_NUMBER() OVER (ORDER BY a, b),
|
|
a, b, NULL, NOW(), NOW()
|
|
FROM _machineconstructeurs
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM machine_constructeur_links
|
|
WHERE machineid = _machineconstructeurs.a
|
|
AND constructeurid = _machineconstructeurs.b
|
|
)
|
|
");
|
|
|
|
// Product links
|
|
$this->addSql("
|
|
INSERT INTO product_constructeur_links (id, productid, constructeurid, supplierreference, createdat, updatedat)
|
|
SELECT
|
|
'clmig_prc_' || ROW_NUMBER() OVER (ORDER BY a, b),
|
|
a, b, NULL, NOW(), NOW()
|
|
FROM _productconstructeurs
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM product_constructeur_links
|
|
WHERE productid = _productconstructeurs.a
|
|
AND constructeurid = _productconstructeurs.b
|
|
)
|
|
");
|
|
}
|
|
|
|
public function down(Schema $schema): void
|
|
{
|
|
// Remove only the migrated rows (identifiable by 'clmig_' prefix)
|
|
$this->addSql("DELETE FROM composant_constructeur_links WHERE id LIKE 'clmig_cc_%'");
|
|
$this->addSql("DELETE FROM piece_constructeur_links WHERE id LIKE 'clmig_pc_%'");
|
|
$this->addSql("DELETE FROM machine_constructeur_links WHERE id LIKE 'clmig_mc_%'");
|
|
$this->addSql("DELETE FROM product_constructeur_links WHERE id LIKE 'clmig_prc_%'");
|
|
}
|
|
}
|