From: Sebastian Podjasek Date: Wed, 8 Apr 2015 23:40:35 +0000 (+0200) Subject: Add support for USING and WHERE on indexes in PostgreSQL producer X-Git-Tag: v0.11022~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5b36314d8299489928d0d4bf44702ef8a5b93550;p=dbsrgits%2FSQL-Translator.git Add support for USING and WHERE on indexes in PostgreSQL producer RT#63814, GH#52 --- diff --git a/Changes b/Changes index 2ba7d65..2fcabb2 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,8 @@ Changes for SQL::Translator * Add support for CHECK constraint in SQLite producer (GH#57) * Fix forgotten quoting in the MySQL DROP TABLE diff producer (GH#50) * Fix Pg grammar parsing of UUID, time, timetz columns (RT#100196, GH#52) + * Add support for USING and WHERE on indexes in PostgreSQL producer + (RT#63814, GH#52) * Improve add_trigger consistency between producers (GH#48) * Declare dependencies in deterministic order (RT#102859) * Multiple speedups of naive internal debugging mechanism (GH#54) diff --git a/lib/SQL/Translator/Producer/PostgreSQL.pm b/lib/SQL/Translator/Producer/PostgreSQL.pm index fb2f9d5..29d152a 100644 --- a/lib/SQL/Translator/Producer/PostgreSQL.pm +++ b/lib/SQL/Translator/Producer/PostgreSQL.pm @@ -543,6 +543,23 @@ sub create_index my @fields = $index->fields; return unless @fields; + my $index_using; + my $index_where; + for my $opt ( $index->options ) { + if ( ref $opt eq 'HASH' ) { + foreach my $key (keys %$opt) { + my $value = $opt->{$key}; + next unless defined $value; + if ( uc($key) eq 'USING' ) { + $index_using = "USING $value"; + } + elsif ( uc($key) eq 'WHERE' ) { + $index_where = "WHERE $value"; + } + } + } + } + my $def_start = 'CONSTRAINT ' . $generator->quote($name) . ' '; my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')'; if ( $type eq PRIMARY_KEY ) { @@ -553,8 +570,8 @@ sub create_index } elsif ( $type eq NORMAL ) { $index_def = - 'CREATE INDEX ' . $generator->quote($name) . ' on ' . $generator->quote($table_name) . ' ' . $field_names - ; + 'CREATE INDEX ' . $generator->quote($name) . ' on ' . $generator->quote($table_name) . ' ' . + join ' ', grep { defined } $index_using, $field_names, $index_where; } else { warn "Unknown index type ($type) on table $table_name.\n" diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index 3e0ac90..bf57a01 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -627,6 +627,14 @@ is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote); is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes'); } + + { + my $index = $table->add_index(name => 'myindex', options => [{using => 'hash'}, {where => "upper(foo) = 'bar' AND bar = 'foo'"}], fields => ['bar', 'lower(foo)']); + my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index); + is($def, "CREATE INDEX myindex on foobar USING hash (bar, lower(foo)) WHERE upper(foo) = 'bar' AND bar = 'foo'", 'index using & where created'); + ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote); + is($def, 'CREATE INDEX "myindex" on "foobar" USING hash ("bar", lower(foo)) WHERE upper(foo) = \'bar\' AND bar = \'foo\'', 'index using & where created w/ quotes'); + } } my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };