Skip to content

Commit

Permalink
Migrate array columns to json types
Browse files Browse the repository at this point in the history
To do the migration for local dev envs, run:

bin/console packagist:migrate-db
bin/console doctrine:schema:upd --force --complete
  • Loading branch information
Seldaek committed Nov 27, 2024
1 parent 5099cb5 commit 8b3b1aa
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 15 deletions.
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions src/Command/MigrateDataTypesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php declare(strict_types=1);

/*
* This file is part of Packagist.
*
* (c) Jordi Boggiano <[email protected]>
* Nils Adermann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Command;

use App\Entity\Dist;
use App\Entity\DistType;
use App\Entity\Version;
use App\Service\FallbackGitHubAuthProvider;
use Composer\Util\HttpDownloader;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Package;
use App\Package\V2Dumper;
use App\Service\Locker;
use Psr\Log\LoggerInterface;
use Seld\Signal\SignalHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpClient\Exception\TimeoutException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\NoPrivateNetworkHttpClient;
use ZipArchive;

class MigrateDataTypesCommand extends Command
{
use \App\Util\DoctrineTrait;

public function __construct(
private readonly ManagerRegistry $doctrine,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setName('packagist:migrate-db')
->setDescription('Migrate DB data types from array to json type, run this command once to migrate old data')
->setDefinition([
])
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
do {
$em = $this->getEM();
$versions = $em->getConnection()->fetchAllAssociative('SELECT id, extra FROM package_version WHERE extra LIKE "a%" LIMIT 5000');

$output->writeln(count($versions).' versions to update');
$em->getConnection()->beginTransaction();
foreach ($versions as $version) {
$extra = unserialize($version['extra']);
if ($extra === false) {
throw new \UnexpectedValueException('Could not parse '.$version['extra']);
}
$extra = json_encode($extra);
if ($extra === false) {
throw new \UnexpectedValueException('Failed json-encoding '.$version['extra']);
}
$em->getConnection()->update('package_version', ['extra' => $extra], ['id' => $version['id']]);
}
$em->getConnection()->commit();
} while (count($versions) > 0);

do {
$emptyRefs = $em->getConnection()->fetchAllAssociative('SELECT package_id, emptyReferences FROM empty_references WHERE emptyReferences LIKE "a%" LIMIT 1000');

$output->writeln(count($emptyRefs).' empty refs to update');
$em->getConnection()->beginTransaction();
foreach ($emptyRefs as $emptyRef) {
$emptyReferences = unserialize($emptyRef['emptyReferences']);
if ($emptyReferences === false) {
throw new \UnexpectedValueException('Could not parse '.$emptyRef['emptyReferences']);
}
$emptyReferences = json_encode($emptyReferences);
if ($emptyReferences === false) {
throw new \UnexpectedValueException('Failed json-encoding '.$emptyRef['emptyReferences']);
}
$em->getConnection()->update('empty_references', ['emptyReferences' => $emptyReferences], ['package_id' => $emptyRef['package_id']]);
}
$em->getConnection()->commit();
} while (count($emptyRefs) > 0);

do {
$users = $em->getConnection()->fetchAllAssociative('SELECT id, roles FROM fos_user WHERE roles LIKE "a%" LIMIT 1000');

$output->writeln(count($users).' users to update');
$em->getConnection()->beginTransaction();
foreach ($users as $user) {
$roles = unserialize($user['roles']);
if ($roles === false) {
throw new \UnexpectedValueException('Could not parse '.$user['roles']);
}
$roles = json_encode($roles);
if ($roles === false) {
throw new \UnexpectedValueException('Failed json-encoding '.$user['roles']);
}
$em->getConnection()->update('fos_user', ['roles' => $roles], ['id' => $user['id']]);
}
$em->getConnection()->commit();
} while (count($users) > 0);

return 0;
}
}
2 changes: 1 addition & 1 deletion src/Entity/EmptyReferenceCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EmptyReferenceCache
/**
* @var list<string>
*/
#[ORM\Column(type: 'array')]
#[ORM\Column(type: 'json')]
private array $emptyReferences = [];

public function __construct(Package $package)
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class User implements UserInterface, TwoFactorInterface, BackupCodeInterface, Eq
/**
* @var list<string>
*/
#[ORM\Column(type: 'array')]
#[ORM\Column(type: 'json')]
private array $roles = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Version
/**
* @var array<mixed>
*/
#[ORM\Column(type: 'array')]
#[ORM\Column(type: 'json')]
private array $extra = [];

/**
Expand Down

0 comments on commit 8b3b1aa

Please sign in to comment.