@@ -2,7 +2,9 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
22 use ExUnit.Case , async: true
33
44 import Ecto.Adapters.SQLite3.TestHelpers
5- import Ecto.Migration , only: [ table: 1 , table: 2 , index: 2 , index: 3 , constraint: 3 ]
5+
6+ import Ecto.Migration ,
7+ only: [ table: 1 , table: 2 , index: 2 , index: 3 , constraint: 2 , constraint: 3 ]
68
79 alias Ecto.Migration.Reference
810
@@ -512,21 +514,95 @@ defmodule Ecto.Adapters.SQLite3.Connection.MigrationTest do
512514 end
513515 end
514516
515- test "drop constraint" do
517+ test "create check constraint" do
518+ create =
519+ { :create , constraint ( :products , "price_must_be_positive" , check: "price > 0" ) }
520+
521+ assert execute_ddl ( create ) ==
522+ [
523+ ~s| ALTER TABLE "products" ADD CONSTRAINT "price_must_be_positive" CHECK (price > 0)|
524+ ]
525+
526+ create =
527+ { :create ,
528+ constraint ( :products , "price_must_be_positive" ,
529+ check: "price > 0" ,
530+ prefix: "foo"
531+ ) }
532+
533+ assert execute_ddl ( create ) ==
534+ [
535+ ~s| ALTER TABLE "foo"."products" ADD CONSTRAINT "price_must_be_positive" CHECK (price > 0)|
536+ ]
537+ end
538+
539+ test "create exclusion constraint" do
540+ assert_raise ArgumentError , "SQLite3 does not support exclusion constraints" , fn ->
541+ execute_ddl (
542+ { :create ,
543+ constraint ( :products , "price_must_be_positive" ,
544+ exclude: ~s| gist (int4range("from", "to", '[]') WITH &&)|
545+ ) }
546+ )
547+ end
548+ end
549+
550+ test "create constraint with comment" do
551+ create =
552+ { :create ,
553+ constraint ( :products , "price_must_be_positive" ,
554+ check: "price > 0" ,
555+ prefix: "foo" ,
556+ comment: "comment"
557+ ) }
558+
559+ assert execute_ddl ( create ) == [
560+ ~s| ALTER TABLE "foo"."products" ADD CONSTRAINT "price_must_be_positive" CHECK (price > 0)|
561+ ]
562+
563+ # NOTE: Comments are not supported by SQLite. DDL query generator will ignore them.
564+ end
565+
566+ test "create invalid constraint" do
516567 assert_raise ArgumentError ,
517- ~r / S Q L i t e 3 d o e s n o t s u p p o r t A L T E R T A B L E D R O P C O N S T R A I N T . / ,
568+ " SQLite3 does not support `validate: false` in constraints" ,
518569 fn ->
519570 execute_ddl (
520- { :drop ,
521- constraint ( :products , "price_must_be_positive" , prefix: :foo ) ,
522- :restrict }
571+ { :create ,
572+ constraint ( :products , "price_must_be_positive" ,
573+ check: "price > 0" ,
574+ prefix: "foo" ,
575+ validate: false
576+ ) }
523577 )
524578 end
525579 end
526580
581+ test "drop constraint" do
582+ drop =
583+ { :drop , constraint ( :products , "price_must_be_positive" ) , :restrict }
584+
585+ assert execute_ddl ( drop ) == [
586+ ~s| ALTER TABLE "products" DROP CONSTRAINT "price_must_be_positive"|
587+ ]
588+
589+ drop = { :drop , constraint ( :products , "price_must_be_positive" ) , :cascade }
590+
591+ assert_raise ArgumentError ,
592+ "SQLite3 does not support `CASCADE` in `DROP CONSTRAINT` commands" ,
593+ fn -> execute_ddl ( drop ) end
594+
595+ drop =
596+ { :drop , constraint ( :products , "price_must_be_positive" , prefix: :foo ) , :restrict }
597+
598+ assert execute_ddl ( drop ) == [
599+ ~s| ALTER TABLE "foo"."products" DROP CONSTRAINT "price_must_be_positive"|
600+ ]
601+ end
602+
527603 test "drop_if_exists constraint" do
528604 assert_raise ArgumentError ,
529- ~r / S Q L i t e 3 d o e s n o t s u p p o r t A L T E R T A B L E D R O P C O N S T R A I N T . / ,
605+ " SQLite3 does not support `drop_if_exists` for constraints" ,
530606 fn ->
531607 execute_ddl (
532608 { :drop_if_exists ,
0 commit comments