Skip to content

Commit

Permalink
Create migration to better auto upgrade to 0.5.0 (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jul 20, 2020
1 parent c570e63 commit 8336246
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ services:
Buddy\Repman\Service\Dist\Storage:
class: Buddy\Repman\Service\Dist\Storage\FileStorage

proxy.storage.public:
public: true
alias: 'proxy.storage'

Buddy\Repman\Service\Proxy\ProxyRegister:
public: true

repman.organization.dist_storage:
class: Buddy\Repman\Service\Dist\Storage\FileStorage
arguments:
Expand Down
77 changes: 77 additions & 0 deletions src/Migrations/Version20200720112146.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Migrations;

use Buddy\Repman\Service\Proxy;
use Buddy\Repman\Service\Proxy\ProxyRegister;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use League\Flysystem\Exception;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class Version20200720112146 extends AbstractMigration implements ContainerAwareInterface
{
private ContainerInterface $container;

public function setContainer(ContainerInterface $container = null): void
{
if ($container === null) {
throw new \InvalidArgumentException('Container is required');
}

$this->container = $container;
}

public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$filesystem = $this->container->get('proxy.storage.public');
$register = $this->container->get(ProxyRegister::class);

$register->all()->forEach(function (Proxy $proxy) use ($filesystem): void {
foreach ($filesystem->listContents(sprintf('%s', (string) parse_url($proxy->url(), PHP_URL_HOST)), true) as $file) {
if ($file['type'] !== 'file') {
continue;
}

// remove old metadata
if ($file['extension'] === 'json') {
$filesystem->delete($file['path']);
}

if (strpos($file['basename'], '_') === false) {
continue;
}

// rename old dist files to new format
$newName = $file['dirname'].DIRECTORY_SEPARATOR.substr($file['basename'], strpos($file['basename'], '_') + 1);
if ($filesystem->has($newName)) {
continue;
}

try {
$filesystem->rename($file['path'], $newName);
} catch (Exception $exception) {
$this->write(sprintf('Error when renaming %s: %s', $file['path'], $exception->getMessage()));
}
}
});
}

public function down(Schema $schema): void
{
// nothing to do here
}

public function isTransactional(): bool
{
return false;
}
}
5 changes: 5 additions & 0 deletions src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public function syncMetadata(): void
$this->downloader->run();
}

public function url(): string
{
return $this->url;
}

/**
* @param mixed[] $files
*/
Expand Down

0 comments on commit 8336246

Please sign in to comment.