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:
@@ -282,10 +300,24 @@ public function collectGarbage(): array
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
+ foreach (self ::DEPRECATED_TEMP_TABLE_PREFIX as $ prefix ) {
306
+ $ prefixes [] = $ prefix ;
307
+ }
308
+
285
309
foreach ($ schemaManager ->listTables () as $ tableName ) {
286
- if (\str_starts_with ($ tableName , AbstractAnonymizer::TEMP_TABLE_PREFIX )) {
287
- $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
288
- } else {
310
+ $ found = false ;
311
+ foreach ($ prefixes as $ prefix ) {
312
+ if (\str_starts_with ($ tableName , $ prefix )) {
313
+ $ garbage [] = ['type ' => 'table ' , 'name ' => $ tableName ];
314
+ $ found = true ;
315
+ break ;
316
+ }
317
+ }
318
+
319
+ // If table is not marked for deletion, lookup for added columns.
320
+ if (!$ found ) {
289
321
$ garbage = \array_merge ($ garbage , $ this ->collectGarbageInto ($ tableName ));
290
322
}
291
323
}
@@ -320,15 +352,24 @@ protected function collectGarbageInto(string $table): array
320
352
}
321
353
}
322
354
355
+ $ names = [AbstractAnonymizer::JOIN_ID ];
356
+ // For backward compatibilty, removes legacy column names as well.
357
+ foreach (self ::DEPRECATED_JOIN_ID as $ name ) {
358
+ $ names [] = $ name ;
359
+ }
360
+
323
361
foreach ($ table ->getColumns () as $ column ) {
324
362
\assert ($ column instanceof Column);
325
363
326
- if (AbstractAnonymizer::JOIN_ID === $ column ->getName ()) {
327
- $ garbage [] = [
328
- 'type ' => 'column ' ,
329
- 'name ' => $ column ->getName (),
330
- 'table ' => $ table ->getName (),
331
- ];
364
+ foreach ($ names as $ name ) {
365
+ if ($ name === $ column ->getName ()) {
366
+ $ garbage [] = [
367
+ 'type ' => 'column ' ,
368
+ 'name ' => $ column ->getName (),
369
+ 'table ' => $ table ->getName (),
370
+ ];
371
+ break ;
372
+ }
332
373
}
333
374
}
334
375
0 commit comments