2017-07-20 22:05:44 +02:00
< ? php
namespace Application\Migrations ;
2023-08-05 19:35:09 +02:00
use Doctrine\DBAL\Platforms\SqlitePlatform ;
2017-07-20 22:05:44 +02:00
use Doctrine\DBAL\Schema\Schema ;
2018-06-14 13:43:09 +02:00
use Wallabag\CoreBundle\Doctrine\WallabagMigration ;
2017-07-20 22:05:44 +02:00
/**
2017-08-27 16:59:02 +02:00
* Changed tags to lowercase .
2017-07-20 22:05:44 +02:00
*/
2018-06-14 13:43:09 +02:00
class Version20170719231144 extends WallabagMigration
2017-07-20 22:05:44 +02:00
{
2022-12-14 14:36:29 +01:00
public function up ( Schema $schema ) : void
2017-07-20 22:05:44 +02:00
{
2023-08-05 19:35:09 +02:00
$this -> skipIf ( $this -> connection -> getDatabasePlatform () instanceof SqlitePlatform , '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-12-10 19:31:30 +01:00
SELECT LOWER ( label ) AS 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
);
2022-12-14 14:36:29 +01:00
foreach ( $dupTags -> fetchAllAssociative () as $duplicates ) {
2017-12-10 19:31:30 +01:00
$label = $duplicates [ 'lower_label' ];
2017-07-20 22:05:44 +02:00
// Retrieve all duplicate tags for a given tag
2022-12-14 14:36:29 +01:00
$tags = $this -> connection -> query ( '
2017-07-20 22:05:44 +02:00
SELECT id
2017-12-10 19:31:30 +01:00
FROM ' . $this->getTable(' tag ') . '
2017-11-27 22:48:17 +01:00
WHERE LOWER ( label ) = : label
2017-12-10 19:31:30 +01:00
ORDER BY id ASC ' ,
[
'label' => $label ,
]
2017-07-20 22:05:44 +02:00
);
$first = true ;
$newId = null ;
$ids = [];
2022-12-14 14:36:29 +01:00
foreach ( $tags -> fetchAllAssociative () as $tag ) {
2017-07-20 22:05:44 +02:00
// 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...
2018-09-05 14:25:32 +02:00
if ( \count ( $ids ) > 0 ) {
2017-07-20 22:05:44 +02:00
// Merge tags
2017-08-27 16:59:02 +02:00
$this -> addSql ( '
UPDATE ' . $this->getTable(' entry_tag ') . '
SET tag_id = ' . $newId . '
2017-12-10 19:31:30 +01:00
WHERE tag_id IN ( ' . implode(' , ', $ids) . ' )
AND entry_id NOT IN (
SELECT entry_id
2017-12-15 13:59:02 +01:00
FROM ( SELECT * FROM ' . $this->getTable(' entry_tag ') . ' ) AS _entry_tag
2017-12-10 19:31:30 +01: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 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
);
}
2022-12-14 14:36:29 +01:00
public function down ( Schema $schema ) : void
2017-07-20 22:05:44 +02:00
{
throw new SkipMigrationException ( 'Too complex ...' );
}
}