@@ -179,13 +179,16 @@ public function testColumnForwardsRenameColumnAndDropColumn(): void
179179
180180 public function testColumnForwardsIndexFamily (): void
181181 {
182+ // Each call must hit a fresh Column: index methods return Table, so
183+ // chaining a second method off a Column would route through Table.
182184 $ bp = new Table ();
183- $ bp ->string ('name ' )
184- ->index (['name ' ])
185- ->uniqueIndex (['name ' ], 'uq_name ' )
186- ->fulltextIndex (['name ' ], 'ft_name ' )
187- ->spatialIndex (['name ' ], 'sp_name ' )
188- ->addIndex ('custom ' , ['name ' ], IndexType::Unique);
185+ $ col = fn (): Column => $ bp ->string ('name ' );
186+
187+ $ col ()->index (['name ' ]);
188+ $ col ()->uniqueIndex (['name ' ], 'uq_name ' );
189+ $ col ()->fulltextIndex (['name ' ], 'ft_name ' );
190+ $ col ()->spatialIndex (['name ' ], 'sp_name ' );
191+ $ col ()->addIndex ('custom ' , ['name ' ], IndexType::Unique);
189192
190193 $ this ->assertCount (5 , $ bp ->indexes );
191194 $ this ->assertSame ('idx_name ' , $ bp ->indexes [0 ]->name );
@@ -219,7 +222,7 @@ public function testColumnForwardsAddAndDropForeignKey(): void
219222 {
220223 $ bp = new Table ();
221224 $ fk = $ bp ->string ('name ' )->addForeignKey ('parent_id ' );
222- $ bp ->dropForeignKey ('fk_old ' );
225+ $ bp ->string ( ' other ' )-> dropForeignKey ('fk_old ' );
223226
224227 $ this ->assertSame ('parent_id ' , $ fk ->column );
225228 $ this ->assertCount (1 , $ bp ->foreignKeys );
@@ -843,6 +846,16 @@ public function testColumnForwardsTruncateAndRenameTerminals(): void
843846 $ this ->assertSame ('RENAME TABLE `old` TO `new` ' , $ rename ->query );
844847 }
845848
849+ public function testColumnForwardsDropAndDropIfExistsTerminals (): void
850+ {
851+ $ schema = new MySQL ();
852+ $ drop = $ schema ->table ('users ' )->string ('name ' )->drop ();
853+ $ dropIfExists = $ schema ->table ('users ' )->string ('name ' )->dropIfExists ();
854+
855+ $ this ->assertSame ('DROP TABLE `users` ' , $ drop ->query );
856+ $ this ->assertSame ('DROP TABLE IF EXISTS `users` ' , $ dropIfExists ->query );
857+ }
858+
846859 public function testForeignKeyForwardsTruncateAndRenameTerminals (): void
847860 {
848861 $ schema = new MySQL ();
@@ -935,41 +948,42 @@ public function testDeepChainOfHeterogeneousMethodsCompiles(): void
935948
936949 public function testForeignKeyForwardsToEveryTableColumnFactory (): void
937950 {
951+ // Each call must hit a fresh ForeignKey: column factories return Column,
952+ // so chaining a second factory off a FK would route through Column instead.
938953 $ bp = new Table ();
939- $ bp ->id ()
940- -> integer ('user_id ' )
941- -> foreignKey ( ' user_id ' )-> references ( ' id ' )-> on ( ' users ' )
942- -> string ( ' s ' )
943- -> text ( ' t ' )
944- -> mediumText ( ' mt ' )
945- -> longText ( ' lt ' )
946- -> integer ( ' i ' )
947- -> bigInteger ( ' bi ' )
948- -> serial ( ' sr ' )
949- -> bigSerial ( ' bsr ' )
950- -> smallSerial ( ' ssr ' )
951- -> float ( ' f ' )
952- -> boolean ( ' b ' )
953- -> datetime ( ' dt ' )
954- -> timestamp ( ' ts ' )
955- -> json ( ' j ' )
956- -> binary ( ' bin ' )
957- -> enum ( ' status ' , [ ' draft ' , ' live ' ])
958- -> point ( ' p ' )
959- -> linestring ( ' ls ' )
960- -> polygon ( ' pg ' )
961- -> vector ( ' v ' , 8 )
962- -> id ( ' id_col ' );
954+ $ bp ->integer ( ' user_id ' );
955+ $ fk = fn (): ForeignKey => $ bp -> foreignKey ('user_id ' );
956+
957+ $ fk ()-> id ( ' id_col ' );
958+ $ fk ()-> string ( ' s ' );
959+ $ fk ()-> text ( ' t ' );
960+ $ fk ()-> mediumText ( ' mt ' );
961+ $ fk ()-> longText ( ' lt ' );
962+ $ fk ()-> integer ( ' i ' );
963+ $ fk ()-> bigInteger ( ' bi ' );
964+ $ fk ()-> serial ( ' sr ' );
965+ $ fk ()-> bigSerial ( ' bsr ' );
966+ $ fk ()-> smallSerial ( ' ssr ' );
967+ $ fk ()-> float ( ' f ' );
968+ $ fk ()-> boolean ( ' b ' );
969+ $ fk ()-> datetime ( ' dt ' );
970+ $ fk ()-> timestamp ( ' ts ' );
971+ $ fk ()-> json ( ' j ' );
972+ $ fk ()-> binary ( ' bin ' );
973+ $ enumCol = $ fk ()-> enum ( ' status ' , [ ' draft ' , ' live ' ]);
974+ $ fk ()-> point ( ' p ' );
975+ $ fk ()-> linestring ( ' ls ' );
976+ $ fk ()-> polygon ( ' pg ' );
977+ $ fk ()-> vector ( ' v ' , 8 );
963978
964979 $ names = \array_column ($ bp ->columns , 'name ' );
965980 $ this ->assertSame (
966- ['id ' , 'user_id ' , 's ' , 't ' , 'mt ' , 'lt ' , 'i ' , 'bi ' , 'sr ' , 'bsr ' , 'ssr ' , 'f ' , 'b ' , 'dt ' , 'ts ' , 'j ' , 'bin ' , 'status ' , 'p ' , 'ls ' , 'pg ' , 'v ' , ' id_col ' ],
981+ ['user_id ' , 'id_col ' , 's ' , 't ' , 'mt ' , 'lt ' , 'i ' , 'bi ' , 'sr ' , 'bsr ' , 'ssr ' , 'f ' , 'b ' , 'dt ' , 'ts ' , 'j ' , 'bin ' , 'status ' , 'p ' , 'ls ' , 'pg ' , 'v ' ],
967982 $ names ,
968983 );
969984
970- $ statusColumn = $ bp ->columns [17 ];
971- $ this ->assertSame (ColumnType::Enum, $ statusColumn ->type );
972- $ this ->assertSame (['draft ' , 'live ' ], $ statusColumn ->enumValues );
985+ $ this ->assertSame (ColumnType::Enum, $ enumCol ->type );
986+ $ this ->assertSame (['draft ' , 'live ' ], $ enumCol ->enumValues );
973987 }
974988
975989 public function testForeignKeyForwardsTimestamps (): void
@@ -989,27 +1003,23 @@ public function testForeignKeyForwardsTimestamps(): void
9891003 public function testForeignKeyForwardsAddColumnAndModifyColumn (): void
9901004 {
9911005 $ bp = new Table ();
992- $ bp ->id ()
993- ->integer ('user_id ' )
994- ->foreignKey ('user_id ' )->references ('id ' )->on ('users ' )
995- ->addColumn ('phone ' , ColumnType::String, 30 )
996- ->modifyColumn ('email ' , ColumnType::String, 200 );
1006+ $ bp ->integer ('user_id ' );
1007+ $ bp ->foreignKey ('user_id ' )->addColumn ('phone ' , ColumnType::String, 30 );
1008+ $ bp ->foreignKey ('user_id ' )->modifyColumn ('email ' , ColumnType::String, 200 );
9971009
998- $ this ->assertCount (4 , $ bp ->columns );
999- $ this ->assertSame ('phone ' , $ bp ->columns [2 ]->name );
1000- $ this ->assertFalse ($ bp ->columns [2 ]->isModify );
1001- $ this ->assertSame ('email ' , $ bp ->columns [3 ]->name );
1002- $ this ->assertTrue ($ bp ->columns [3 ]->isModify );
1010+ $ this ->assertCount (3 , $ bp ->columns );
1011+ $ this ->assertSame ('phone ' , $ bp ->columns [1 ]->name );
1012+ $ this ->assertFalse ($ bp ->columns [1 ]->isModify );
1013+ $ this ->assertSame ('email ' , $ bp ->columns [2 ]->name );
1014+ $ this ->assertTrue ($ bp ->columns [2 ]->isModify );
10031015 }
10041016
10051017 public function testForeignKeyForwardsRenameColumnAndDropColumn (): void
10061018 {
10071019 $ bp = new Table ();
1008- $ bp ->id ()
1009- ->integer ('user_id ' )
1010- ->foreignKey ('user_id ' )->references ('id ' )->on ('users ' )
1011- ->renameColumn ('old ' , 'new ' )
1012- ->dropColumn ('legacy ' );
1020+ $ bp ->integer ('user_id ' );
1021+ $ bp ->foreignKey ('user_id ' )->renameColumn ('old ' , 'new ' );
1022+ $ bp ->foreignKey ('user_id ' )->dropColumn ('legacy ' );
10131023
10141024 $ this ->assertSame ([['from ' => 'old ' , 'to ' => 'new ' ]], \array_map (
10151025 fn ($ r ) => ['from ' => $ r ->from , 'to ' => $ r ->to ],
@@ -1021,14 +1031,14 @@ public function testForeignKeyForwardsRenameColumnAndDropColumn(): void
10211031 public function testForeignKeyForwardsIndexFamily (): void
10221032 {
10231033 $ bp = new Table ();
1024- $ bp ->id ()
1025- -> integer ('user_id ' )
1026- -> foreignKey ( ' user_id ' )-> references ( ' id ' )-> on ( ' users ' )
1027- ->index (['user_id ' ])
1028- ->uniqueIndex (['user_id ' ], 'uq_user ' )
1029- ->fulltextIndex (['user_id ' ], 'ft_user ' )
1030- ->spatialIndex (['user_id ' ], 'sp_user ' )
1031- ->addIndex ('custom_user ' , ['user_id ' ], IndexType::Unique);
1034+ $ bp ->integer ( ' user_id ' );
1035+ $ fk = fn (): ForeignKey => $ bp -> foreignKey ('user_id ' );
1036+
1037+ $ fk () ->index (['user_id ' ]);
1038+ $ fk () ->uniqueIndex (['user_id ' ], 'uq_user ' );
1039+ $ fk () ->fulltextIndex (['user_id ' ], 'ft_user ' );
1040+ $ fk () ->spatialIndex (['user_id ' ], 'sp_user ' );
1041+ $ fk () ->addIndex ('custom_user ' , ['user_id ' ], IndexType::Unique);
10321042
10331043 $ this ->assertCount (5 , $ bp ->indexes );
10341044 $ this ->assertSame (IndexType::Index, $ bp ->indexes [0 ]->type );
@@ -1106,11 +1116,9 @@ public function testForeignKeyForwardsCheck(): void
11061116 public function testForeignKeyForwardsRawColumnAndRawIndex (): void
11071117 {
11081118 $ bp = new Table ();
1109- $ bp ->id ()
1110- ->integer ('user_id ' )
1111- ->foreignKey ('user_id ' )->references ('id ' )->on ('users ' )
1112- ->rawColumn ('`extra` JSON NOT NULL ' )
1113- ->rawIndex ('FULLTEXT INDEX `ft` (`extra`) ' );
1119+ $ bp ->integer ('user_id ' );
1120+ $ bp ->foreignKey ('user_id ' )->rawColumn ('`extra` JSON NOT NULL ' );
1121+ $ bp ->foreignKey ('user_id ' )->rawIndex ('FULLTEXT INDEX `ft` (`extra`) ' );
11141122
11151123 $ this ->assertSame (['`extra` JSON NOT NULL ' ], $ bp ->rawColumnDefs );
11161124 $ this ->assertSame (['FULLTEXT INDEX `ft` (`extra`) ' ], $ bp ->rawIndexDefs );
@@ -1140,12 +1148,10 @@ public function testForeignKeyForwardsPartitionFamily(): void
11401148 public function testForeignKeyForwardsEngineAndOrderByAndTtl (): void
11411149 {
11421150 $ bp = new Table ();
1143- $ bp ->integer ('id ' )
1144- ->integer ('user_id ' )
1145- ->foreignKey ('user_id ' )->references ('id ' )->on ('users ' )
1146- ->engine (Engine::MergeTree)
1147- ->orderBy (['id ' ])
1148- ->ttl ('id + INTERVAL 1 DAY ' );
1151+ $ bp ->integer ('id ' )->integer ('user_id ' );
1152+ $ bp ->foreignKey ('user_id ' )->engine (Engine::MergeTree);
1153+ $ bp ->foreignKey ('user_id ' )->orderBy (['id ' ]);
1154+ $ bp ->foreignKey ('user_id ' )->ttl ('id + INTERVAL 1 DAY ' );
11491155
11501156 $ this ->assertSame (Engine::MergeTree, $ bp ->engine );
11511157 $ this ->assertSame (['id ' ], $ bp ->orderBy );
0 commit comments