From: Andrew Gregory Date: Sun, 7 Jun 2015 06:30:30 +0000 (-0400) Subject: Add SQLite support for check constraints X-Git-Tag: v0.11022~39 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e773f3ff8f00ea020f5ac4cbd4360b7f0398bf7c;p=dbsrgits%2FSQL-Translator.git Add SQLite support for check constraints --- diff --git a/Changes b/Changes index 6b576b3..50dd5f3 100644 --- a/Changes +++ b/Changes @@ -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) diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index 9a2c492..9cc92af 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -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); } @@ -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; diff --git a/t/56-sqlite-producer.t b/t/56-sqlite-producer.t index 3e962e5..d0d2cfe 100644 --- a/t/56-sqlite-producer.t +++ b/t/56-sqlite-producer.t @@ -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;