mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
make new relation many-to-many between Person and AccompagnyingPeriod
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\Migrations\Person;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Change model relation between Person and AccompagnyingPeriod
|
||||
* Migrate datas into new join table
|
||||
*
|
||||
* @author Mathieu Jaumotte mathieu.jaumotte@champs-libres.coop
|
||||
*/
|
||||
final class Version20210326113045 extends AbstractMigration
|
||||
{
|
||||
private $datas = [];
|
||||
|
||||
public function getDescription() : string
|
||||
{
|
||||
return 'Change model relation between Person and AccompagnyingPeriod, without losing datas when going up';
|
||||
}
|
||||
|
||||
public function preUp(Schema $schema) : void
|
||||
{
|
||||
// extract datas
|
||||
$query = "SELECT person_id, id as accompagnying_period_id FROM chill_person_accompanying_period WHERE person_id IS NOT NULL";
|
||||
$data = $this->connection->prepare($query);
|
||||
$data->execute();
|
||||
foreach ($data as $row)
|
||||
{
|
||||
$personId = $row['person_id'];
|
||||
$accompagnyingPeriodId = $row['accompagnying_period_id'];
|
||||
$this->datas[] = "($personId, $accompagnyingPeriodId)";
|
||||
}
|
||||
}
|
||||
|
||||
public function up(Schema $schema) : void
|
||||
{
|
||||
// create join table
|
||||
$this->addSql('CREATE TABLE persons_accompanying_periods (person_id INT NOT NULL, accompanyingperiod_id INT NOT NULL, PRIMARY KEY(person_id, accompanyingperiod_id))');
|
||||
$this->addSql('CREATE INDEX IDX_49A3871F217BBB47 ON persons_accompanying_periods (person_id)');
|
||||
$this->addSql('CREATE INDEX IDX_49A3871F550B0C53 ON persons_accompanying_periods (accompanyingperiod_id)');
|
||||
$this->addSql('ALTER TABLE persons_accompanying_periods ADD CONSTRAINT FK_49A3871F217BBB47 FOREIGN KEY (person_id) REFERENCES chill_person_person (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
$this->addSql('ALTER TABLE persons_accompanying_periods ADD CONSTRAINT FK_49A3871F550B0C53 FOREIGN KEY (accompanyingperiod_id) REFERENCES chill_person_accompanying_period (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
|
||||
// drop column
|
||||
$this->addSql('DROP INDEX idx_64a4a621217bbb47');
|
||||
$this->addSql('ALTER TABLE chill_person_accompanying_period DROP CONSTRAINT fk_64a4a621217bbb47');
|
||||
$this->addSql('ALTER TABLE chill_person_accompanying_period DROP person_id');
|
||||
}
|
||||
|
||||
public function postUp(Schema $schema) : void
|
||||
{
|
||||
// insert datas in new join table
|
||||
$SQL = "INSERT INTO persons_accompanying_periods (person_id, accompanyingperiod_id) VALUES " . implode(', ', $this->datas);
|
||||
$this->connection->executeQuery($SQL);
|
||||
}
|
||||
|
||||
|
||||
public function down(Schema $schema) : void
|
||||
{
|
||||
// drop join table
|
||||
//$this->addSql('DROP TABLE persons_accompanying_periods');
|
||||
|
||||
// INFO this dangerous query is commented, then if you go down you must manually :
|
||||
// * insert datas in TABLE chill_person_accompanying_period COLUMN person_id ;
|
||||
// * drop join TABLE persons_accompanying_periods when you are sure it works
|
||||
|
||||
// add column
|
||||
$this->addSql('ALTER TABLE chill_person_accompanying_period ADD person_id INT DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE chill_person_accompanying_period ADD CONSTRAINT fk_64a4a621217bbb47 FOREIGN KEY (person_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
$this->addSql('CREATE INDEX idx_64a4a621217bbb47 ON chill_person_accompanying_period (person_id)');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user