From: Andrew Gregory Date: Mon, 3 Nov 2014 18:19:42 +0000 (-0500) Subject: Provide default index names for SQLite X-Git-Tag: v0.11021~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FSQL-Translator.git;a=commitdiff_plain;h=2491a467cb7f1991af6aa89d09e7bfd974583242 Provide default index names for SQLite 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. --- diff --git a/AUTHORS b/AUTHORS index 859f092..f01fa19 100644 --- a/AUTHORS +++ b/AUTHORS @@ -9,6 +9,7 @@ The following people have contributed to the SQLFairy project: - André Walker - Andreas 'ac0v' Specht - Andrew Moore +- Andrew Gregory - Andrew Pam - Arthur Axel "fREW" Schmidt - Ben Faga diff --git a/Changes b/Changes index df77e5f..92e778e 100644 --- a/Changes +++ b/Changes @@ -12,6 +12,7 @@ Changes for SQL::Translator * Fix erroneous PostgreSQL floating point type translations (RT#99725) * Remove executable bit from Parser/JSON.pm (RT#100532) * Update the Free Software Foundation's address (RT#100531) + * Provide default index names for SQLite (GH#45) 0.11020 2014-09-02 diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index aefd038..1c04edd 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -225,7 +225,6 @@ sub create_table # # Indices # - my $idx_name_default = 'A'; for my $index ( $table->get_indices ) { push @index_defs, create_index($index); } @@ -233,7 +232,6 @@ sub create_table # # Constraints # - my $c_name_default = 'A'; for my $c ( $table->get_constraints ) { if ($c->type eq "FOREIGN KEY") { push @field_defs, create_foreignkey($c); @@ -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 = @@ -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; diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 5d56adf..66bb8bb 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -164,4 +164,32 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; is_deeply($result, $expected, 'correctly unquoted excempted DEFAULTs'); } +{ + my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => ['foo'] ); + + { + my $index = $table->add_index(name => 'myindex', fields => ['foo']); + my ($def) = SQL::Translator::Producer::SQLite::create_index($index); + is($def, 'CREATE INDEX "myindex" ON "foobar" ("foo")', 'index created'); + } + + { + my $index = $table->add_index(fields => ['foo']); + my ($def) = SQL::Translator::Producer::SQLite::create_index($index); + is($def, 'CREATE INDEX "foobar_idx" ON "foobar" ("foo")', 'index created'); + } + + { + my $constr = $table->add_constraint(name => 'constr', fields => ['foo']); + my ($def) = SQL::Translator::Producer::SQLite::create_constraint($constr); + is($def, 'CREATE UNIQUE INDEX "constr" ON "foobar" ("foo")', 'constraint created'); + } + + { + my $constr = $table->add_constraint(fields => ['foo']); + my ($def) = SQL::Translator::Producer::SQLite::create_constraint($constr); + is($def, 'CREATE UNIQUE INDEX "foobar_idx02" ON "foobar" ("foo")', 'constraint created'); + } +} + done_testing;