12
12
use MakinaCorpus \DbToolsBundle \Helper \Output \NullOutput ;
13
13
use MakinaCorpus \DbToolsBundle \Helper \Output \OutputInterface ;
14
14
use MakinaCorpus \QueryBuilder \DatabaseSession ;
15
+ use MakinaCorpus \QueryBuilder \Vendor ;
15
16
use MakinaCorpus \QueryBuilder \Error \Server \DatabaseObjectDoesNotExistError ;
16
17
use MakinaCorpus \QueryBuilder \Query \Update ;
17
18
use MakinaCorpus \QueryBuilder \Schema \Read \Column ;
18
19
use MakinaCorpus \QueryBuilder \Schema \Read \Index ;
19
- use MakinaCorpus \QueryBuilder \Schema \Read \Table ;
20
20
use MakinaCorpus \QueryBuilder \Type \Type ;
21
- use MakinaCorpus \QueryBuilder \Vendor ;
22
21
use Psr \Log \LoggerAwareInterface ;
23
22
use Psr \Log \LoggerAwareTrait ;
24
23
use Psr \Log \NullLogger ;
@@ -27,6 +26,21 @@ class Anonymizator implements LoggerAwareInterface
27
26
{
28
27
use LoggerAwareTrait;
29
28
29
+ /**
30
+ * Used for the garbage collection/cleanup procedure, removes tables with older names.
31
+ */
32
+ protected const DEPRECATED_JOIN_ID = [
33
+ '_anonymize_id ' , // @todo Remove in 3.0
34
+ '_anonymizer_id ' , // @todo Remove in 3.0
35
+ ];
36
+
37
+ /**
38
+ * Used for the garbage collection/cleanup procedure, removes tables with older names.
39
+ */
40
+ protected const DEPRECATED_TEMP_TABLE_PREFIX = [
41
+ 'anonymizer_sample_ ' , // @todo Remove in 3.0
42
+ ];
43
+
30
44
private OutputInterface $ output ;
31
45
32
46
public function __construct (
@@ -268,6 +282,10 @@ public function clean(): void
268
282
}
269
283
270
284
/**
285
+ * @param bool $withDeprecated
286
+ * Also collect garbage from previous versions. This is not default
287
+ * because it might conflict with legit project tables. We leave this
288
+ * responsability to the user.
271
289
*
272
290
* @return array<array<string, string>>
273
291
* Each item is an array structured as such:
@@ -277,23 +295,39 @@ public function clean(): void
277
295
* 'table' => 'table_name' (only for columns and indexes)
278
296
* ]
279
297
*/
280
- public function collectGarbage (): array
298
+ public function collectGarbage (bool $ withDeprecated = false ): array
281
299
{
282
300
$ schemaManager = $ this ->databaseSession ->getSchemaManager ();
283
301
$ garbage = [];
284
302
303
+ $ prefixes = [AbstractAnonymizer::TEMP_TABLE_PREFIX ];
304
+ // For backward compatibilty, removes legacy table names as well.
305
+ if ($ withDeprecated ) {
306
+ foreach (self ::DEPRECATED_TEMP_TABLE_PREFIX as $ prefix ) {
307
+ $ prefixes [] = $ prefix ;
308
+ }
309
+ }
310
+
285
311
foreach ($ schemaManager ->listTables () as $ tableName ) {
286
- if (\str_starts_with ($ tableName , AbstractAnonymizer::TEMP_TABLE_PREFIX )) {
287
- $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
288
- } else {
289
- $ garbage = \array_merge ($ garbage , $ this ->collectGarbageInto ($ tableName ));
312
+ $ found = false ;
313
+ foreach ($ prefixes as $ prefix ) {
314
+ if (\str_starts_with ($ tableName , $ prefix )) {
315
+ $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
316
+ $ found = true ;
317
+ break ;
318
+ }
319
+ }
320
+
321
+ // If table is not marked for deletion, lookup for added columns.
322
+ if (!$ found ) {
323
+ $ garbage = \array_merge ($ garbage , $ this ->collectGarbageInto ($ tableName , $ withDeprecated ));
290
324
}
291
325
}
292
326
293
327
return $ garbage ;
294
328
}
295
329
296
- protected function collectGarbageInto (string $ table ): array
330
+ protected function collectGarbageInto (string $ table, bool $ withDeprecated = false ): array
297
331
{
298
332
$ schemaManager = $ this ->databaseSession ->getSchemaManager ();
299
333
$ garbage = [];
@@ -320,15 +354,26 @@ protected function collectGarbageInto(string $table): array
320
354
}
321
355
}
322
356
357
+ $ names = [AbstractAnonymizer::JOIN_ID ];
358
+ // For backward compatibilty, removes legacy column names as well.
359
+ if ($ withDeprecated ) {
360
+ foreach (self ::DEPRECATED_JOIN_ID as $ name ) {
361
+ $ names [] = $ name ;
362
+ }
363
+ }
364
+
323
365
foreach ($ table ->getColumns () as $ column ) {
324
366
\assert ($ column instanceof Column);
325
367
326
- if (AbstractAnonymizer::JOIN_ID === $ column ->getName ()) {
327
- $ garbage [] = [
328
- 'type ' => 'column ' ,
329
- 'name ' => $ column ->getName (),
330
- 'table ' => $ table ->getName (),
331
- ];
368
+ foreach ($ names as $ name ) {
369
+ if ($ name === $ column ->getName ()) {
370
+ $ garbage [] = [
371
+ 'type ' => 'column ' ,
372
+ 'name ' => $ column ->getName (),
373
+ 'table ' => $ table ->getName (),
374
+ ];
375
+ break ;
376
+ }
332
377
}
333
378
}
334
379
0 commit comments