Skip to content

Commit

Permalink
Provide default index names for SQLite
Browse files Browse the repository at this point in the history
The allegedly default names were never used causing the first index
created without a name to fail and subsequent indices to be named 02,
03, 04...  This uses the table name with '_idx' appended as the default,
similar to the PostgreSQL producer.
  • Loading branch information
andrewgregory committed Nov 4, 2014
1 parent 90089d6 commit e3201f7
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/SQL/Translator/Producer/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,13 @@ sub create_table
#
# Indices
#
my $idx_name_default = 'A';
for my $index ( $table->get_indices ) {
push @index_defs, create_index($index);
}

#
# Constraints
#
my $c_name_default = 'A';
for my $c ( $table->get_constraints ) {
if ($c->type eq "FOREIGN KEY") {
push @field_defs, create_foreignkey($c);
Expand Down Expand Up @@ -283,14 +281,13 @@ sub create_index
{
my ($index, $options) = @_;

my $name = $index->name;
$name = mk_name($name);
(my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
my $name = mk_name($index->name || "${index_table_name}_idx");

my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : '';

# strip any field size qualifiers as SQLite doesn't like these
my @fields = map { s/\(\d+\)$//; _generator()->quote($_) } $index->fields;
(my $index_table_name = $index->table->name) =~ s/^.+?\.//; # table name may not specify schema
$index_table_name = _generator()->quote($index_table_name);
warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
my $index_def =
Expand All @@ -304,10 +301,9 @@ sub create_constraint
{
my ($c, $options) = @_;

my $name = $c->name;
$name = mk_name($name);
my @fields = map _generator()->quote($_), $c->fields;
(my $index_table_name = $c->table->name) =~ s/^.+?\.//; # table name may not specify schema
my $name = mk_name($c->name || "${index_table_name}_idx");
my @fields = map _generator()->quote($_), $c->fields;
$index_table_name = _generator()->quote($index_table_name);
warn "removing schema name from '" . $c->table->name . "' to make '$index_table_name'\n" if $WARN;

Expand Down

0 comments on commit e3201f7

Please sign in to comment.