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