Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward-compatible sqlite autoincrement #47

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -27,6 +27,7 @@ The following people have contributed to the SQLFairy project:
- Geoff Cant <geoff@catalyst.net.nz>
- Gudmundur A. Thorisson <mummi@cshl.org>
- Guillermo Roditi <groditi@cpan.org>
- Ivan Baidakou (basiliscos) <dmol@cpan.org>
- Jaime Soriano Pastor <jsoriano@tuenti.com>
- Jason Williams <smdwilliams@users.sourceforge.net>
- Johan Viklund <viklund@cpan.org>
Expand Down
15 changes: 14 additions & 1 deletion lib/SQL/Translator/Generator/DDL/SQLite.pm
Expand Up @@ -75,17 +75,30 @@ sub _ipk {
( $field->data_type =~ /^number?$/i && $field->size !~ /,/ ) )
}

sub field_autoinc {
my ($self, $field) = @_;
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
&& $self->_ipk($field)
&& defined($auto_increment_method)
&& $auto_increment_method eq 'sequence'
;
return ( $force_autoinc ? 'AUTOINCREMENT' : () )
}

sub field {
my ($self, $field) = @_;


return join ' ',
$self->field_comments($field),
$self->field_name($field),
( $self->_ipk($field)
? ( 'INTEGER PRIMARY KEY' )
: ( $self->field_type($field) )
),
$self->field_autoinc($field),
$self->field_nullable($field),
$self->field_default($field, {
NULL => 1,
Expand Down
19 changes: 19 additions & 0 deletions t/56-sqlite-producer.t
Expand Up @@ -164,6 +164,25 @@ $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 => {
auto_increment_method => 'sequence',
},
);
$table->primary_key('id');
my $expected = [ qq<CREATE TABLE "some_table" (\n "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n)>];
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'] );

Expand Down