Skip to content

Commit

Permalink
Add migration to convert first task on all surveys to post stage
Browse files Browse the repository at this point in the history
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
rjmackay committed Sep 2, 2016
1 parent b7d7c13 commit f157431
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions migrations/20160902084943_migrate_first_task_to_post_stage.php
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'");
}
}

0 comments on commit f157431

Please sign in to comment.