addSql('CREATE TABLE IF NOT EXISTS composant_constructeur_links (id VARCHAR(36) NOT NULL, supplierReference VARCHAR(255) DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, composantId VARCHAR(36) NOT NULL, constructeurId VARCHAR(36) NOT NULL, PRIMARY KEY (id))'); $this->addSql('CREATE INDEX IDX_139F3E3A345EE564 ON composant_constructeur_links (composantId)'); $this->addSql('CREATE INDEX IDX_139F3E3A70AF5AF0 ON composant_constructeur_links (constructeurId)'); $this->addSql('CREATE UNIQUE INDEX uniq_composant_constructeur ON composant_constructeur_links (composantid, constructeurid)'); $this->addSql('CREATE TABLE IF NOT EXISTS machine_constructeur_links (id VARCHAR(36) NOT NULL, supplierReference VARCHAR(255) DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, machineId VARCHAR(36) NOT NULL, constructeurId VARCHAR(36) NOT NULL, PRIMARY KEY (id))'); $this->addSql('CREATE INDEX IDX_665B2620633EC4FD ON machine_constructeur_links (machineId)'); $this->addSql('CREATE INDEX IDX_665B262070AF5AF0 ON machine_constructeur_links (constructeurId)'); $this->addSql('CREATE UNIQUE INDEX uniq_machine_constructeur ON machine_constructeur_links (machineid, constructeurid)'); $this->addSql('CREATE TABLE IF NOT EXISTS piece_constructeur_links (id VARCHAR(36) NOT NULL, supplierReference VARCHAR(255) DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, pieceId VARCHAR(36) NOT NULL, constructeurId VARCHAR(36) NOT NULL, PRIMARY KEY (id))'); $this->addSql('CREATE INDEX IDX_E664DAEC3C6A9D1 ON piece_constructeur_links (pieceId)'); $this->addSql('CREATE INDEX IDX_E664DAEC70AF5AF0 ON piece_constructeur_links (constructeurId)'); $this->addSql('CREATE UNIQUE INDEX uniq_piece_constructeur ON piece_constructeur_links (pieceid, constructeurid)'); $this->addSql('CREATE TABLE IF NOT EXISTS product_constructeur_links (id VARCHAR(36) NOT NULL, supplierReference VARCHAR(255) DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, productId VARCHAR(36) NOT NULL, constructeurId VARCHAR(36) NOT NULL, PRIMARY KEY (id))'); $this->addSql('CREATE INDEX IDX_E3D850B536799605 ON product_constructeur_links (productId)'); $this->addSql('CREATE INDEX IDX_E3D850B570AF5AF0 ON product_constructeur_links (constructeurId)'); $this->addSql('CREATE UNIQUE INDEX uniq_product_constructeur ON product_constructeur_links (productid, constructeurid)'); // Foreign keys $this->addSql('ALTER TABLE composant_constructeur_links ADD CONSTRAINT FK_139F3E3A345EE564 FOREIGN KEY (composantId) REFERENCES composants (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE composant_constructeur_links ADD CONSTRAINT FK_139F3E3A70AF5AF0 FOREIGN KEY (constructeurId) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE machine_constructeur_links ADD CONSTRAINT FK_665B2620633EC4FD FOREIGN KEY (machineId) REFERENCES machines (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE machine_constructeur_links ADD CONSTRAINT FK_665B262070AF5AF0 FOREIGN KEY (constructeurId) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE piece_constructeur_links ADD CONSTRAINT FK_E664DAEC3C6A9D1 FOREIGN KEY (pieceId) REFERENCES pieces (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE piece_constructeur_links ADD CONSTRAINT FK_E664DAEC70AF5AF0 FOREIGN KEY (constructeurId) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE product_constructeur_links ADD CONSTRAINT FK_E3D850B536799605 FOREIGN KEY (productId) REFERENCES products (id) ON DELETE CASCADE NOT DEFERRABLE'); $this->addSql('ALTER TABLE product_constructeur_links ADD CONSTRAINT FK_E3D850B570AF5AF0 FOREIGN KEY (constructeurId) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE'); // Data migration: copy from old join tables to new link tables $this->addSql("INSERT INTO machine_constructeur_links (id, machineid, constructeurid, supplierreference, createdat, updatedat) SELECT 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24), a, b, NULL, NOW(), NOW() FROM \"_machineconstructeurs\""); $this->addSql("INSERT INTO piece_constructeur_links (id, pieceid, constructeurid, supplierreference, createdat, updatedat) SELECT 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24), a, b, NULL, NOW(), NOW() FROM \"_piececonstructeurs\""); $this->addSql("INSERT INTO composant_constructeur_links (id, composantid, constructeurid, supplierreference, createdat, updatedat) SELECT 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24), a, b, NULL, NOW(), NOW() FROM \"_composantconstructeurs\""); $this->addSql("INSERT INTO product_constructeur_links (id, productid, constructeurid, supplierreference, createdat, updatedat) SELECT 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24), a, b, NULL, NOW(), NOW() FROM \"_productconstructeurs\""); // Drop old join tables $this->addSql('ALTER TABLE _composantconstructeurs DROP CONSTRAINT IF EXISTS fk_607601254ad0cf31'); $this->addSql('ALTER TABLE _composantconstructeurs DROP CONSTRAINT IF EXISTS fk_60760125d3d99e8b'); $this->addSql('ALTER TABLE _machineconstructeurs DROP CONSTRAINT IF EXISTS fk_e6a040cc4ad0cf31'); $this->addSql('ALTER TABLE _machineconstructeurs DROP CONSTRAINT IF EXISTS fk_e6a040ccd3d99e8b'); $this->addSql('ALTER TABLE _piececonstructeurs DROP CONSTRAINT IF EXISTS fk_e94732e54ad0cf31'); $this->addSql('ALTER TABLE _piececonstructeurs DROP CONSTRAINT IF EXISTS fk_e94732e5d3d99e8b'); $this->addSql('ALTER TABLE _productconstructeurs DROP CONSTRAINT IF EXISTS fk_cf7403fc4ad0cf31'); $this->addSql('ALTER TABLE _productconstructeurs DROP CONSTRAINT IF EXISTS fk_cf7403fcd3d99e8b'); $this->addSql('DROP TABLE IF EXISTS _composantconstructeurs'); $this->addSql('DROP TABLE IF EXISTS _machineconstructeurs'); $this->addSql('DROP TABLE IF EXISTS _piececonstructeurs'); $this->addSql('DROP TABLE IF EXISTS _productconstructeurs'); } public function down(Schema $schema): void { // Recreate old join tables $this->addSql('CREATE TABLE _composantconstructeurs (a VARCHAR(36) NOT NULL, b VARCHAR(36) NOT NULL, PRIMARY KEY (a, b))'); $this->addSql('CREATE INDEX "_ComposantConstructeurs_B_index" ON _composantconstructeurs (b)'); $this->addSql('CREATE INDEX IDX_5B97D813E8B7BE43 ON _composantconstructeurs (a)'); $this->addSql('CREATE TABLE _machineconstructeurs (a VARCHAR(36) NOT NULL, b VARCHAR(36) NOT NULL, PRIMARY KEY (a, b))'); $this->addSql('CREATE INDEX "_MachineConstructeurs_B_index" ON _machineconstructeurs (b)'); $this->addSql('CREATE INDEX IDX_4F225B32E8B7BE43 ON _machineconstructeurs (a)'); $this->addSql('CREATE TABLE _piececonstructeurs (a VARCHAR(36) NOT NULL, b VARCHAR(36) NOT NULL, PRIMARY KEY (a, b))'); $this->addSql('CREATE INDEX "_PieceConstructeurs_B_index" ON _piececonstructeurs (b)'); $this->addSql('CREATE INDEX IDX_77FC120E8B7BE43 ON _piececonstructeurs (a)'); $this->addSql('CREATE TABLE _productconstructeurs (a VARCHAR(36) NOT NULL, b VARCHAR(36) NOT NULL, PRIMARY KEY (a, b))'); $this->addSql('CREATE INDEX "_ProductConstructeurs_B_index" ON _productconstructeurs (b)'); $this->addSql('CREATE INDEX IDX_66F61802E8B7BE43 ON _productconstructeurs (a)'); $this->addSql('ALTER TABLE _composantconstructeurs ADD CONSTRAINT fk_607601254ad0cf31 FOREIGN KEY (b) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _composantconstructeurs ADD CONSTRAINT fk_60760125d3d99e8b FOREIGN KEY (a) REFERENCES composants (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _machineconstructeurs ADD CONSTRAINT fk_e6a040cc4ad0cf31 FOREIGN KEY (b) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _machineconstructeurs ADD CONSTRAINT fk_e6a040ccd3d99e8b FOREIGN KEY (a) REFERENCES machines (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _piececonstructeurs ADD CONSTRAINT fk_e94732e54ad0cf31 FOREIGN KEY (b) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _piececonstructeurs ADD CONSTRAINT fk_e94732e5d3d99e8b FOREIGN KEY (a) REFERENCES pieces (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _productconstructeurs ADD CONSTRAINT fk_cf7403fc4ad0cf31 FOREIGN KEY (b) REFERENCES constructeurs (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE _productconstructeurs ADD CONSTRAINT fk_cf7403fcd3d99e8b FOREIGN KEY (a) REFERENCES products (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); // Data migration: copy back from new link tables to old join tables $this->addSql('INSERT INTO _machineconstructeurs (a, b) SELECT machineid, constructeurid FROM machine_constructeur_links'); $this->addSql('INSERT INTO _piececonstructeurs (a, b) SELECT pieceid, constructeurid FROM piece_constructeur_links'); $this->addSql('INSERT INTO _composantconstructeurs (a, b) SELECT composantid, constructeurid FROM composant_constructeur_links'); $this->addSql('INSERT INTO _productconstructeurs (a, b) SELECT productid, constructeurid FROM product_constructeur_links'); // Drop new link tables $this->addSql('ALTER TABLE composant_constructeur_links DROP CONSTRAINT FK_139F3E3A345EE564'); $this->addSql('ALTER TABLE composant_constructeur_links DROP CONSTRAINT FK_139F3E3A70AF5AF0'); $this->addSql('ALTER TABLE machine_constructeur_links DROP CONSTRAINT FK_665B2620633EC4FD'); $this->addSql('ALTER TABLE machine_constructeur_links DROP CONSTRAINT FK_665B262070AF5AF0'); $this->addSql('ALTER TABLE piece_constructeur_links DROP CONSTRAINT FK_E664DAEC3C6A9D1'); $this->addSql('ALTER TABLE piece_constructeur_links DROP CONSTRAINT FK_E664DAEC70AF5AF0'); $this->addSql('ALTER TABLE product_constructeur_links DROP CONSTRAINT FK_E3D850B536799605'); $this->addSql('ALTER TABLE product_constructeur_links DROP CONSTRAINT FK_E3D850B570AF5AF0'); $this->addSql('DROP TABLE composant_constructeur_links'); $this->addSql('DROP TABLE machine_constructeur_links'); $this->addSql('DROP TABLE piece_constructeur_links'); $this->addSql('DROP TABLE product_constructeur_links'); } }