From: Ivan Baidakou Date: Mon, 8 Dec 2014 05:49:06 +0000 (+0300) Subject: Add support for optional monotonically increasing sqlite autoincrement X-Git-Tag: v0.11022~48 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7f3f64d7569376436130a955eb3c9e6b30284c34;p=dbsrgits%2FSQL-Translator.git Add support for optional monotonically increasing sqlite autoincrement Enabled with $field->extra->{auto_increment_type} = 'monotonic' --- diff --git a/AUTHORS b/AUTHORS index f01fa19..e05e379 100644 --- a/AUTHORS +++ b/AUTHORS @@ -27,6 +27,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/Changes b/Changes index 6c9c88e..f2ecbad 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Changes for SQL::Translator + * Add support for monotonically increasing SQLite autoincs (GH#47) + 0.11021 2015-01-29 * Fix Oracle producer generating an unnecessary / at the end in case there diff --git a/lib/SQL/Translator/Generator/DDL/SQLite.pm b/lib/SQL/Translator/Generator/DDL/SQLite.pm index 0f55fd9..621ff8a 100644 --- a/lib/SQL/Translator/Generator/DDL/SQLite.pm +++ b/lib/SQL/Translator/Generator/DDL/SQLite.pm @@ -75,6 +75,22 @@ sub _ipk { ( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) ) } +sub field_autoinc { + my ($self, $field) = @_; + + return ( + ( + ($field->extra->{auto_increment_type}||'') eq 'monotonic' + and + $self->_ipk($field) + and + $field->is_auto_increment + ) + ? 'AUTOINCREMENT' + : '' + ); +} + sub field { my ($self, $field) = @_; @@ -86,6 +102,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 66bb8bb..4b79a60 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -165,6 +165,25 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0; } { + 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 => { + auto_increment_type => 'monotonic', + }, + ); + $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'); +} + +{ my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => ['foo'] ); {