Odyssey
2017030100_transhdr.php
1 <?php
2 /* Notes:
3  * 1. Anything complicated needs to be done with SQL.
4  * 2. Always have a check to know if the migration needs to occur (up or down).
5  * 3. Use up() and down(), not change(), because of the SQL
6  */
7 use Phinx\Migration\AbstractMigration;
8 
9 class TransHdr extends AbstractMigration
10 {
11  /* This is the process for moving forward. We are re-creating the table so if there is
12  * existing data will need to create a temporary table, copy the data, drop the old table,
13  * rename the temporary table.
14  */
15  public function up()
16  {
17  // get the list of credit unions
18  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
19 
20  for ( $i = 0; $i < count( $cuList ); $i++ ) {
21  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
22 
23  // check if we already made the change
24  $tableName = "{$cu}transhdr";
25  $table = $this->table( $tableName );
26  $hasColumn = $table->hasColumn('approved_status');
27 
28  // test to see if change has already happened
29  if ( !$hasColumn ) {
30  // announce it
31  print "Converting $cu {$tableName}\n";
32 
33  // create the SQL for the temp table
34  $sql = "CREATE TABLE {$cu}transhdrtemp (
35  id SERIAL PRIMARY KEY,
36  feature_code character varying(10) NOT NULL,
37  effective_date date,
38  posted_by integer,
39  posted_date timestamp without time zone,
40  approved_by integer,
41  approved_date timestamp without time zone,
42  approved_status smallint,
43  processed_by character varying(50),
44  processed_date timestamp without time zone,
45  processed_status smallint,
46  accountnumber character(12),
47  accounttype character(12),
48  deposittype character(1),
49  transactioncode character(2),
50  memo character varying(255)
51  )";
52 
53  // create the temp table
54  $rows = $this->query( $sql );
55 
56  // add some comments
57  $sql = "COMMENT ON COLUMN {$cu}transhdrtemp.feature_code is 'Same feature code as menuing'";
58  $rows = $this->query( $sql );
59 
60  $sql = "COMMENT ON COLUMN {$cu}transhdrtemp.approved_status is 'Approved(10) or cancelled(99)'";
61  $rows = $this->query( $sql );
62 
63  $sql = "COMMENT ON COLUMN {$cu}transhdrtemp.processed_status is 'Waiting(0), Approved/in-batch(10), Emailed(20), or cancelled(99)';";
64  $rows = $this->query( $sql );
65 
66  // move existing data
67  $sql = "INSERT INTO {$cu}transhdrtemp
68  (id, feature_code, effective_date,
69  posted_by, posted_date, approved_by, approved_date,
70  accountnumber, accounttype,
71  deposittype, transactioncode, memo)
72  SELECT id, feature_code, effective_date date,
73  posted_by, posted_date, approved_by, approved_date,
74  accountnumber, accounttype,
75  deposittype, transactioncode, memo FROM $tableName";
76 
77  // copy the data
78  $count = $this->execute( $sql );
79 
80  print "$count row(s) copied from existing $tableName";
81 
82  // drop the old table
83  $this->dropTable( $tableName );
84 
85  // rename the temporary table
86  $table = $this->table( "{$cu}transhdrtemp" );
87  $table->rename( $tableName );
88  }
89  }
90  }
91 
92  /* To execute this you need to run the following command:
93  * php vendor/bin/phinx rollback (using correct path to phinx)
94  * Or, to roll back to a specific time:
95  * php vendor/bin/phinx rollback -t 20151123211207
96  */
97  public function down()
98  {
99  // get the list of credit unions
100  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
101 
102  for ( $i = 0; $i < count( $cuList ); $i++ ) {
103  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
104 
105  // check if we already made the change
106  $tableName = "{$cu}transhdr";
107  $table = $this->table( $tableName );
108  $hasColumn = $table->hasColumn('approved_status');
109 
110  // test to see if table has NOT been reversed yet
111  if ( $hasColumn ) {
112  // announce it
113  print "Reverting $cu {$tableName}\n";
114 
115  // change the column back (need to cast to get character changed to integer)
116  $sql = "ALTER TABLE $tableName ALTER COLUMN processed_by TYPE integer USING processed_by::integer";
117  $rows = $this->query( $sql );
118 
119  // drop the columns that were added
120  $table->removeColumn('processed_status')
121  ->save();
122  $table->removeColumn('approved_status')
123  ->save();
124  }
125  }
126  }
127 }