Odyssey
2018041001_localidxupdate.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 LocalIdxUpdate 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  // loop through tables to update
24  $tableNames = array(
25  "achtemplate" => array("title"=>"group_idx", "cols"=>"tmpl_id, group_id"),
26  "extkey" => array("title"=>"provider_idx", "cols"=>"providermode, accountnumber, id"),
27  "transhdr" => array("title"=>"feature_idx", "cols"=>"id, feature_code"),
28  "transdtl" => array("title"=>"transhdr_idx", "cols"=>"transhdr_id"),
29  "userlogins" => array("title"=>"user_idx", "cols"=>"user_id, user_name, hcucode")
30  );
31 
32  foreach ($tableNames as $table => $index) {
33  $tableName = $cu . $table;
34  $tableExists = $this->hasTable( $tableName );
35  $indexName = $cu . $table . "_" . $index['title'];
36  $indexColumns = $index['cols'];
37 
38  // check if index exists for this table
39  $sql = "
40  SELECT COUNT(*) FROM pg_class pgc
41  WHERE pgc.relname = '{$indexName}'
42  AND pgc.relkind = 'i'";
43 
44  $idxCount = $this->fetchAll($sql);
45  $idxExists = intval($idxCount[0]['count']);
46 
47  if ($tableExists && $idxExists == 0) {
48  $sql = "CREATE INDEX {$indexName} ON {$tableName} USING btree ($indexColumns)";
49  $sqlRs = $this->query($sql);
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  {
62 
63  // get the list of credit unions
64  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
65 
66  for ( $i = 0; $i < count( $cuList ); $i++ ) {
67  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
68 
69  // loop through tables to update
70  $tableNames = array(
71  "achtemplate" => array("title"=>"group_idx", "cols"=>"tmpl_id, group_id"),
72  "extkey" => array("title"=>"provider_idx", "cols"=>"providermode, accountnumber, id"),
73  "transhdr" => array("title"=>"feature_idx", "cols"=>"id, feature_code"),
74  "transdtl" => array("title"=>"transhdr_idx", "cols"=>"transhdr_id"),
75  "userlogins" => array("title"=>"user_idx", "cols"=>"user_id, user_name, hcucode")
76  );
77 
78  foreach ($tableNames as $table => $index) {
79  $tableName = $cu . $table;
80  $tableExists = $this->hasTable( $tableName );
81  $indexName = $cu . $table . "_" . $index['title'];
82  $indexColumns = $index['cols'];
83 
84  // check if index exists for this table
85  $sql = "
86  SELECT COUNT(*) FROM pg_class pgc
87  WHERE pgc.relname = '{$indexName}'
88  AND pgc.relkind = 'i'";
89 
90  $idxCount = $this->fetchAll($sql);
91  $idxExists = intval($idxCount[0]['count']);
92 
93  if ($tableExists && $idxExists > 0) {
94  $sql = "DROP INDEX {$indexName}";
95  $sqlRs = $this->query($sql);
96  }
97  }
98  }
99  }
100 }