2017-07-20 20:05:44 +00:00
< ? php
namespace Application\Migrations ;
2023-08-05 17:35:09 +00:00
use Doctrine\DBAL\Platforms\SqlitePlatform ;
2017-07-20 20:05:44 +00:00
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
{
2022-12-14 13:36:29 +00:00
public function up ( Schema $schema ) : void
2017-07-20 20:05:44 +00:00
{
2023-08-05 17:35:09 +00:00
$this -> skipIf ( $this -> connection -> getDatabasePlatform () instanceof SqlitePlatform , '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
);
2022-12-14 13:36:29 +00:00
foreach ( $dupTags -> fetchAllAssociative () 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
2022-12-14 13:36:29 +00:00
$tags = $this -> connection -> query ( '
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 = [];
2022-12-14 13:36:29 +00:00
foreach ( $tags -> fetchAllAssociative () as $tag ) {
2017-07-20 20:05:44 +00: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 12:25:32 +00:00
if ( \count ( $ids ) > 0 ) {
2017-07-20 20:05:44 +00:00
// 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
);
}
2022-12-14 13:36:29 +00:00
public function down ( Schema $schema ) : void
2017-07-20 20:05:44 +00:00
{
throw new SkipMigrationException ( 'Too complex ...' );
}
}