Skip to content

Commit

Permalink
Migrate order field
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Aug 22, 2022
1 parent 4d50d15 commit 47f3d6d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
136 changes: 136 additions & 0 deletions src/Migration/OrderFieldMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace MadeYourDay\RockSolidSlider\Migration;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Contao\StringUtil;
use Doctrine\DBAL\Connection;

/**
* @internal
*/
class OrderFieldMigration extends AbstractMigration
{
private const ORDER_FIELDS = [
'tl_rocksolid_slide' => [
'videosOrder' => 'videos',
'backgroundVideosOrder' => 'backgroundVideos',
],
'tl_rocksolid_slider' => [
'orderSRC' => 'multiSRC',
],
];

private Connection $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

public function shouldRun(): bool
{
$schemaManager = $this->connection->createSchemaManager();

foreach (self::ORDER_FIELDS as $table => $fields) {
if (!$schemaManager->tablesExist($table)) {
continue;
}

$columns = $schemaManager->listTableColumns($table);

foreach ($fields as $orderField => $field) {
if (isset($columns[strtolower($orderField)], $columns[strtolower($field)])) {
return true;
}
}
}

return false;
}

public function run(): MigrationResult
{
$schemaManager = $this->connection->createSchemaManager();

foreach (self::ORDER_FIELDS as $table => $fields) {
if (!$schemaManager->tablesExist($table)) {
continue;
}

$columns = $schemaManager->listTableColumns($table);

foreach ($fields as $orderField => $field) {
if (isset($columns[strtolower($orderField)], $columns[strtolower($field)])) {
$this->migrateOrderField($table, $orderField, $field);
}
}
}

return $this->createResult(true);
}

private function migrateOrderField(string $table, string $orderField, string $field): void
{
$tableQuoted = $this->connection->quoteIdentifier($table);
$orderFieldQuoted = $this->connection->quoteIdentifier($orderField);
$fieldQuoted = $this->connection->quoteIdentifier($field);

$rows = $this->connection->fetchAllAssociative("
SELECT
$orderFieldQuoted, $fieldQuoted
FROM
$tableQuoted
WHERE
$orderFieldQuoted IS NOT NULL
AND $orderFieldQuoted != ''
AND $fieldQuoted IS NOT NULL
AND $fieldQuoted != ''
");

foreach ($rows as $row) {
$items = StringUtil::deserialize($row[$field], true);
$order = array_map(static function (): void {}, array_flip(StringUtil::deserialize($row[$orderField], true)));

foreach ($items as $key => $value) {
if (\array_key_exists($value, $order)) {
$order[$value] = $value;
unset($items[$key]);
}
}

$items = array_merge(array_values(array_filter($order)), array_values($items));

$this->connection
->prepare("
UPDATE
$tableQuoted
SET
$fieldQuoted = :items,
$orderFieldQuoted = ''
WHERE
$fieldQuoted = :field
AND $orderFieldQuoted = :orderField
")
->executeStatement([
':items' => serialize($items),
':field' => $row[$field],
':orderField' => $row[$orderField],
])
;
}

$this->connection->executeStatement("ALTER TABLE $tableQuoted DROP $orderFieldQuoted");
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ services:
arguments:
- '@database_connection'
- '@contao.framework'

MadeYourDay\RockSolidSlider\Migration\OrderFieldMigration:
arguments:
- '@database_connection'

0 comments on commit 47f3d6d

Please sign in to comment.