44
55use Illuminate \Contracts \Console \Kernel ;
66use Illuminate \Database \ConnectionInterface ;
7+ use Illuminate \Database \Query \Expression ;
8+ use Illuminate \Database \Schema \PostgresBuilder ;
79use Illuminate \Foundation \Testing \Traits \CanConfigureMigrationCommands ;
810use Illuminate \Support \Collection ;
911
@@ -84,32 +86,58 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,
8486
8587 $ connection ->unsetEventDispatcher ();
8688
87- (new Collection (static :: $ allTables [ $ name ] ??= $ connection -> getSchemaBuilder ()-> getTableListing ( )))
89+ (new Collection ($ this -> getAllTablesForConnection ( $ connection , $ name )))
8890 ->when (
89- property_exists ($ this , 'tablesToTruncate ' ),
90- fn ($ tables ) => $ tables ->intersect ($ this ->tablesToTruncate ),
91- fn ($ tables ) => $ tables ->diff ($ this ->exceptTables ($ name ))
91+ $ this ->tablesToTruncate ($ connection , $ name ),
92+ function (Collection $ tables , array $ tablesToTruncate ) {
93+ return $ tables ->filter (fn (array $ table ) => $ this ->tableExistsIn ($ table , $ tablesToTruncate ));
94+ },
95+ function (Collection $ tables ) use ($ connection , $ name ) {
96+ $ exceptTables = $ this ->exceptTables ($ connection , $ name );
97+
98+ return $ tables ->filter (fn (array $ table ) => ! $ this ->tableExistsIn ($ table , $ exceptTables ));
99+ }
92100 )
93- ->filter (fn ($ table ) => $ connection ->table ($ this ->withoutTablePrefix ($ connection , $ table ))->exists ())
94- ->each (fn ($ table ) => $ connection ->table ($ this ->withoutTablePrefix ($ connection , $ table ))->truncate ());
101+ ->each (function (array $ table ) use ($ connection ) {
102+ $ table = $ connection ->table (
103+ new Expression ($ table ['schema ' ] ? $ table ['schema ' ].'. ' .$ table ['name ' ] : $ table ['name ' ])
104+ );
105+
106+ if ($ table ->exists ()) {
107+ $ table ->truncate ();
108+ }
109+ });
95110
96111 $ connection ->setEventDispatcher ($ dispatcher );
97112 }
98113
99114 /**
100- * Remove the table prefix from a table name, if it exists.
101- *
102- * @param \Illuminate\Database\ConnectionInterface $connection
103- * @param string $table
104- * @return string
115+ * Get all the tables that belong to the connection.
105116 */
106- protected function withoutTablePrefix (ConnectionInterface $ connection , string $ table )
117+ protected function getAllTablesForConnection (ConnectionInterface $ connection , ? string $ name ): array
107118 {
108- $ prefix = $ connection ->getTablePrefix ();
119+ if (isset (static ::$ allTables [$ name ])) {
120+ return static ::$ allTables [$ name ];
121+ }
122+
123+ $ schema = $ connection ->getSchemaBuilder ();
109124
110- return str_starts_with ($ table , $ prefix )
111- ? substr ($ table , strlen ($ prefix ))
112- : $ table ;
125+ return static ::$ allTables [$ name ] = (new Collection ($ schema ->getTables ()))->when (
126+ $ schema instanceof PostgresBuilder ? $ schema ->getSchemas () : null ,
127+ fn (Collection $ tables , array $ schemas ) => $ tables ->filter (
128+ fn (array $ table ) => in_array ($ table ['schema ' ], $ schemas )
129+ )
130+ )->all ();
131+ }
132+
133+ /**
134+ * Determine if a table exists in the given list, with or without its schema.
135+ */
136+ protected function tableExistsIn (array $ table , array $ tables ): bool
137+ {
138+ return $ table ['schema ' ]
139+ ? ! empty (array_intersect ([$ table ['name ' ], $ table ['schema ' ].'. ' .$ table ['name ' ]], $ tables ))
140+ : in_array ($ table ['name ' ], $ tables );
113141 }
114142
115143 /**
@@ -123,33 +151,32 @@ protected function connectionsToTruncate(): array
123151 ? $ this ->connectionsToTruncate : [null ];
124152 }
125153
154+ /**
155+ * Get the tables that should be truncated.
156+ */
157+ protected function tablesToTruncate (ConnectionInterface $ connection , ?string $ connectionName ): ?array
158+ {
159+ return property_exists ($ this , 'tablesToTruncate ' ) && is_array ($ this ->tablesToTruncate )
160+ ? $ this ->tablesToTruncate [$ connectionName ] ?? $ this ->tablesToTruncate
161+ : null ;
162+ }
163+
126164 /**
127165 * Get the tables that should not be truncated.
128- *
129- * @param string|null $connectionName
130- * @return array
131166 */
132- protected function exceptTables (?string $ connectionName ): array
167+ protected function exceptTables (ConnectionInterface $ connection , ?string $ connectionName ): array
133168 {
134169 $ migrations = $ this ->app ['config ' ]->get ('database.migrations ' );
135170
136- $ migrationsTable = is_array ($ migrations ) ? ($ migrations ['table ' ] ?? null ) : $ migrations ;
137-
138- if (property_exists ($ this , 'exceptTables ' )) {
139- if (array_is_list ($ this ->exceptTables ?? [])) {
140- return array_merge (
141- $ this ->exceptTables ?? [],
142- [$ migrationsTable ],
143- );
144- }
171+ $ migrationsTable = is_array ($ migrations ) ? ($ migrations ['table ' ] ?? 'migrations ' ) : $ migrations ;
172+ $ migrationsTable = $ connection ->getTablePrefix ().$ migrationsTable ;
145173
146- return array_merge (
147- $ this ->exceptTables [$ connectionName ] ?? [],
174+ return property_exists ($ this , 'exceptTables ' ) && is_array ($ this ->exceptTables )
175+ ? array_merge (
176+ $ this ->exceptTables [$ connectionName ] ?? $ this ->exceptTables ,
148177 [$ migrationsTable ],
149- );
150- }
151-
152- return [$ migrationsTable ];
178+ )
179+ : [$ migrationsTable ];
153180 }
154181
155182 /**
0 commit comments