Odyssey
2017111500_ilikeIndex.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 IlikeIndex 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  print "ilike index up\n";
18 
19  // check if we already made the change
20  $indexname= "_username_1";
21  $tablename= "user"; // Without CU of course.
22 
23  // This query is going through all CUs and seeing if the index has been applied.
24  $query= "select a.cu, u.*, lower(indexdef) like '%lower%' as islower from cuadmin a
25  left join (select t.relname as table_name, i.relname as index_name from pg_class t inner join pg_index ix on t.oid = ix.indrelid inner join pg_class i on ix.indexrelid= i.oid
26  where t.relkind = 'r') u on lower(trim(a.cu) || '$tablename') = lower(trim(u.table_name)) and lower(trim(replace(u.index_name, u.table_name, ''))) = '$indexname'
27  left join pg_indexes ids on u.table_name = ids.tablename and u.index_name = ids.indexname order by a.cu, u.table_name, u.index_name";
28 
29  $rows= $this->fetchAll($query);
30 
31  for ( $i = 0, $count= count($rows); $i < $count; $i++ )
32  {
33  $row= $rows[$i];
34 
35  $removeIndex= isset($row["index_name"]) && !$row["islower"];
36  $addIndex= !isset($row["index_name"]) || !$row["islower"];
37 
38  $cu= strtolower(trim($row["cu"]));
39 
40  if ($removeIndex)
41  {
42  print "Removing previous username index for " . $cu . "...\n";
43  $this->query("drop index " . $cu . $tablename . $indexname);
44  }
45 
46  if ($addIndex)
47  {
48 
49  print "Adding ilike index for " . $cu . "...\n";
50  $this->query("create unique index " . $cu . $tablename . $indexname . " on " . $cu . $tablename . " ((lower(user_name)))");
51  }
52  }
53 
54  }
55 
56  /* To execute this you need to run the following command:
57  * php vendor/bin/phinx rollback (using correct path to phinx)
58  * Or, to roll back to a specific time:
59  * php vendor/bin/phinx rollback -t 20151123211207
60  */
61  public function down() {
62  print "ilike index up\n";
63 
64  // check if we already made the change
65  $indexname= "_username_1";
66  $tablename= "user"; // Without CU of course.
67 
68  // This query is going through all CUs and seeing if the index has been applied.
69  $query= "select a.cu, u.*, lower(indexdef) like '%lower%' as islower from cuadmin a
70  left join (select t.relname as table_name, i.relname as index_name from pg_class t inner join pg_index ix on t.oid = ix.indrelid inner join pg_class i on ix.indexrelid= i.oid
71  where t.relkind = 'r') u on lower(trim(a.cu) || '$tablename') = lower(trim(u.table_name)) and lower(trim(replace(u.index_name, u.table_name, ''))) = '$indexname'
72  left join pg_indexes ids on u.table_name = ids.tablename and u.index_name = ids.indexname order by a.cu, u.table_name, u.index_name";
73 
74  $rows= $this->fetchAll($query);
75 
76  print "tablename: $tablename";
77 
78  for ( $i = 0, $count= count($rows); $i < $count; $i++ )
79  {
80  $row= $rows[$i];
81 
82  $removeIndex= isset($row["index_name"]) && $row["islower"];
83  $addIndex= !isset($row["index_name"]) || $row["islower"];
84 
85  $cu= strtolower(trim($row["cu"]));
86 
87  if ($removeIndex)
88  {
89  print "Removing ilike index for " . $cu . "...\n";
90  $this->query("drop index " . $cu . $tablename . $indexname);
91  }
92 
93  if ($addIndex)
94  {
95 
96  print "Adding previous username index for " . $cu . "...\n";
97  $this->query("create unique index " . $cu . $tablename . $indexname . " on " . $cu . $tablename . " (user_name)");
98  }
99  }
100  }
101 }
102 
103 ?>