-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to convert first task on all surveys to post stage
All surveys need a 'post' stage. We'll convert the first stage to a post stage if it isn't done already
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
migrations/20160902084943_migrate_first_task_to_post_stage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class MigrateFirstTaskToPostStage extends AbstractMigration | ||
{ | ||
/** | ||
* Migrate Up. | ||
*/ | ||
public function up() | ||
{ | ||
$pdo = $this->getAdapter()->getConnection(); | ||
|
||
// Get first stage for each form | ||
$rows = $this->fetchAll( | ||
"SELECT a.id | ||
FROM form_stages a | ||
LEFT JOIN form_stages b -- JOIN for priority | ||
ON ( | ||
b.form_id = a.form_id | ||
AND b.priority < a.priority | ||
) | ||
LEFT JOIN form_stages c -- JOIN for priority ties | ||
ON ( | ||
c.form_id = a.form_id | ||
AND c.priority = a.priority | ||
AND c.id < a.id | ||
) | ||
WHERE b.id IS NULL AND c.id IS NULL AND a.form_id NOT IN ( | ||
SELECT form_id from form_stages WHERE form_stages.type = 'post' | ||
)" | ||
); | ||
|
||
$update = $pdo->prepare( | ||
"UPDATE | ||
form_stages fs | ||
SET type = 'post', required = 0 | ||
WHERE id = :id" | ||
); | ||
|
||
foreach ($rows as $row) { | ||
$update->execute( | ||
[ | ||
':id' => $row['id'] | ||
] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Migrate Down. | ||
*/ | ||
public function down() | ||
{ | ||
$this->execute("UPDATE form_stages fs SET type = 'task', required = 1, priority = 0 | ||
WHERE type = 'post'"); | ||
} | ||
} |