mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fix migration review: improve up and down query
This commit is contained in:
parent
de240b099f
commit
5bee607afb
@ -23,26 +23,8 @@ final class Version20210326113045 extends AbstractMigration
|
||||
}
|
||||
|
||||
/**
|
||||
* === UP ===>
|
||||
*
|
||||
* Preup and postup functions backup datas and transform them
|
||||
* before injecting them in the new many-to-many join table.
|
||||
* In these direction, there is no loss.
|
||||
*/
|
||||
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
|
||||
@ -51,6 +33,10 @@ final class Version20210326113045 extends AbstractMigration
|
||||
$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');
|
||||
|
||||
// insert datas in new join table
|
||||
$this->addSql('INSERT INTO persons_accompanying_periods (person_id, accompanyingperiod_id)'
|
||||
. 'SELECT person_id, id as accompagnying_period_id FROM chill_person_accompanying_period WHERE person_id IS NOT NULL');
|
||||
|
||||
// drop column
|
||||
$this->addSql('DROP INDEX idx_64a4a621217bbb47');
|
||||
@ -58,55 +44,27 @@ final class Version20210326113045 extends AbstractMigration
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <=== DOWN ===
|
||||
*
|
||||
* In predown and postdown we backup datas from jointable, and transform them
|
||||
* before injecting them back in the many-to-one column 'person_id'.
|
||||
*
|
||||
* The distinct clause makes that for each group of duplicates, it keeps only the first row in the returned result set.
|
||||
* Then we have only few lost datas. Lost datas: when many persons for one AccompanyingPeriod (keep only first person)
|
||||
*/
|
||||
|
||||
public function preDown(Schema $schema) : void
|
||||
{
|
||||
// extract datas
|
||||
$query = "SELECT DISTINCT ON (accompanyingperiod_id) accompanyingperiod_id AS id, person_id FROM persons_accompanying_periods ORDER BY id, person_id ASC";
|
||||
$data = $this->connection->prepare($query);
|
||||
$data->execute();
|
||||
foreach ($data as $row)
|
||||
{
|
||||
$id = $row['id'];
|
||||
$personId = $row['person_id'];
|
||||
$this->datas[$id] = "$personId";
|
||||
}
|
||||
}
|
||||
|
||||
public function down(Schema $schema) : void
|
||||
{
|
||||
// drop join table
|
||||
$this->addSql('DROP TABLE persons_accompanying_periods');
|
||||
|
||||
// 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)');
|
||||
}
|
||||
|
||||
public function postDown(Schema $schema) : void
|
||||
{
|
||||
|
||||
// insert datas in existing table
|
||||
foreach ($this->datas as $id => $personId) {
|
||||
$SQL = "UPDATE chill_person_accompanying_period SET person_id='$personId' WHERE id='$id'";
|
||||
$this->connection->executeQuery($SQL); // CHECK if we have to execute query out of foreach loop. how ?
|
||||
}
|
||||
$this->addSql('UPDATE chill_person_accompanying_period AS ap '
|
||||
. 'SET person_id = jt.person_id '
|
||||
. 'FROM ( '
|
||||
. 'SELECT DISTINCT ON (accompanyingperiod_id) accompanyingperiod_id AS id, person_id FROM persons_accompanying_periods '
|
||||
. 'ORDER BY id, person_id ASC '
|
||||
. ') AS jt '
|
||||
. 'WHERE ap.id = jt.id');
|
||||
|
||||
// drop join table
|
||||
$this->addSql('DROP TABLE persons_accompanying_periods');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user