Odyssey
2017121800_addcrossaccountstable.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 AddCrossAccountsTable 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 the new 'CrossAccounts' table for each CU that exists in the current database
18  // get the list of credit unions
19  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
20 
21  for ( $i = 0; $i < count( $cuList ); $i++ ) {
22  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
23 
24  /**
25  * Check for existence of file name
26  */
27  $tableName = "{$cu}crossaccounts";
28 
29  $exists = $this->hasTable($tableName);
30 
31  if (!$exists) {
32  /**
33  * Table does not exist --
34  * CREATE
35  */
36 
37  $sql = "
38  CREATE TABLE {$tableName} (
39  accountnumber character (12) NOT NULL,
40  tomember character(12) NOT NULL,
41  accounttype character(12) NOT NULL,
42  deposittype character (1) NOT NULL,
43  description character varying (255),
44  misc1 character varying (255)
45  );";
46  $sqlRs = $this->query($sql);
47 
48  /* ADD PRIMARY KEY */
49  $sql = "ALTER TABLE ONLY {$tableName} ADD CONSTRAINT {$tableName}_pkey PRIMARY KEY (accountnumber, tomember, accounttype, deposittype);";
50  $sqlRs = $this->query($sql);
51 
52  }
53  }
54  }
55 
56 }