Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Bug #63814: Allow for index creation with USING and WHERE keywords... #51

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/SQL/Translator/Producer/PostgreSQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( $key eq 'using' ) {
$index_using = "USING $value";
}
elsif ( $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 ) {
Expand All @@ -553,7 +570,10 @@ 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) .
($index_using ne '' ? ' ' . $index_using : '') .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you include the leading space when setting the value above, you can get rid of the ugly conditionals here.

' ' . $field_names .
($index_where ne '' ? ' ' . $index_where : '')
;
}
else {
Expand Down
8 changes: 8 additions & 0 deletions t/47postgres-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down