3 namespace Application\Migrations
;
5 use Doctrine\DBAL\Migrations\AbstractMigration
;
6 use Doctrine\DBAL\Schema\Schema
;
7 use Symfony\Component\DependencyInjection\ContainerAwareInterface
;
8 use Symfony\Component\DependencyInjection\ContainerInterface
;
11 * Changed tags to lowercase.
13 class Version20170719231144
extends AbstractMigration
implements ContainerAwareInterface
16 * @var ContainerInterface
20 public function setContainer(ContainerInterface
$container = null)
22 $this->container
= $container;
26 * @param Schema $schema
28 public function up(Schema
$schema)
30 $this->skipIf('sqlite' === $this->connection
->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
32 // Find tags which need to be merged
33 $dupTags = $this->connection
->query('
34 SELECT LOWER(label) AS lower_label
35 FROM ' . $this->getTable('tag') . '
41 foreach ($dupTags->fetchAll() as $duplicates) {
42 $label = $duplicates['lower_label'];
44 // Retrieve all duplicate tags for a given tag
45 $tags = $this->connection
->executeQuery('
47 FROM ' . $this->getTable('tag') . '
48 WHERE LOWER(label) = :label
59 foreach ($tags->fetchAll() as $tag) {
60 // Ignore the first tag as we use it as the new reference tag
70 if (count($ids) > 0) {
73 UPDATE ' . $this->getTable('entry_tag') . '
74 SET tag_id = ' . $newId . '
75 WHERE tag_id IN (' . implode(',', $ids) . ')
78 FROM (SELECT * FROM ' . $this->getTable('entry_tag') . ') AS _entry_tag
79 WHERE tag_id = ' . $newId . '
83 // Delete links to unused tags
85 DELETE FROM ' . $this->getTable('entry_tag') . '
86 WHERE tag_id IN (' . implode(',', $ids) . ')'
91 DELETE FROM ' . $this->getTable('tag') . '
92 WHERE id IN (' . implode(',', $ids) . ')'
97 // Iterate over all tags to lowercase them
99 UPDATE ' . $this->getTable('tag') . '
100 SET label = LOWER(label)'
105 * @param Schema $schema
107 public function down(Schema
$schema)
109 throw new SkipMigrationException('Too complex ...');
112 private function getTable($tableName)
114 return $this->container
->getParameter('database_table_prefix') . $tableName;