Odyssey
2018042000_allowEnroll.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 AllowEnroll 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 
19  // check if we already made the change
20  $tableName = "{$cu}memberacct";
21 
22  if ($this->hasTable($tableName)) { // Not all Credit Unions have this table. Webonly Credit Unions don't have a lot of the CU-specific tables.
23  $table = $this->table( $tableName );
24 
25  $hasColumn = $table->hasColumn('allowenroll');
26 
27  // test to see if change has already happened
28  if ( !$hasColumn ) {
29  // announce it
30  print "Upgrade $cu {$tableName}.\n";
31 
32  // Add column
33  $this->query("alter table $tableName add column allowenroll boolean default false");
34  }
35  } else {
36  print "Skip $cu which doesn't have the member acct table.\n";
37  }
38 
39  }
40  } // End "up" function.
41 
42  public function down() {
43  // get the list of credit unions
44  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
45 
46 
47  foreach ($cuList as $cuRow) {
48  $cu = strtolower(trim($cuRow["cu"]));
49 
50  // check if we already made the change
51  $tableName = "{$cu}memberacct";
52 
53  if ($this->hasTable($tableName)) { // Not all Credit Unions have this table. Webonly Credit Unions don't have a lot of the CU-specific tables.
54  $table = $this->table( $tableName );
55  $hasColumn = $table->hasColumn('allowenroll');
56 
57  // test to see if change has already happened
58  if ( $hasColumn ) {
59  // announce it
60  print "Downgrade $cu {$tableName}.\n";
61 
62  // drop column
63  $this->query("alter table $tableName drop column allowenroll");
64  }
65  } else {
66  print "Skip $cu which doesn't have the member acct table.\n";
67  }
68 
69  }
70  } // End "down" function
71 }