Skip to content

Commit

Permalink
Add SQLite support for check constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgregory authored and ribasushi committed Jun 24, 2015
1 parent 0e9badb commit e773f3f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changes for SQL::Translator

* Add support for monotonically increasing SQLite autoincs (GH#47)
* Add support for CHECK constraint in SQLite producer (GH#57)
* Fix forgotten quoting in the MySQL DROP TABLE diff producer (GH#50)
* Improve add_trigger consistency between producers (GH#48)
* Declare dependencies in deterministic order (RT#102859)
Expand Down
11 changes: 11 additions & 0 deletions lib/SQL/Translator/Producer/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ sub create_table
if ($c->type eq "FOREIGN KEY") {
push @field_defs, create_foreignkey($c);
}
elsif ($c->type eq "CHECK") {
push @field_defs, create_check_constraint($c);
}
next unless $c->type eq UNIQUE;
push @constraint_defs, create_constraint($c);
}
Expand All @@ -245,6 +248,14 @@ sub create_table
return (@create, $create_table, @index_defs, @constraint_defs );
}

sub create_check_constraint {
my $c = shift;
my $check = '';
$check .= 'CONSTRAINT ' . _generator->quote( $c->name ) . ' ' if $c->name;
$check .= 'CHECK(' . $c->expression . ')';
return $check;
}

sub create_foreignkey {
my $c = shift;

Expand Down
8 changes: 8 additions & 0 deletions t/56-sqlite-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,12 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
}
}

{
my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => ['foo'] );
my $constr = $table->add_constraint(name => 'constr', expression => "foo != 'baz'");
my ($def) = SQL::Translator::Producer::SQLite::create_check_constraint($constr);

is($def, q{CONSTRAINT "constr" CHECK(foo != 'baz')}, 'check constraint created');
}

done_testing;

0 comments on commit e773f3f

Please sign in to comment.