]>
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(' |
bd164a75 | 34 | SELECT 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) { | |
42 | $label = $duplicates['LOWER(label)']; | |
43 | ||
44 | // Retrieve all duplicate tags for a given tag | |
6c5904ba | 45 | $tags = $this->connection->createQuery(' |
bd164a75 | 46 | SELECT id |
7b4f6688 | 47 | FROM ' . $this->getTable('tag') . " |
6c5904ba | 48 | WHERE LOWER(label) = :label |
bd164a75 KD |
49 | ORDER BY id ASC" |
50 | ); | |
6c5904ba | 51 | $tags->setParameter('label', $label); |
bd164a75 KD |
52 | $tags->execute(); |
53 | ||
54 | $first = true; | |
55 | $newId = null; | |
56 | $ids = []; | |
57 | ||
58 | foreach ($tags->fetchAll() as $tag) { | |
59 | // Ignore the first tag as we use it as the new reference tag | |
60 | if ($first) { | |
61 | $first = false; | |
62 | $newId = $tag['id']; | |
63 | } else { | |
64 | $ids[] = $tag['id']; | |
65 | } | |
66 | } | |
67 | ||
68 | // Just in case... | |
69 | if (count($ids) > 0) { | |
70 | // Merge tags | |
7b4f6688 KD |
71 | $this->addSql(' |
72 | UPDATE ' . $this->getTable('entry_tag') . ' | |
73 | SET tag_id = ' . $newId . ' | |
74 | WHERE tag_id IN (' . implode(',', $ids) . ')' | |
bd164a75 KD |
75 | ); |
76 | ||
77 | // Delete unused tags | |
7b4f6688 KD |
78 | $this->addSql(' |
79 | DELETE FROM ' . $this->getTable('tag') . ' | |
80 | WHERE id IN (' . implode(',', $ids) . ')' | |
bd164a75 KD |
81 | ); |
82 | } | |
83 | } | |
84 | ||
85 | // Iterate over all tags to lowercase them | |
7b4f6688 KD |
86 | $this->addSql(' |
87 | UPDATE ' . $this->getTable('tag') . ' | |
88 | SET label = LOWER(label)' | |
bd164a75 KD |
89 | ); |
90 | } | |
91 | ||
92 | /** | |
93 | * @param Schema $schema | |
94 | */ | |
95 | public function down(Schema $schema) | |
96 | { | |
97 | throw new SkipMigrationException('Too complex ...'); | |
98 | } | |
7b4f6688 KD |
99 | |
100 | private function getTable($tableName) | |
101 | { | |
102 | return $this->container->getParameter('database_table_prefix') . $tableName; | |
103 | } | |
bd164a75 | 104 | } |