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
->query('
47 FROM ' . $this->getTable('tag') . "
48 WHERE LOWER(label) = '" . $label . "'
57 foreach ($tags->fetchAll() as $tag) {
58 // Ignore the first tag as we use it as the new reference tag
68 if (count($ids) > 0) {
71 UPDATE ' . $this->getTable('entry_tag') . '
72 SET tag_id = ' . $newId . '
73 WHERE tag_id IN (' . implode(',', $ids) . ')'
78 DELETE FROM ' . $this->getTable('tag') . '
79 WHERE id IN (' . implode(',', $ids) . ')'
84 // Iterate over all tags to lowercase them
86 UPDATE ' . $this->getTable('tag') . '
87 SET label = LOWER(label)'
92 * @param Schema $schema
94 public function down(Schema
$schema)
96 throw new SkipMigrationException('Too complex ...');
99 private function getTable($tableName)
101 return $this->container
->getParameter('database_table_prefix') . $tableName;