mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
add migration down query to preserve datas
This commit is contained in:
parent
f6801c0c4f
commit
dcc6969310
@ -22,6 +22,13 @@ final class Version20210326113045 extends AbstractMigration
|
||||
return 'Change model relation between Person and AccompagnyingPeriod, without losing datas when going up';
|
||||
}
|
||||
|
||||
/**
|
||||
* === 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
|
||||
@ -59,18 +66,47 @@ final class Version20210326113045 extends AbstractMigration
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <=== 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');
|
||||
|
||||
// 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
|
||||
$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 ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user