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('
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
->createQuery('
47 FROM ' . $this->getTable('tag') . "
48 WHERE LOWER(label) = :label
51 $tags->setParameter('label', $label);
58 foreach ($tags->fetchAll() as $tag) {
59 // Ignore the first tag as we use it as the new reference tag
69 if (count($ids) > 0) {
72 UPDATE ' . $this->getTable('entry_tag') . '
73 SET tag_id = ' . $newId . '
74 WHERE tag_id IN (' . implode(',', $ids) . ')'
79 DELETE FROM ' . $this->getTable('tag') . '
80 WHERE id IN (' . implode(',', $ids) . ')'
85 // Iterate over all tags to lowercase them
87 UPDATE ' . $this->getTable('tag') . '
88 SET label = LOWER(label)'
93 * @param Schema $schema
95 public function down(Schema
$schema)
97 throw new SkipMigrationException('Too complex ...');
100 private function getTable($tableName)
102 return $this->container
->getParameter('database_table_prefix') . $tableName;