From 4839d79497d6128cf7621358d2de96ff1af4e25a Mon Sep 17 00:00:00 2001 From: Ivan Baidakou Date: Mon, 8 Dec 2014 08:49:06 +0300 Subject: [PATCH 1/3] Backward-compatible sqlite autoincrement --- AUTHORS | 1 + lib/SQL/Translator/Generator/DDL/SQLite.pm | 14 +++++++++++++- t/56-sqlite-producer.t | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index a7cc17dc6..43868dc49 100644 --- a/AUTHORS +++ b/AUTHORS @@ -26,6 +26,7 @@ The following people have contributed to the SQLFairy project: - Geoff Cant - Gudmundur A. Thorisson - Guillermo Roditi +- Ivan Baidakou (basiliscos) - Jaime Soriano Pastor - Jason Williams - Johan Viklund diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 0f55fd992..875ca7e9e 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -75,10 +75,21 @@ sub _ipk { ( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) ) } +sub field_autoinc { + my ($self, $field) = @_; + my $autoinc_method = $field->extra->{autoinc_method}; + # use 'old' backward-compatible behaviour, i.e. use + # monotonic autoincrement only if it is specified in extra + my $force_autoinc = $field->is_auto_increment + && defined($autoinc_method) + && $autoinc_method eq 'sequence' + ; + return ( $force_autoinc ? 'AUTOINCREMENT' : () ) +} + sub field { my ($self, $field) = @_; - return join ' ', $self->field_comments($field), $self->field_name($field), @@ -86,6 +97,7 @@ sub field { ? ( 'INTEGER PRIMARY KEY' ) : ( $self->field_type($field) ) ), + $self->field_autoinc($field), $self->field_nullable($field), $self->field_default($field, { NULL => 1, diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 5d56adf08..e58a9e270 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -164,4 +164,23 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; is_deeply($result, $expected, 'correctly unquoted excempted DEFAULTs'); } +{ + my $table = SQL::Translator::Schema::Table->new( + name => 'some_table', + ); + $table->add_field( + name => 'id', + data_type => 'integer', + is_auto_increment => 1, + is_nullable => 0, + extra => { + autoinc_method => 'sequence', + }, + ); + my $expected = [ qq]; + my $result = [SQL::Translator::Producer::SQLite::create_table($table, { no_comments => 1 })]; + is_deeply($result, $expected, 'correctly built monotonicly autoincremened PK'); + +} + done_testing; From 46c40ccc9bb6ed8e7605f29ebe294727b61e7cbb Mon Sep 17 00:00:00 2001 From: Ivan Baidakou Date: Wed, 7 Jan 2015 14:06:59 +0300 Subject: [PATCH 2/3] Rename autoinc_method -> auto_increment_method --- lib/SQL/Translator/Generator/DDL/SQLite.pm | 6 +++--- t/56-sqlite-producer.t | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 875ca7e9e..77572876a 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -77,12 +77,12 @@ sub _ipk { sub field_autoinc { my ($self, $field) = @_; - my $autoinc_method = $field->extra->{autoinc_method}; + my $auto_increment_method = $field->extra->{auto_increment_method}; # use 'old' backward-compatible behaviour, i.e. use # monotonic autoincrement only if it is specified in extra my $force_autoinc = $field->is_auto_increment - && defined($autoinc_method) - && $autoinc_method eq 'sequence' + && defined($auto_increment_method) + && $auto_increment_method eq 'sequence' ; return ( $force_autoinc ? 'AUTOINCREMENT' : () ) } diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index e58a9e270..2f62eb2f4 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -174,7 +174,7 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; is_auto_increment => 1, is_nullable => 0, extra => { - autoinc_method => 'sequence', + auto_increment_method => 'sequence', }, ); my $expected = [ qq]; From ee779a21f6445be7c4c03d6afe806c34653b5183 Mon Sep 17 00:00:00 2001 From: Ivan Baidakou Date: Sun, 1 Feb 2015 15:06:02 +0300 Subject: [PATCH 3/3] Add "AUTOINCREMENT" only after INTEGER PRIMARY KEY for sqlite ddl generator --- lib/SQL/Translator/Generator/DDL/SQLite.pm | 1 + t/56-sqlite-producer.t | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 77572876a..4c34b23db 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -81,6 +81,7 @@ sub field_autoinc { # use 'old' backward-compatible behaviour, i.e. use # monotonic autoincrement only if it is specified in extra my $force_autoinc = $field->is_auto_increment + && $self->_ipk($field) && defined($auto_increment_method) && $auto_increment_method eq 'sequence' ; diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index cde77a240..08bc6a45c 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -177,7 +177,8 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; auto_increment_method => 'sequence', }, ); - my $expected = [ qq]; + $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 monotonicly autoincremened PK'); }