Odyssey
2018041600_emailnotify.php
1 <?php
2 /**
3  * Notes:
4  * 1. Anything complicated needs to be done with SQL.
5  * 2. Always have a check to know if the migration needs to occur (up or down).
6  * 3. Use up() and down(), not change(), because of the SQL
7  */
8 use Phinx\Migration\AbstractMigration;
9 
10 class EmailNotify extends AbstractMigration {
11  public function up() {
12  // get the list of credit unions
13  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
14 
15 
16  foreach ($cuList as $cuRow) {
17  $cu = strtolower(trim($cuRow["cu"]));
18  $cuTableExists = $this->hasTable($cu . "transdtl");
19 
20  if ($cuTableExists) {
21  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify DROP DEFAULT");
22  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify TYPE smallint USING CASE WHEN email_notify=TRUE THEN 1 ELSE 0 END");
23  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify SET DEFAULT 0");
24  }
25  }
26  }
27 
28  public function down() {
29  // get the list of credit unions
30  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
31 
32 
33  foreach ($cuList as $cuRow) {
34  $cu = strtolower(trim($cuRow["cu"]));
35  $cuTableExists = $this->hasTable($cu . "transdtl");
36 
37  if ($cuTableExists) {
38  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify DROP DEFAULT");
39  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify TYPE bool USING CASE WHEN email_notify=0 THEN FALSE ELSE TRUE END");
40  $this->query("ALTER TABLE {$cu}transdtl ALTER COLUMN email_notify SET DEFAULT FALSE");
41  }
42  }
43  }
44 }