Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create migration for better auto upgrade to 0.5.0 #227

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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