diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 98b0eab22..3f9397a48 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -235,6 +235,8 @@ sub create_table for my $c ( $table->get_constraints ) { 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); @@ -245,6 +247,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; diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 4b79a602c..3bf4c2741 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -211,4 +211,13 @@ $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;