Odyssey
2018090400_keepAccessUseAllowed.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 KeepAccessUseAllowed extends AbstractMigration {
11  public function up() {
12 
13  $cuList = $this->fetchAll('select cu from cuadmin');
14  for ($i = 0; $i < count($cuList); $i ++) {
15  $cu = strtolower( trim( $cuList[$i]["cu"] ) );
16  $tableRights = $this->hasTable("${cu}memberacctrights");
17 
18  if ($tableRights) {
19  $sql = "insert into ${cu}memberacctrights (user_id, accountnumber, whichright, allowed, platform)
20  select a.user_id, a.accountnumber, 'ACCESS', false, null from (select row_number() over (partition by user_id, accountnumber) as rown, user_id, accountnumber
21  from ${cu}useraccounts) a
22  left join ${cu}memberacctrights b on a.user_id = b.user_id and a.accountnumber = b.accountnumber and b.whichright = 'ACCESS'
23  where a.rown = 1 and b.user_id is null";
24 
25  $this->query($sql);
26  }
27  }
28  }
29 }