Odyssey
2018050100_grouptaxid.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 GroupTaxID 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  $tableName = "{$cu}group";
19 
20  // check if table exists
21  if ($this->hasTable($tableName)) {
22  $table = $this->table($tableName);
23  $tableColumn = "tax_id";
24 
25  // add column if it doesn't exist
26  if (!$table->hasColumn($tableColumn)) {
27  $this->query("ALTER TABLE {$cu}group ADD COLUMN tax_id varchar(10)");
28  }
29  }
30  }
31  }
32 
33  public function down() {
34  // get the list of credit unions
35  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
36 
37 
38  foreach ($cuList as $cuRow) {
39  $cu = strtolower(trim($cuRow["cu"]));
40  $tableName = "{$cu}group";
41 
42  // check if table exists
43  if ($this->hasTable($tableName)) {
44  $table = $this->table($tableName);
45  $tableColumn = "tax_id";
46 
47  // remove column if it exists
48  if ($table->hasColumn($tableColumn)) {
49  $this->query("ALTER TABLE {$cu}group DROP COLUMN tax_id");
50  }
51  }
52  }
53  }
54 }