X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=app%2FDoctrineMigrations%2FVersion20170719231144.php;h=93fe7f26031c44fe426f51ff78601fbc9776b6eb;hb=2a1ceb67b4400f46f4d3067e887ff54aa906f0a2;hp=0f5fa75ae527c2708ee2eec30193723ba772e447;hpb=b9c26b6330f688ce18af65a05a821f70dd73259c;p=github%2Fwallabag%2Fwallabag.git diff --git a/app/DoctrineMigrations/Version20170719231144.php b/app/DoctrineMigrations/Version20170719231144.php index 0f5fa75a..93fe7f26 100644 --- a/app/DoctrineMigrations/Version20170719231144.php +++ b/app/DoctrineMigrations/Version20170719231144.php @@ -2,36 +2,24 @@ namespace Application\Migrations; -use Doctrine\DBAL\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Wallabag\CoreBundle\Doctrine\WallabagMigration; /** * Changed tags to lowercase. */ -class Version20170719231144 extends AbstractMigration implements ContainerAwareInterface +class Version20170719231144 extends WallabagMigration { - /** - * @var ContainerInterface - */ - private $container; - - public function setContainer(ContainerInterface $container = null) - { - $this->container = $container; - } - /** * @param Schema $schema */ public function up(Schema $schema) { - $this->skipIf($this->connection->getDatabasePlatform()->getName() === 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.'); + $this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.'); // Find tags which need to be merged $dupTags = $this->connection->query(' - SELECT LOWER(label) + SELECT LOWER(label) AS lower_label FROM ' . $this->getTable('tag') . ' GROUP BY LOWER(label) HAVING COUNT(*) > 1' @@ -39,16 +27,18 @@ class Version20170719231144 extends AbstractMigration implements ContainerAwareI $dupTags->execute(); foreach ($dupTags->fetchAll() as $duplicates) { - $label = $duplicates['LOWER(label)']; + $label = $duplicates['lower_label']; // Retrieve all duplicate tags for a given tag - $tags = $this->connection->query(' + $tags = $this->connection->executeQuery(' SELECT id - FROM ' . $this->getTable('tag') . " - WHERE LOWER(label) = '" . $label . "' - ORDER BY id ASC" + FROM ' . $this->getTable('tag') . ' + WHERE LOWER(label) = :label + ORDER BY id ASC', + [ + 'label' => $label, + ] ); - $tags->execute(); $first = true; $newId = null; @@ -65,12 +55,23 @@ class Version20170719231144 extends AbstractMigration implements ContainerAwareI } // Just in case... - if (count($ids) > 0) { + if (\count($ids) > 0) { // Merge tags $this->addSql(' UPDATE ' . $this->getTable('entry_tag') . ' SET tag_id = ' . $newId . ' - WHERE tag_id IN (' . implode(',', $ids) . ')' + WHERE tag_id IN (' . implode(',', $ids) . ') + AND entry_id NOT IN ( + SELECT entry_id + FROM (SELECT * FROM ' . $this->getTable('entry_tag') . ') AS _entry_tag + WHERE tag_id = ' . $newId . ' + )' + ); + + // Delete links to unused tags + $this->addSql(' + DELETE FROM ' . $this->getTable('entry_tag') . ' + WHERE tag_id IN (' . implode(',', $ids) . ')' ); // Delete unused tags @@ -95,9 +96,4 @@ class Version20170719231144 extends AbstractMigration implements ContainerAwareI { throw new SkipMigrationException('Too complex ...'); } - - private function getTable($tableName) - { - return $this->container->getParameter('database_table_prefix') . $tableName; - } }