@@ -573,7 +573,10 @@ mod tests {
573573 use super :: * ;
574574 use insta:: { assert_snapshot, with_settings} ;
575575 use rstest:: rstest;
576- use vespertide_core:: IndexDef ;
576+ use vespertide_core:: schema:: primary_key:: PrimaryKeySyntax ;
577+ use vespertide_core:: {
578+ ColumnDef , ColumnType , ComplexColumnType , IndexDef , SimpleColumnType , StrOrBoolOrArray ,
579+ } ;
577580
578581 fn col ( name : & str , ty : ColumnType ) -> ColumnDef {
579582 ColumnDef {
@@ -673,6 +676,33 @@ mod tests {
673676 assert_eq ! ( reference_action_sql( & action) , expected) ;
674677 }
675678
679+ #[ test]
680+ #[ should_panic]
681+ fn test_sqlite_create_foreign_key_build_panics ( ) {
682+ let fk = ForeignKey :: create ( )
683+ . name ( "fk" )
684+ . from_tbl ( Alias :: new ( "a" ) )
685+ . from_col ( Alias :: new ( "c" ) )
686+ . to_tbl ( Alias :: new ( "b" ) )
687+ . to_col ( Alias :: new ( "id" ) )
688+ . to_owned ( ) ;
689+ let q = BuiltQuery :: CreateForeignKey ( Box :: new ( fk) ) ;
690+ // sea-query panics when building FK SQL for SQLite; exercise that branch
691+ let _ = q. build ( DatabaseBackend :: Sqlite ) ;
692+ }
693+
694+ #[ test]
695+ #[ should_panic]
696+ fn test_sqlite_drop_foreign_key_build_panics ( ) {
697+ let fk = ForeignKey :: drop ( )
698+ . name ( "fk" )
699+ . table ( Alias :: new ( "a" ) )
700+ . to_owned ( ) ;
701+ let q = BuiltQuery :: DropForeignKey ( Box :: new ( fk) ) ;
702+ // sea-query panics when building FK SQL for SQLite; exercise that branch
703+ let _ = q. build ( DatabaseBackend :: Sqlite ) ;
704+ }
705+
676706 #[ test]
677707 fn test_backend_specific_quoting ( ) {
678708 let action = MigrationAction :: CreateTable {
@@ -729,6 +759,144 @@ mod tests {
729759 DatabaseBackend :: Sqlite ,
730760 & [ "CREATE TABLE \" users\" ( \" id\" integer )" ]
731761 ) ]
762+ #[ case:: create_table_with_inline_constraints_postgres(
763+ "create_table_with_inline_constraints_postgres" ,
764+ MigrationAction :: CreateTable {
765+ table: "users" . into( ) ,
766+ columns: vec![
767+ ColumnDef {
768+ name: "id" . into( ) ,
769+ r#type: ColumnType :: Simple ( SimpleColumnType :: Integer ) ,
770+ nullable: false ,
771+ default : None ,
772+ comment: None ,
773+ primary_key: Some ( PrimaryKeySyntax :: Bool ( true ) ) ,
774+ unique: None ,
775+ index: None ,
776+ foreign_key: None ,
777+ } ,
778+ ColumnDef {
779+ name: "email" . into( ) ,
780+ r#type: ColumnType :: Simple ( SimpleColumnType :: Text ) ,
781+ nullable: true ,
782+ default : None ,
783+ comment: None ,
784+ primary_key: None ,
785+ unique: Some ( StrOrBoolOrArray :: Bool ( true ) ) ,
786+ index: None ,
787+ foreign_key: None ,
788+ } ,
789+ ] ,
790+ constraints: vec![
791+ TableConstraint :: PrimaryKey {
792+ auto_increment: false ,
793+ columns: vec![ "id" . into( ) ] ,
794+ } ,
795+ TableConstraint :: Unique {
796+ name: Some ( "uq_email" . into( ) ) ,
797+ columns: vec![ "email" . into( ) ] ,
798+ } ,
799+ TableConstraint :: Check {
800+ name: "chk_always_true" . into( ) ,
801+ expr: "1 = 1" . into( ) ,
802+ } ,
803+ ] ,
804+ } ,
805+ DatabaseBackend :: Postgres ,
806+ & [ "PRIMARY KEY" , "UNIQUE" ]
807+ ) ]
808+ #[ case:: create_table_with_inline_constraints_mysql(
809+ "create_table_with_inline_constraints_mysql" ,
810+ MigrationAction :: CreateTable {
811+ table: "users" . into( ) ,
812+ columns: vec![
813+ ColumnDef {
814+ name: "id" . into( ) ,
815+ r#type: ColumnType :: Simple ( SimpleColumnType :: Integer ) ,
816+ nullable: false ,
817+ default : None ,
818+ comment: None ,
819+ primary_key: Some ( PrimaryKeySyntax :: Bool ( true ) ) ,
820+ unique: None ,
821+ index: None ,
822+ foreign_key: None ,
823+ } ,
824+ ColumnDef {
825+ name: "email" . into( ) ,
826+ r#type: ColumnType :: Simple ( SimpleColumnType :: Text ) ,
827+ nullable: true ,
828+ default : None ,
829+ comment: None ,
830+ primary_key: None ,
831+ unique: Some ( StrOrBoolOrArray :: Bool ( true ) ) ,
832+ index: None ,
833+ foreign_key: None ,
834+ } ,
835+ ] ,
836+ constraints: vec![
837+ TableConstraint :: PrimaryKey {
838+ auto_increment: false ,
839+ columns: vec![ "id" . into( ) ] ,
840+ } ,
841+ TableConstraint :: Unique {
842+ name: Some ( "uq_email" . into( ) ) ,
843+ columns: vec![ "email" . into( ) ] ,
844+ } ,
845+ TableConstraint :: Check {
846+ name: "chk_always_true" . into( ) ,
847+ expr: "1 = 1" . into( ) ,
848+ } ,
849+ ] ,
850+ } ,
851+ DatabaseBackend :: Postgres ,
852+ & [ "PRIMARY KEY" , "UNIQUE" ]
853+ ) ]
854+ #[ case:: create_table_with_inline_constraints_sqlite(
855+ "create_table_with_inline_constraints_sqlite" ,
856+ MigrationAction :: CreateTable {
857+ table: "users" . into( ) ,
858+ columns: vec![
859+ ColumnDef {
860+ name: "id" . into( ) ,
861+ r#type: ColumnType :: Simple ( SimpleColumnType :: Integer ) ,
862+ nullable: false ,
863+ default : None ,
864+ comment: None ,
865+ primary_key: Some ( PrimaryKeySyntax :: Bool ( true ) ) ,
866+ unique: None ,
867+ index: None ,
868+ foreign_key: None ,
869+ } ,
870+ ColumnDef {
871+ name: "email" . into( ) ,
872+ r#type: ColumnType :: Simple ( SimpleColumnType :: Text ) ,
873+ nullable: true ,
874+ default : None ,
875+ comment: None ,
876+ primary_key: None ,
877+ unique: Some ( StrOrBoolOrArray :: Bool ( true ) ) ,
878+ index: None ,
879+ foreign_key: None ,
880+ } ,
881+ ] ,
882+ constraints: vec![
883+ TableConstraint :: PrimaryKey {
884+ auto_increment: false ,
885+ columns: vec![ "id" . into( ) ] ,
886+ } ,
887+ TableConstraint :: Unique {
888+ name: Some ( "uq_email" . into( ) ) ,
889+ columns: vec![ "email" . into( ) ] ,
890+ } ,
891+ TableConstraint :: Check {
892+ name: "chk_always_true" . into( ) ,
893+ expr: "1 = 1" . into( ) ,
894+ } ,
895+ ] ,
896+ } ,
897+ DatabaseBackend :: Postgres ,
898+ & [ "PRIMARY KEY" , "UNIQUE" ]
899+ ) ]
732900 #[ case:: create_table_with_fk_postgres(
733901 "create_table_with_fk_postgres" ,
734902 MigrationAction :: CreateTable {
@@ -1260,6 +1428,54 @@ mod tests {
12601428 DatabaseBackend :: MySql ,
12611429 & [ "FOREIGN KEY (\" user_id\" )" , "REFERENCES \" users\" (\" id\" )" , "ON DELETE CASCADE" , "ON UPDATE RESTRICT" ]
12621430 ) ]
1431+ #[ case:: add_constraint_foreign_key_unnamed_postgres(
1432+ "add_constraint_foreign_key_unnamed_postgres" ,
1433+ MigrationAction :: AddConstraint {
1434+ table: "posts" . into( ) ,
1435+ constraint: TableConstraint :: ForeignKey {
1436+ name: None ,
1437+ columns: vec![ "user_id" . into( ) ] ,
1438+ ref_table: "users" . into( ) ,
1439+ ref_columns: vec![ "id" . into( ) ] ,
1440+ on_delete: None ,
1441+ on_update: None ,
1442+ } ,
1443+ } ,
1444+ DatabaseBackend :: Postgres ,
1445+ & [ "ADD FOREIGN KEY (\" user_id\" ) REFERENCES \" users\" (\" id\" )" ]
1446+ ) ]
1447+ #[ case:: add_constraint_foreign_key_unnamed_mysql(
1448+ "add_constraint_foreign_key_unnamed_mysql" ,
1449+ MigrationAction :: AddConstraint {
1450+ table: "posts" . into( ) ,
1451+ constraint: TableConstraint :: ForeignKey {
1452+ name: None ,
1453+ columns: vec![ "user_id" . into( ) ] ,
1454+ ref_table: "users" . into( ) ,
1455+ ref_columns: vec![ "id" . into( ) ] ,
1456+ on_delete: None ,
1457+ on_update: None ,
1458+ } ,
1459+ } ,
1460+ DatabaseBackend :: MySql ,
1461+ & [ "ADD FOREIGN KEY (\" user_id\" ) REFERENCES \" users\" (\" id\" )" ]
1462+ ) ]
1463+ #[ case:: add_constraint_foreign_key_unnamed_sqlite(
1464+ "add_constraint_foreign_key_unnamed_sqlite" ,
1465+ MigrationAction :: AddConstraint {
1466+ table: "posts" . into( ) ,
1467+ constraint: TableConstraint :: ForeignKey {
1468+ name: None ,
1469+ columns: vec![ "user_id" . into( ) ] ,
1470+ ref_table: "users" . into( ) ,
1471+ ref_columns: vec![ "id" . into( ) ] ,
1472+ on_delete: None ,
1473+ on_update: None ,
1474+ } ,
1475+ } ,
1476+ DatabaseBackend :: Sqlite ,
1477+ & [ "ADD FOREIGN KEY (\" user_id\" ) REFERENCES \" users\" (\" id\" )" ]
1478+ ) ]
12631479 #[ case:: add_constraint_foreign_key_sqlite(
12641480 "add_constraint_foreign_key_sqlite" ,
12651481 MigrationAction :: AddConstraint {
0 commit comments