2017-07-20 20:05:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Application\Migrations;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2018-06-14 11:43:09 +00:00
|
|
|
use Wallabag\CoreBundle\Doctrine\WallabagMigration;
|
2017-07-20 20:05:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-27 14:59:02 +00:00
|
|
|
* Changed tags to lowercase.
|
2017-07-20 20:05:44 +00:00
|
|
|
*/
|
2018-06-14 11:43:09 +00:00
|
|
|
class Version20170719231144 extends WallabagMigration
|
2017-07-20 20:05:44 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function up(Schema $schema)
|
|
|
|
{
|
2017-10-09 14:47:15 +00:00
|
|
|
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
|
2017-08-27 14:59:02 +00:00
|
|
|
|
2017-07-20 20:05:44 +00:00
|
|
|
// Find tags which need to be merged
|
2017-08-27 14:59:02 +00:00
|
|
|
$dupTags = $this->connection->query('
|
2017-12-10 18:31:30 +00:00
|
|
|
SELECT LOWER(label) AS lower_label
|
2017-08-27 14:59:02 +00:00
|
|
|
FROM ' . $this->getTable('tag') . '
|
2017-07-20 20:05:44 +00:00
|
|
|
GROUP BY LOWER(label)
|
2017-08-27 14:59:02 +00:00
|
|
|
HAVING COUNT(*) > 1'
|
2017-07-20 20:05:44 +00:00
|
|
|
);
|
|
|
|
$dupTags->execute();
|
|
|
|
|
|
|
|
foreach ($dupTags->fetchAll() as $duplicates) {
|
2017-12-10 18:31:30 +00:00
|
|
|
$label = $duplicates['lower_label'];
|
2017-07-20 20:05:44 +00:00
|
|
|
|
|
|
|
// Retrieve all duplicate tags for a given tag
|
2017-12-10 18:31:30 +00:00
|
|
|
$tags = $this->connection->executeQuery('
|
2017-07-20 20:05:44 +00:00
|
|
|
SELECT id
|
2017-12-10 18:31:30 +00:00
|
|
|
FROM ' . $this->getTable('tag') . '
|
2017-11-27 21:48:17 +00:00
|
|
|
WHERE LOWER(label) = :label
|
2017-12-10 18:31:30 +00:00
|
|
|
ORDER BY id ASC',
|
|
|
|
[
|
|
|
|
'label' => $label,
|
|
|
|
]
|
2017-07-20 20:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$first = true;
|
|
|
|
$newId = null;
|
|
|
|
$ids = [];
|
|
|
|
|
|
|
|
foreach ($tags->fetchAll() as $tag) {
|
|
|
|
// Ignore the first tag as we use it as the new reference tag
|
|
|
|
if ($first) {
|
|
|
|
$first = false;
|
|
|
|
$newId = $tag['id'];
|
|
|
|
} else {
|
|
|
|
$ids[] = $tag['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just in case...
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
// Merge tags
|
2017-08-27 14:59:02 +00:00
|
|
|
$this->addSql('
|
|
|
|
UPDATE ' . $this->getTable('entry_tag') . '
|
|
|
|
SET tag_id = ' . $newId . '
|
2017-12-10 18:31:30 +00:00
|
|
|
WHERE tag_id IN (' . implode(',', $ids) . ')
|
|
|
|
AND entry_id NOT IN (
|
|
|
|
SELECT entry_id
|
2017-12-15 12:59:02 +00:00
|
|
|
FROM (SELECT * FROM ' . $this->getTable('entry_tag') . ') AS _entry_tag
|
2017-12-10 18:31:30 +00:00
|
|
|
WHERE tag_id = ' . $newId . '
|
|
|
|
)'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Delete links to unused tags
|
|
|
|
$this->addSql('
|
|
|
|
DELETE FROM ' . $this->getTable('entry_tag') . '
|
|
|
|
WHERE tag_id IN (' . implode(',', $ids) . ')'
|
2017-07-20 20:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Delete unused tags
|
2017-08-27 14:59:02 +00:00
|
|
|
$this->addSql('
|
|
|
|
DELETE FROM ' . $this->getTable('tag') . '
|
|
|
|
WHERE id IN (' . implode(',', $ids) . ')'
|
2017-07-20 20:05:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over all tags to lowercase them
|
2017-08-27 14:59:02 +00:00
|
|
|
$this->addSql('
|
|
|
|
UPDATE ' . $this->getTable('tag') . '
|
|
|
|
SET label = LOWER(label)'
|
2017-07-20 20:05:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function down(Schema $schema)
|
|
|
|
{
|
|
|
|
throw new SkipMigrationException('Too complex ...');
|
|
|
|
}
|
|
|
|
}
|