@@ -999,6 +999,94 @@ mod tests {
999999 assert ! ( sql. contains( "CONSTRAINT \" chk_age\" CHECK" ) ) ;
10001000 }
10011001
1002+ #[ test]
1003+ fn test_add_constraint_primary_key_sqlite_without_existing_check ( ) {
1004+ // Test PrimaryKey addition when there are no existing CHECK constraints (line 84)
1005+ // This should hit the else branch: BuiltQuery::CreateTable(Box::new(create_temp_table))
1006+ let constraint = TableConstraint :: PrimaryKey {
1007+ columns : vec ! [ "id" . into( ) ] ,
1008+ auto_increment : false ,
1009+ } ;
1010+ let current_schema = vec ! [ TableDef {
1011+ name: "users" . into( ) ,
1012+ columns: vec![ ColumnDef {
1013+ name: "id" . into( ) ,
1014+ r#type: ColumnType :: Simple ( SimpleColumnType :: Integer ) ,
1015+ nullable: true ,
1016+ default : None ,
1017+ comment: None ,
1018+ primary_key: None ,
1019+ unique: None ,
1020+ index: None ,
1021+ foreign_key: None ,
1022+ } ] ,
1023+ constraints: vec![ ] , // No existing CHECK constraints
1024+ indexes: vec![ ] ,
1025+ } ] ;
1026+ let result = build_add_constraint (
1027+ & DatabaseBackend :: Sqlite ,
1028+ "users" ,
1029+ & constraint,
1030+ & current_schema,
1031+ ) ;
1032+ assert ! ( result. is_ok( ) ) ;
1033+ let queries = result. unwrap ( ) ;
1034+ let sql = queries
1035+ . iter ( )
1036+ . map ( |q| q. build ( DatabaseBackend :: Sqlite ) )
1037+ . collect :: < Vec < String > > ( )
1038+ . join ( "\n " ) ;
1039+ // Should create table without CHECK constraints (using BuiltQuery::CreateTable)
1040+ assert ! ( sql. contains( "CREATE TABLE" ) ) ;
1041+ assert ! ( sql. contains( "PRIMARY KEY" ) ) ;
1042+ }
1043+
1044+ #[ test]
1045+ fn test_add_constraint_foreign_key_sqlite_without_existing_check ( ) {
1046+ // Test ForeignKey addition when there are no existing CHECK constraints (line 238)
1047+ // This should hit the else branch: BuiltQuery::CreateTable(Box::new(create_temp_table))
1048+ let constraint = TableConstraint :: ForeignKey {
1049+ name : Some ( "fk_user" . into ( ) ) ,
1050+ columns : vec ! [ "user_id" . into( ) ] ,
1051+ ref_table : "users" . into ( ) ,
1052+ ref_columns : vec ! [ "id" . into( ) ] ,
1053+ on_delete : None ,
1054+ on_update : None ,
1055+ } ;
1056+ let current_schema = vec ! [ TableDef {
1057+ name: "posts" . into( ) ,
1058+ columns: vec![ ColumnDef {
1059+ name: "user_id" . into( ) ,
1060+ r#type: ColumnType :: Simple ( SimpleColumnType :: Integer ) ,
1061+ nullable: true ,
1062+ default : None ,
1063+ comment: None ,
1064+ primary_key: None ,
1065+ unique: None ,
1066+ index: None ,
1067+ foreign_key: None ,
1068+ } ] ,
1069+ constraints: vec![ ] , // No existing CHECK constraints
1070+ indexes: vec![ ] ,
1071+ } ] ;
1072+ let result = build_add_constraint (
1073+ & DatabaseBackend :: Sqlite ,
1074+ "posts" ,
1075+ & constraint,
1076+ & current_schema,
1077+ ) ;
1078+ assert ! ( result. is_ok( ) ) ;
1079+ let queries = result. unwrap ( ) ;
1080+ let sql = queries
1081+ . iter ( )
1082+ . map ( |q| q. build ( DatabaseBackend :: Sqlite ) )
1083+ . collect :: < Vec < String > > ( )
1084+ . join ( "\n " ) ;
1085+ // Should create table without CHECK constraints (using BuiltQuery::CreateTable)
1086+ assert ! ( sql. contains( "CREATE TABLE" ) ) ;
1087+ assert ! ( sql. contains( "FOREIGN KEY" ) ) ;
1088+ }
1089+
10021090 #[ test]
10031091 fn test_add_constraint_check_sqlite_with_indexes ( ) {
10041092 use vespertide_core:: IndexDef ;
0 commit comments