]>
Commit | Line | Data |
---|---|---|
bd164a75 KD |
1 | <?php |
2 | ||
3 | namespace Application\Migrations; | |
4 | ||
bd164a75 | 5 | use Doctrine\DBAL\Schema\Schema; |
bfe7a692 | 6 | use Wallabag\CoreBundle\Doctrine\WallabagMigration; |
bd164a75 KD |
7 | |
8 | /** | |
7b4f6688 | 9 | * Changed tags to lowercase. |
bd164a75 | 10 | */ |
bfe7a692 | 11 | class Version20170719231144 extends WallabagMigration |
bd164a75 | 12 | { |
bd164a75 KD |
13 | public function up(Schema $schema) |
14 | { | |
3ef055ce | 15 | $this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.'); |
7b4f6688 | 16 | |
bd164a75 | 17 | // Find tags which need to be merged |
7b4f6688 | 18 | $dupTags = $this->connection->query(' |
40a63c8b | 19 | SELECT LOWER(label) AS lower_label |
7b4f6688 | 20 | FROM ' . $this->getTable('tag') . ' |
bd164a75 | 21 | GROUP BY LOWER(label) |
7b4f6688 | 22 | HAVING COUNT(*) > 1' |
bd164a75 KD |
23 | ); |
24 | $dupTags->execute(); | |
25 | ||
26 | foreach ($dupTags->fetchAll() as $duplicates) { | |
40a63c8b | 27 | $label = $duplicates['lower_label']; |
bd164a75 KD |
28 | |
29 | // Retrieve all duplicate tags for a given tag | |
40a63c8b | 30 | $tags = $this->connection->executeQuery(' |
bd164a75 | 31 | SELECT id |
40a63c8b | 32 | FROM ' . $this->getTable('tag') . ' |
6c5904ba | 33 | WHERE LOWER(label) = :label |
40a63c8b KD |
34 | ORDER BY id ASC', |
35 | [ | |
36 | 'label' => $label, | |
37 | ] | |
bd164a75 | 38 | ); |
bd164a75 KD |
39 | |
40 | $first = true; | |
41 | $newId = null; | |
42 | $ids = []; | |
43 | ||
44 | foreach ($tags->fetchAll() as $tag) { | |
45 | // Ignore the first tag as we use it as the new reference tag | |
46 | if ($first) { | |
47 | $first = false; | |
48 | $newId = $tag['id']; | |
49 | } else { | |
50 | $ids[] = $tag['id']; | |
51 | } | |
52 | } | |
53 | ||
54 | // Just in case... | |
2a1ceb67 | 55 | if (\count($ids) > 0) { |
bd164a75 | 56 | // Merge tags |
7b4f6688 KD |
57 | $this->addSql(' |
58 | UPDATE ' . $this->getTable('entry_tag') . ' | |
59 | SET tag_id = ' . $newId . ' | |
40a63c8b KD |
60 | WHERE tag_id IN (' . implode(',', $ids) . ') |
61 | AND entry_id NOT IN ( | |
62 | SELECT entry_id | |
caf719f1 | 63 | FROM (SELECT * FROM ' . $this->getTable('entry_tag') . ') AS _entry_tag |
40a63c8b KD |
64 | WHERE tag_id = ' . $newId . ' |
65 | )' | |
66 | ); | |
67 | ||
68 | // Delete links to unused tags | |
69 | $this->addSql(' | |
70 | DELETE FROM ' . $this->getTable('entry_tag') . ' | |
71 | WHERE tag_id IN (' . implode(',', $ids) . ')' | |
bd164a75 KD |
72 | ); |
73 | ||
74 | // Delete unused tags | |
7b4f6688 KD |
75 | $this->addSql(' |
76 | DELETE FROM ' . $this->getTable('tag') . ' | |
77 | WHERE id IN (' . implode(',', $ids) . ')' | |
bd164a75 KD |
78 | ); |
79 | } | |
80 | } | |
81 | ||
82 | // Iterate over all tags to lowercase them | |
7b4f6688 KD |
83 | $this->addSql(' |
84 | UPDATE ' . $this->getTable('tag') . ' | |
85 | SET label = LOWER(label)' | |
bd164a75 KD |
86 | ); |
87 | } | |
88 | ||
bd164a75 KD |
89 | public function down(Schema $schema) |
90 | { | |
91 | throw new SkipMigrationException('Too complex ...'); | |
92 | } | |
93 | } |