Odyssey
2017051500_m2m.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 M2M 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  // add to common tables
18  $tableName = "cu_feature";
19  $row = $this->fetchRow( "SELECT * FROM cu_feature WHERE feature_code = 'TRNM2M'" );
20  if ( !$row ) {
21  $singleRow = ["feature_code" => "TRNM2M",
22  "description" => "Member to member transfer",
23  "limit_type" => "B",
24  "category" => null,
25  "enabled" => true ];
26  $table = $this->table( $tableName );
27  $table->insert($singleRow)->saveData();
28  }
29 
30  // get the list of credit unions
31  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
32 
33  for ( $i = 0; $i < count( $cuList ); $i++ ) {
34  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
35  $CU = strtoupper( $cu );
36 
37  $tableName = "cu_featuremenu";
38  $row = $this->fetchRow( "SELECT * FROM cu_featuremenu WHERE feature_code = 'TRNM2M' AND cu='{$CU}'" );
39  if ( !$row ) {
40  $singleRow = ["feature_code" => "TRNM2M",
41  "cu" => $CU,
42  "display_order" => 12,
43  "parent_item_id" => 32,
44  "menu_item_type" => "D",
45  "menu_item_platform" => '["D","M"]',
46  "menu_item_attr" => '{"display":{"en_US":"M2M Accounts","es_US":""},"fa-icon":"","href":"hcuM2MAccts.prg","hrefUrlQuery":1,"target":0,"hrefExtraParam":""}' ];
47  $table = $this->table( $tableName );
48  $table->insert($singleRow)->saveData();
49  }
50 
51  // check if we already made the change to the table
52  $tableName = "{$cu}extaccount";
53  $table = $this->table( $tableName );
54  $hasColumn = $table->hasColumn('type');
55 
56  // test to see if change has already happened
57  if ( !$hasColumn ) {
58  // announce it
59  print "Converting $cu {$tableName}\n";
60 
61  // create the SQL for the temp table
62  $sql = "CREATE TABLE {$cu}extaccounttemp (
63  id SERIAL PRIMARY KEY,
64  user_id integer NOT NULL,
65  type char(3),
66  display_name varchar(20),
67  status character(1),
68  remote_info varchar
69  )";
70 
71  // create the extaccount table
72  $rows = $this->query( $sql );
73 
74  // move existing data
75  $sql = "INSERT INTO {$cu}extaccounttemp
76  (id, user_id, type, display_name,
77  status, remote_info)
78  SELECT id, user_id, 'EXT' as type, display_name,
79  status, remote_info FROM $tableName";
80 
81  // copy the data
82  $count = $this->execute( $sql );
83 
84  print "$count row(s) copied from existing $tableName";
85 
86  // drop the old table
87  $this->dropTable( $tableName );
88 
89  // rename the temporary table
90  $table = $this->table( "{$cu}extaccounttemp" );
91  $table->rename( $tableName );
92 
93  // add an index so the display name is unique per user (for either type of table usage)
94  $sql = "CREATE UNIQUE INDEX {$cu}extaccount_name_key on {$cu}extaccount USING btree ( user_id, display_name );";
95  $rows = $this->query( $sql );
96 
97  // add some comments
98  $sql = "COMMENT ON COLUMN {$cu}extaccount.user_id is 'User who set it up; foreign key to <cu>user.user_id';";
99  $rows = $this->query( $sql );
100 
101  $sql = "COMMENT ON COLUMN {$cu}extaccount.type is 'EXT, M2M';";
102  $rows = $this->query( $sql );
103 
104  $sql = "COMMENT ON COLUMN {$cu}extaccount.display_name is 'Name that displays in Banking';";
105  $rows = $this->query( $sql );
106 
107  $sql = "COMMENT ON COLUMN {$cu}extaccount.status is 'Pending/Active/Inactive flag';";
108  $rows = $this->query( $sql );
109 
110  $sql = "COMMENT ON COLUMN {$cu}extaccount.remote_info is 'JSON object that holds the remote account information';";
111  $rows = $this->query( $sql );
112 
113  }
114  }
115  }
116 
117  /* To execute this you need to run the following command:
118  * php vendor/bin/phinx rollback (using correct path to phinx)
119  * Or, to roll back to a specific time:
120  * php vendor/bin/phinx rollback -t 20151123211207
121  */
122  public function down()
123  {
124  // get the list of credit unions
125  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
126 
127  for ( $i = 0; $i < count( $cuList ); $i++ ) {
128  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
129 
130  // check if we already made the change
131  $tableName = "{$cu}extaccount";
132  $hasColumn = $table->hasColumn('type');
133 
134  // test to see if table has NOT been reversed yet
135  if ( $hasColumn ) {
136  // announce it
137  print "Reverting $cu {$tableName}\n";
138 
139  // drop the columns that was added
140  $table->removeColumn('type')->save();
141  }
142  }
143  }
144 }