8 use Phinx\Migration\AbstractMigration;
11 public function up() {
12 $table =
"cucmsresponse";
14 if ($this->hasTable($table)) {
16 $pkey =
"cucmsresponse_pkey";
17 $sql =
"alter table $table drop constraint $pkey";
20 $sql =
"alter table $table add constraint $pkey primary key (docsid, cu, user_id, accountnumber)";
27 $sql =
"select a.docsid as startid, b.docsid as stopid 28 from cucmsdocs a cross join cucmsdocs b 29 where a.docsname = 'esTermsStart' and b.docsname = 'esTermsStop'";
30 $row = $this->fetchRow($sql);
31 $startId = intval($row[
"startid"]);
32 $stopId = intval($row[
"stopid"]);
36 $sql =
"update $table set accountnumber = '' where docsid not in ($startId, $stopId) and accountnumber <> ''";
40 $sql =
"select upper(a.cu) as cu 41 from (select substring(table_name from 0 for (char_length(table_name) - 9)) as cu from information_schema.tables 42 where table_type = 'BASE TABLE' and table_schema = 'public' and table_name like '%memberacct') a 43 inner join information_schema.tables b on a.cu || 'user' = b.table_name 44 inner join information_schema.tables c on a.cu || 'useraccounts' = c.table_name 46 $rows = $this->fetchAll($sql);
48 $regex =
"select cr.docsid, '{{CU}}'::text as cu, u.user_id, ma1.accountnumber as account1, ma2.accountnumber as account2, 49 row_number() over (partition by cr.docsid, u.user_id, ma1.accountnumber) as rown 50 from {{CU}}useraccounts ua1 51 inner join $table cr on ua1.user_id = cr.user_id and cr.cu = '{{CU}}'::text and cr.docsid in ($startId, $stopId) 52 left join {{CU}}useraccounts ua2 on ua1.user_id = ua2.user_id and ua1.accountnumber <> ua2.accountnumber 53 inner join {{CU}}memberacct ma1 on ua1.accountnumber = ma1.accountnumber 54 left join {{CU}}memberacct ma2 on ua2.accountnumber = ma2.accountnumber 55 inner join {{CU}}user u on ua1.user_id = u.user_id 56 order by cr.docsid, u.user_id, ma1.accountnumber";
58 foreach($rows as $cuRow) {
60 $cu = trim($cuRow[
"cu"]);
61 $cuRegex = str_replace(
"{{CU}}", $cu, $regex);
65 $sql =
"update $table a set accountnumber = b.accountnumber 67 select t.docsid, t.cu, t.user_id, t.account1 as accountnumber from ($cuRegex) t where t.rown = 1 68 ) b where a.docsid = b.docsid and a.cu = b.cu and a.user_id = b.user_id";
73 $sql =
"insert into $table (docsid, cu, accountnumber, responseon, user_id) 74 select t.docsid, t.cu, t.account1, r.responseon, t.user_id from ($cuRegex) t 75 inner join $table r on t.docsid = r.docsid and t.cu = r.cu and t.user_id = r.user_id 76 and t.account1 <> r.accountnumber and t.rown = 1 and t.account2 is not null";
82 public function down() {