Odyssey
2018032900_auditMember.php
1 <?php
2 /**
3  * Notes:
4  * 1. Anything complicated needs to be done with SQL.
5  * 2. Always have a check to know if the migration needs to occur (up or down).
6  * 3. Use up() and down(), not change(), because of the SQL
7  */
8 use Phinx\Migration\AbstractMigration;
9 
10 class AuditMember extends AbstractMigration {
11  public function up() {
12  // get the list of credit unions
13  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
14 
15 
16  foreach ($cuList as $cuRow) {
17  $cu = strtolower(trim($cuRow["cu"]));
18  $indexName = "${cu}idx_audituser";
19 
20  // check if we already made the change
21  $tableName = "{$cu}audituser";
22 
23  if ($this->hasTable($tableName)) { // Not all Credit Unions have this table. Webonly Credit Unions don't have a lot of the CU-specific tables.
24  $table = $this->table( $tableName );
25 
26  $hasColumn = $table->hasColumn('accountnumber');
27 
28  // test to see if change has already happened
29  if ( !$hasColumn ) {
30  // announce it
31  print "Upgrade $cu {$tableName}.\n";
32 
33  // Add column
34  $this->query("alter table $tableName add column accountnumber varchar(12) default ''");
35 
36  // Drop primary key (doesn't have account_number in it.)
37  $this->query("alter table $tableName drop constraint $indexName");
38 
39  // Add primary key with account_number in it.
40  $this->query("alter table $tableName add constraint $indexName primary key (user_id, accountnumber, auditdate, auditaction)");
41 
42  // Add accountnumber to previous e-statement start/stop audits.
43  $this->query("update ${cu}audituser u set accountnumber = a.accountnumber from
44  (select user_id, accountnumber as prevaccount, auditdate, auditaction, auditrecbefore::json->'memberacct'->0->>'accountnumber' as accountnumber from ${cu}audituser
45  where auditfulldesc = 'E-Statement Update' and auditsrccode_context in ('hcuStatement.prg', 'hcuStatementStop.prg') and nullif(trim(accountnumber), '') is null) a
46  where u.user_id = a.user_id and u.accountnumber = a.prevaccount and u.auditdate = a.auditdate and u.auditaction = a.auditaction");
47  }
48  } else {
49  print "Skip $cu which doesn't have the user audit table.\n";
50  }
51 
52  }
53  } // End "up" function.
54 
55  public function down() {
56  // get the list of credit unions
57  $cuList = $this->fetchAll('SELECT cu FROM cuadmin');
58 
59 
60  foreach ($cuList as $cuRow) {
61  $cu = strtolower(trim($cuRow["cu"]));
62  $indexName = "${cu}idx_audituser";
63 
64  // check if we already made the change
65  $tableName = "{$cu}audituser";
66 
67  if ($this->hasTable($tableName)) { // Not all Credit Unions have this table. Webonly Credit Unions don't have a lot of the CU-specific tables.
68  $table = $this->table( $tableName );
69  $hasColumn = $table->hasColumn('accountnumber');
70 
71  // test to see if change has already happened
72  if ( $hasColumn ) {
73  // announce it
74  print "Downgrade $cu {$tableName}.\n";
75 
76  // Drop primary key
77  $this->query("alter table $tableName drop constraint $indexName");
78 
79  // Add primary key without account_number in it.
80  $this->query("alter table $tableName add constraint $indexName primary key (user_id, auditdate, auditaction)");
81 
82  // drop column
83  $this->query("alter table $tableName drop column accountnumber");
84  }
85  } else {
86  print "Skip $cu which doesn't have the user audit table.\n";
87  }
88 
89  }
90  } // End "down" function
91 }