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,7 +282,6 @@ public function clean(): void
268
282
}
269
283
270
284
/**
271
- *
272
285
* @return array<array<string, string>>
273
286
* Each item is an array structured as such:
274
287
* [
@@ -282,10 +295,24 @@ public function collectGarbage(): array
282
295
$ schemaManager = $ this ->databaseSession ->getSchemaManager ();
283
296
$ garbage = [];
284
297
298
+ $ prefixes = [AbstractAnonymizer::TEMP_TABLE_PREFIX ];
299
+ // For backward compatibilty, removes legacy table names as well.
300
+ foreach (self ::DEPRECATED_TEMP_TABLE_PREFIX as $ prefix ) {
301
+ $ prefixes [] = $ prefix ;
302
+ }
303
+
285
304
foreach ($ schemaManager ->listTables () as $ tableName ) {
286
- if (\str_starts_with ($ tableName , AbstractAnonymizer::TEMP_TABLE_PREFIX )) {
287
- $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
288
- } else {
305
+ $ found = false ;
306
+ foreach ($ prefixes as $ prefix ) {
307
+ if (\str_starts_with ($ tableName , $ prefix )) {
308
+ $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
309
+ $ found = true ;
310
+ break ;
311
+ }
312
+ }
313
+
314
+ // If table is not marked for deletion, lookup for added columns.
315
+ if (!$ found ) {
289
316
$ garbage = \array_merge ($ garbage , $ this ->collectGarbageInto ($ tableName ));
290
317
}
291
318
}
@@ -320,15 +347,24 @@ protected function collectGarbageInto(string $table): array
320
347
}
321
348
}
322
349
350
+ $ names = [AbstractAnonymizer::JOIN_ID ];
351
+ // For backward compatibilty, removes legacy column names as well.
352
+ foreach (self ::DEPRECATED_JOIN_ID as $ name ) {
353
+ $ names [] = $ name ;
354
+ }
355
+
323
356
foreach ($ table ->getColumns () as $ column ) {
324
357
\assert ($ column instanceof Column);
325
358
326
- if (AbstractAnonymizer::JOIN_ID === $ column ->getName ()) {
327
- $ garbage [] = [
328
- 'type ' => 'column ' ,
329
- 'name ' => $ column ->getName (),
330
- 'table ' => $ table ->getName (),
331
- ];
359
+ foreach ($ names as $ name ) {
360
+ if ($ name === $ column ->getName ()) {
361
+ $ garbage [] = [
362
+ 'type ' => 'column ' ,
363
+ 'name ' => $ column ->getName (),
364
+ 'table ' => $ table ->getName (),
365
+ ];
366
+ break ;
367
+ }
332
368
}
333
369
}
334
370
0 commit comments