7 use Phinx\Migration\AbstractMigration;
15 public function up() {
17 print
"ilike index up\n";
20 $indexname=
"_username_1";
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";
29 $rows= $this->fetchAll($query);
31 for ( $i = 0, $count= count($rows); $i < $count; $i++ )
35 $removeIndex= isset($row[
"index_name"]) && !$row[
"islower"];
36 $addIndex= !isset($row[
"index_name"]) || !$row[
"islower"];
38 $cu= strtolower(trim($row[
"cu"]));
42 print
"Removing previous username index for " . $cu .
"...\n";
43 $this->query(
"drop index " . $cu . $tablename . $indexname);
49 print
"Adding ilike index for " . $cu .
"...\n";
50 $this->query(
"create unique index " . $cu . $tablename . $indexname .
" on " . $cu . $tablename .
" ((lower(user_name)))");
61 public function down() {
62 print
"ilike index up\n";
65 $indexname=
"_username_1";
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";
74 $rows= $this->fetchAll($query);
76 print
"tablename: $tablename";
78 for ( $i = 0, $count= count($rows); $i < $count; $i++ )
82 $removeIndex= isset($row[
"index_name"]) && $row[
"islower"];
83 $addIndex= !isset($row[
"index_name"]) || $row[
"islower"];
85 $cu= strtolower(trim($row[
"cu"]));
89 print
"Removing ilike index for " . $cu .
"...\n";
90 $this->query(
"drop index " . $cu . $tablename . $indexname);
96 print
"Adding previous username index for " . $cu .
"...\n";
97 $this->query(
"create unique index " . $cu . $tablename . $indexname .
" on " . $cu . $tablename .
" (user_name)");