Odyssey
2017050800_memberacct.php
1 <?php
2 /**
3  * PURPOSE:
4  * The purpose of this script is to add two columns
5  * balance_stamp
6  * balance_attemp
7  * to the <client>memberacct table.
8  * This script will first verify the fields don't already exist,
9  * ADD - if the fields are not present
10  * SKIP - if the fields are present
11  * Notes:
12  * 1. Anything complicated needs to be done with SQL.
13  * 2. Always have a check to know if the migration needs to occur (up or down).
14  * 3. Use up() and down(), not change(), because of the SQL
15  */
16 use Phinx\Migration\AbstractMigration;
17 
18 class MemberAcct extends AbstractMigration {
19  /* This is the process for moving forward. We are re-creating the table so if there is
20  * existing data will need to create a temporary table, copy the data, drop the old table,
21  * rename the temporary table.
22  */
23  public function up() {
24  // get the list of credit unions
25  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
26 
27  for ( $i = 0; $i < count( $cuList ); $i++ ) {
28  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
29 
30  // check if we already made the change
31  $tableName = "{$cu}memberacct";
32  $table = $this->table( $tableName );
33  $hasColumn = $table->hasColumn('balance_stamp');
34 
35  // test to see if change has already happened
36  if ( !$hasColumn ) {
37  // announce it
38  print "Migration - Upgrade $cu {$tableName}\n";
39 
40  /*
41  * ADD COLUMNS
42  */
43  // ** balance_stamp
44  $table->addColumn('balance_stamp', 'biginteger', array('null' => true))
45  ->save();
46 
47  // ** balance_attempt
48  $table->addColumn('balance_attempt', 'biginteger', array('null' => true))
49  ->save();
50 
51  }
52  }
53  }
54 
55  /* To execute this you need to run the following command:
56  * php vendor/bin/phinx rollback (using correct path to phinx)
57  * Or, to roll back to a specific time:
58  * php vendor/bin/phinx rollback -t 20151123211207
59  */
60  public function down() {
61  // get the list of credit unions
62  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
63 
64  for ( $i = 0; $i < count( $cuList ); $i++ ) {
65  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
66 
67  // check if we already made the change
68  $tableName = "{$cu}memberacct";
69  $table = $this->table( $tableName );
70  $hasColumn = $table->hasColumn('balance_stamp');
71 
72  // test to see if table has NOT been reversed yet
73  if ( $hasColumn ) {
74  // announce it
75  print "Migration - Downgrade $cu {$tableName}\n";
76 
77  /*
78  * REMOVE COLUMNS
79  */
80  // ** balance_stamp
81  $table->removeColumn('balance_stamp')
82  ->save();
83 
84  // ** balance_attempt
85  $table->removeColumn('balance_attempt')
86  ->save();
87  }
88  }
89  }
90 }