4
4
5
5
use Illuminate \Contracts \Console \Kernel ;
6
6
use Illuminate \Database \ConnectionInterface ;
7
+ use Illuminate \Database \Query \Expression ;
8
+ use Illuminate \Database \Schema \PostgresBuilder ;
7
9
use Illuminate \Foundation \Testing \Traits \CanConfigureMigrationCommands ;
8
10
use Illuminate \Support \Collection ;
9
11
@@ -84,32 +86,58 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,
84
86
85
87
$ connection ->unsetEventDispatcher ();
86
88
87
- (new Collection (static :: $ allTables [ $ name ] ??= $ connection -> getSchemaBuilder ()-> getTableListing ( )))
89
+ (new Collection ($ this -> getAllTablesForConnection ( $ connection , $ name )))
88
90
->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
+ }
92
100
)
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
+ });
95
110
96
111
$ connection ->setEventDispatcher ($ dispatcher );
97
112
}
98
113
99
114
/**
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.
105
116
*/
106
- protected function withoutTablePrefix (ConnectionInterface $ connection , string $ table )
117
+ protected function getAllTablesForConnection (ConnectionInterface $ connection , ? string $ name ): array
107
118
{
108
- $ prefix = $ connection ->getTablePrefix ();
119
+ if (isset (static ::$ allTables [$ name ])) {
120
+ return static ::$ allTables [$ name ];
121
+ }
122
+
123
+ $ schema = $ connection ->getSchemaBuilder ();
109
124
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 );
113
141
}
114
142
115
143
/**
@@ -123,33 +151,32 @@ protected function connectionsToTruncate(): array
123
151
? $ this ->connectionsToTruncate : [null ];
124
152
}
125
153
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
+
126
164
/**
127
165
* Get the tables that should not be truncated.
128
- *
129
- * @param string|null $connectionName
130
- * @return array
131
166
*/
132
- protected function exceptTables (?string $ connectionName ): array
167
+ protected function exceptTables (ConnectionInterface $ connection , ?string $ connectionName ): array
133
168
{
134
169
$ migrations = $ this ->app ['config ' ]->get ('database.migrations ' );
135
170
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 ;
145
173
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 ,
148
177
[$ migrationsTable ],
149
- );
150
- }
151
-
152
- return [$ migrationsTable ];
178
+ )
179
+ : [$ migrationsTable ];
153
180
}
154
181
155
182
/**
0 commit comments