diff --git a/AUTHORS b/AUTHORS index 8e1cff9b0..3a90dc588 100644 --- a/AUTHORS +++ b/AUTHORS @@ -42,6 +42,7 @@ The following people have contributed to the SQLFairy project: - Moritz Onken - Paul Harrington - Peter Rabbitson +- Rafael Porres Molina - Robert Bohne - Ross Smith II - Ryan D Johnson diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 0f55fd992..5a60d51f5 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 ef7fee72e..00272ca6b 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 21e8ad355..b7b6e3255 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 5d56adf08..2129c298c 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;