2017-07-20 22:05:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Application\Migrations;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 16:59:02 +02:00
|
|
|
* Changed tags to lowercase.
|
2017-07-20 22:05:44 +02:00
|
|
|
*/
|
|
|
|
class Version20170719231144 extends AbstractMigration implements ContainerAwareInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
public function setContainer(ContainerInterface $container = null)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function up(Schema $schema)
|
|
|
|
{
|
2017-10-09 16:47:15 +02:00
|
|
|
$this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
|
2017-08-27 16:59:02 +02:00
|
|
|
|
2017-07-20 22:05:44 +02:00
|
|
|
// Find tags which need to be merged
|
2017-08-27 16:59:02 +02:00
|
|
|
$dupTags = $this->connection->query('
|
2017-07-20 22:05:44 +02:00
|
|
|
SELECT LOWER(label)
|
2017-08-27 16:59:02 +02:00
|
|
|
FROM ' . $this->getTable('tag') . '
|
2017-07-20 22:05:44 +02:00
|
|
|
GROUP BY LOWER(label)
|
2017-08-27 16:59:02 +02:00
|
|
|
HAVING COUNT(*) > 1'
|
2017-07-20 22:05:44 +02:00
|
|
|
);
|
|
|
|
$dupTags->execute();
|
|
|
|
|
|
|
|
foreach ($dupTags->fetchAll() as $duplicates) {
|
|
|
|
$label = $duplicates['LOWER(label)'];
|
|
|
|
|
|
|
|
// Retrieve all duplicate tags for a given tag
|
2017-08-27 16:59:02 +02:00
|
|
|
$tags = $this->connection->query('
|
2017-07-20 22:05:44 +02:00
|
|
|
SELECT id
|
2017-08-27 16:59:02 +02:00
|
|
|
FROM ' . $this->getTable('tag') . "
|
|
|
|
WHERE LOWER(label) = '" . $label . "'
|
2017-07-20 22:05:44 +02:00
|
|
|
ORDER BY id ASC"
|
|
|
|
);
|
|
|
|
$tags->execute();
|
|
|
|
|
|
|
|
$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 16:59:02 +02:00
|
|
|
$this->addSql('
|
|
|
|
UPDATE ' . $this->getTable('entry_tag') . '
|
|
|
|
SET tag_id = ' . $newId . '
|
|
|
|
WHERE tag_id IN (' . implode(',', $ids) . ')'
|
2017-07-20 22:05:44 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Delete unused tags
|
2017-08-27 16:59:02 +02:00
|
|
|
$this->addSql('
|
|
|
|
DELETE FROM ' . $this->getTable('tag') . '
|
|
|
|
WHERE id IN (' . implode(',', $ids) . ')'
|
2017-07-20 22:05:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over all tags to lowercase them
|
2017-08-27 16:59:02 +02:00
|
|
|
$this->addSql('
|
|
|
|
UPDATE ' . $this->getTable('tag') . '
|
|
|
|
SET label = LOWER(label)'
|
2017-07-20 22:05:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
*/
|
|
|
|
public function down(Schema $schema)
|
|
|
|
{
|
|
|
|
throw new SkipMigrationException('Too complex ...');
|
|
|
|
}
|
2017-08-27 16:59:02 +02:00
|
|
|
|
|
|
|
private function getTable($tableName)
|
|
|
|
{
|
|
|
|
return $this->container->getParameter('database_table_prefix') . $tableName;
|
|
|
|
}
|
2017-07-20 22:05:44 +02:00
|
|
|
}
|