From: Sebastian Podjasek Date: Fri, 26 Jun 2015 13:43:07 +0000 (+0200) Subject: Add support for USING and WHERE on indexes in PostgreSQL parser X-Git-Tag: v0.11022~32 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Translator.git;a=commitdiff_plain;h=9c05d806e51cf7e81870cf0d022593fe212a03e4 Add support for USING and WHERE on indexes in PostgreSQL parser RT#63814, GH#52 --- diff --git a/Changes b/Changes index 2fcabb2..deb821d 100644 --- a/Changes +++ b/Changes @@ -5,7 +5,7 @@ Changes for SQL::Translator * 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) + and parser (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/Parser/PostgreSQL.pm b/lib/SQL/Translator/Parser/PostgreSQL.pm index dc05928..504d365 100644 --- a/lib/SQL/Translator/Parser/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/PostgreSQL.pm @@ -247,7 +247,8 @@ create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method( supertype => $item{'unique'}[0] ? 'constraint' : 'index', type => $item{'unique'}[0] ? 'unique' : 'normal', fields => $item[9], - method => $item{'using_method'}[0], + method => $item{'using_method(?)'}[0], + where => $item{'where_predicate(?)'}[0], } ; } @@ -1080,10 +1081,14 @@ sub parse { } for my $idata ( @{ $tdata->{'indices'} || [] } ) { + my @options = (); + push @options, { using => $idata->{'method'} } if $idata->{method}; + push @options, { where => $idata->{'where'} } if $idata->{where}; my $index = $table->add_index( - name => $idata->{'name'}, - type => uc $idata->{'type'}, - fields => $idata->{'fields'}, + name => $idata->{'name'}, + type => uc $idata->{'type'}, + fields => $idata->{'fields'}, + options => \@options ) or die $table->error . ' ' . $table->name; } diff --git a/t/14postgres-parser.t b/t/14postgres-parser.t index 5548258..c11d616 100644 --- a/t/14postgres-parser.t +++ b/t/14postgres-parser.t @@ -71,6 +71,10 @@ my $sql = q{ FOR EACH ROW EXECUTE PROCEDURE foo(); + CREATE INDEX test_index1 ON t_test1 (f_varchar); + CREATE INDEX test_index2 ON t_test1 USING hash (f_char, f_bool); + CREATE INDEX test_index3 ON t_test1 USING hash (f_bigint, f_tz) WHERE f_bigint = '1' AND f_tz IS NULL; + alter table t_test1 add f_fk2 integer; alter table only t_test1 add constraint c_u1 unique (f_varchar); @@ -380,4 +384,27 @@ is( $trigger->perform_action_when, 'before', "Correct time for trigger"); is( $trigger->scope, 'row', "Correct scope for trigger"); is( $trigger->action, 'EXECUTE PROCEDURE foo()', "Correct action for trigger"); +# test index +my @indices = $t1->get_indices; +is(scalar @indices, 3, 'got three indexes'); + +my $t1_i1 = $indices[0]; +is( $t1_i1->name, 'test_index1', 'First index is "test_index1"' ); +is( join(',', $t1_i1->fields), 'f_varchar', 'Index is on field "f_varchar"' ); +is_deeply( [ $t1_i1->options ], [], 'Index is has no options' ); + +my $t1_i2 = $indices[1]; +is( $t1_i2->name, 'test_index2', 'Second index is "test_index2"' ); +is( join(',', $t1_i2->fields), 'f_char,f_bool', 'Index is on fields "f_char, f_bool"' ); +is_deeply( [ $t1_i2->options ], [ { using => 'hash' } ], 'Index is using hash method' ); + +my $t1_i3 = $indices[2]; +is( $t1_i3->name, 'test_index3', 'Third index is "test_index3"' ); +is( join(',', $t1_i3->fields), 'f_bigint,f_tz', 'Index is on fields "f_bigint, f_tz"' ); +is_deeply( + [ $t1_i3->options ], + [ { using => 'hash' }, { where => "f_bigint = '1' AND f_tz IS NULL" } ], + 'Index is using hash method and has predicate right' +); + done_testing;