From: rporres Date: Tue, 12 Feb 2013 09:05:56 +0000 (+0100) Subject: Fixed autoincrement in primary keys for SQLite X-Git-Tag: v0.011017~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=03b0fa258c8580135c282b0282b5c7dcb0865c14;p=dbsrgits%2FSQL-Translator.git Fixed autoincrement in primary keys for SQLite SQLite autoincrement feature behaves differently in primary keys and non-primary keys. SQLite generator must take into account autoincrement in primary keys when generating schemas. --- diff --git a/AUTHORS b/AUTHORS index 1096342..3e94545 100644 --- a/AUTHORS +++ b/AUTHORS @@ -45,6 +45,7 @@ The following people have contributed to the SQLFairy project: - Paul Harrington - Peter Rabbitson - Robert Bohne +- Rafael Porres Molina - Ross Smith II - Ryan D Johnson - Salvatore Bonaccorso diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 0f55fd9..5a60d51 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -78,12 +78,13 @@ sub _ipk { sub field { my ($self, $field) = @_; - return join ' ', $self->field_comments($field), $self->field_name($field), ( $self->_ipk($field) - ? ( 'INTEGER PRIMARY KEY' ) + ? $field->is_auto_increment + ? ( 'INTEGER PRIMARY KEY AUTOINCREMENT' ) + : ( 'INTEGER PRIMARY KEY' ) : ( $self->field_type($field) ) ), $self->field_nullable($field), diff --git a/t/30sqlt-new-diff-sqlite.t b/t/30sqlt-new-diff-sqlite.t index ef7fee7..00272ca 100644 --- a/t/30sqlt-new-diff-sqlite.t +++ b/t/30sqlt-new-diff-sqlite.t @@ -122,7 +122,7 @@ ALTER TABLE old_name RENAME TO new_name; ALTER TABLE new_name ADD COLUMN new_field int; CREATE TEMPORARY TABLE person_temp_alter ( - person_id INTEGER PRIMARY KEY NOT NULL, + person_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(20) NOT NULL, age int(11) DEFAULT 18, weight double(11,2), @@ -136,7 +136,7 @@ INSERT INTO person_temp_alter( person_id, name, age, weight, iq, is_rock_star, p DROP TABLE person; CREATE TABLE person ( - person_id INTEGER PRIMARY KEY NOT NULL, + person_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(20) NOT NULL, age int(11) DEFAULT 18, weight double(11,2), diff --git a/t/48xml-to-sqlite.t b/t/48xml-to-sqlite.t index 21e8ad3..b7b6e32 100644 --- a/t/48xml-to-sqlite.t +++ b/t/48xml-to-sqlite.t @@ -40,7 +40,7 @@ BEGIN TRANSACTION; DROP TABLE "Basic"; CREATE TABLE "Basic" ( - "id" INTEGER PRIMARY KEY NOT NULL, + "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100) NOT NULL DEFAULT 'hello', "description" text DEFAULT '', "email" varchar(500), @@ -62,7 +62,7 @@ CREATE UNIQUE INDEX "very_long_index_name_on_title_field_which_should_be_truncat DROP TABLE "Another"; CREATE TABLE "Another" ( - "id" INTEGER PRIMARY KEY NOT NULL, + "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "num" numeric(10,2) ); @@ -98,7 +98,7 @@ eq_or_diff(\@sql, 'BEGIN TRANSACTION', q, q, q, q, q, @@ -130,5 +130,3 @@ eq_or_diff(\@sql, 'COMMIT', ], 'SQLite translate in list context matches'); - - diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 5d56adf..2129c29 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -164,4 +164,40 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; is_deeply($result, $expected, 'correctly unquoted excempted DEFAULTs'); } +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'foo_auto_increment', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + is_nullable => 0, + is_auto_increment => 1, + ); + $table->primary_key('id'); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly built table with autoincrement on primary key'); +} + +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'foo_no_auto_increment', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + is_nullable => 0, + is_auto_increment => 0, + ); + $table->primary_key('id'); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly built table without autoincrement on primary key'); +} + done_testing;