8 use Phinx\Migration\AbstractMigration;
11 public function up() {
13 $cuList = $this->fetchAll(
'SELECT cu FROM cuadmin');
16 foreach ($cuList as $cuRow) {
17 $cu = strtolower(trim($cuRow[
"cu"]));
18 $indexName =
"${cu}idx_audituser";
21 $tableName =
"{$cu}audituser";
23 if ($this->hasTable($tableName)) {
24 $table = $this->table( $tableName );
26 $hasColumn = $table->hasColumn(
'accountnumber');
31 print
"Upgrade $cu {$tableName}.\n";
34 $this->query(
"alter table $tableName add column accountnumber varchar(12) default ''");
37 $this->query(
"alter table $tableName drop constraint $indexName");
40 $this->query(
"alter table $tableName add constraint $indexName primary key (user_id, accountnumber, auditdate, auditaction)");
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");
49 print
"Skip $cu which doesn't have the user audit table.\n";
55 public function down() {
57 $cuList = $this->fetchAll(
'SELECT cu FROM cuadmin');
60 foreach ($cuList as $cuRow) {
61 $cu = strtolower(trim($cuRow[
"cu"]));
62 $indexName =
"${cu}idx_audituser";
65 $tableName =
"{$cu}audituser";
67 if ($this->hasTable($tableName)) {
68 $table = $this->table( $tableName );
69 $hasColumn = $table->hasColumn(
'accountnumber');
74 print
"Downgrade $cu {$tableName}.\n";
77 $this->query(
"alter table $tableName drop constraint $indexName");
80 $this->query(
"alter table $tableName add constraint $indexName primary key (user_id, auditdate, auditaction)");
83 $this->query(
"alter table $tableName drop column accountnumber");
86 print
"Skip $cu which doesn't have the user audit table.\n";