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