Odyssey
2017040300_extaccount.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 ExtAccount 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}extaccount";
25  $exists = $this->hasTable( $tableName );
26  if ( !$exists ) {
27  // announce it
28  print "Adding table: $cu {$tableName}\n";
29 
30  // create the SQL for the extaccount table
31  $sql = "CREATE TABLE {$cu}extaccount (
32  id SERIAL PRIMARY KEY,
33  user_id integer NOT NULL,
34  display_name varchar(20),
35  status character(1),
36  remote_info varchar
37  )";
38 
39  // create the extaccount table
40  $rows = $this->query( $sql );
41 
42  // add an index so the display name is unique per user
43  $sql = "CREATE UNIQUE INDEX {$cu}extaccount_name_key on {$cu}extaccount USING btree ( user_id, display_name );";
44  $rows = $this->query( $sql );
45 
46  // add some comments
47  $sql = "COMMENT ON COLUMN {$cu}extaccount.user_id is 'User who set it up; foreign key to <cu>user.user_id';";
48  $rows = $this->query( $sql );
49 
50  $sql = "COMMENT ON COLUMN {$cu}extaccount.display_name is 'Name that displays in Banking';";
51  $rows = $this->query( $sql );
52 
53  $sql = "COMMENT ON COLUMN {$cu}extaccount.status is 'Pending/Active/Inactive flag';";
54  $rows = $this->query( $sql );
55 
56  $sql = "COMMENT ON COLUMN {$cu}extaccount.remote_info is 'JSON object that holds the remote account information';";
57  $rows = $this->query( $sql );
58 
59  }
60  }
61  }
62 
63  /* To execute this you need to run the following command:
64  * php vendor/bin/phinx rollback (using correct path to phinx)
65  * Or, to roll back to a specific time:
66  * php vendor/bin/phinx rollback -t 20151123211207
67  */
68  public function down()
69  {
70  // get the list of credit unions
71  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
72 
73  for ( $i = 0; $i < count( $cuList ); $i++ ) {
74  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
75 
76  // check if we already made the change
77  $tableName = "{$cu}extaccount";
78  if ( $this->hasTable( $tableName ) ) {
79  // announce it
80  print "Dropping $cu {$tableName}\n";
81  $this->dropTable( $tableName );
82  }
83  }
84  }
85 }