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

Keep provider group naming working across years #384

Merged
merged 4 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Deactivate individual provider groups too
This should ensure command outputs are consistent with `root` data and avoid
any more serious unforeseen issues from the different references to groups going
out of sync
  • Loading branch information
NoelLH committed Feb 3, 2021
commit 927c767813371e156f03cd5248457d530a3ca93d
3 changes: 1 addition & 2 deletions src/Entity/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ public function updateProviderGroups(): void
->where('p.providerGroup = \'old\'')
->andWhere('p.lastCommitted >= :date')
->getQuery();
foreach ($groups as $key=>$date) {
foreach ($groups as $key => $date) {
$query->execute(['group' => $key, 'date' => $date->format('Y-m-d')]);
}

}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Service/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,25 @@ public function updateProviderGroup(string $providerGroupName, array $packageNam
$this->storage->saveProvider("providers-$providerGroupName", $providersSha256, $providerDataJson);
}

public function updateRoot()
public function updateRoot(): void
{
$providers = $this->storage->loadAllProviders();
$includes = [];
$providerFormat = 'p/%package%$%hash%.json';
foreach ($providers as $name => $value) {
$sha256 = hash('sha256', $value);

// Skip/delete 3-monthly providers for any year that's not the current one,
// allowing grouping to smoothly switch over at the start of a new year.
// Packages themselves automatically update provider group e.g. from '2020-12'
// to '2020' when the next year starts.
// to '2020' when the next year starts. `continue`ing here implicitly removes
// items from `root` while `deactivateProvider()` deletes or deactivates the
// individual provider.
if ($this->providerIsOutdated($name)) {
$this->storage->deactivateProvider($name, $sha256);
continue;
}

$sha256 = hash('sha256', $value);
$includes[str_replace('%package%', $name, $providerFormat)] = ['sha256' => $sha256];
}

Expand Down
31 changes: 27 additions & 4 deletions src/Storage/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function loadRoot(): ?string
return $this->loadEntity(self::TYPE_ROOT, '', '');
}

public function loadLatestEntities($type, $names = null)
public function loadLatestEntities($type, $names = null): array
{
// we're not interested in the models, only the keyed values, so don't use the repository
$qb = new QueryBuilder($this->entityManager);
Expand All @@ -83,12 +83,12 @@ public function loadLatestEntities($type, $names = null)
return $values;
}

public function loadAllPackages($packageNames)
public function loadAllPackages($packageNames): array
{
return $this->loadLatestEntities(self::TYPE_PACKAGE, $packageNames);
}

public function loadAllProviders()
public function loadAllProviders(): array
{
return $this->loadLatestEntities(self::TYPE_PROVIDER);
}
Expand Down Expand Up @@ -140,12 +140,35 @@ public function savePackage(string $packageName, string $hash, string $json): bo
return $this->saveEntity(self::TYPE_PACKAGE, $packageName, $hash, $json);
}


public function saveProvider(string $name, string $hash, string $json): bool
{
return $this->saveEntity(self::TYPE_PROVIDER, $name, $hash, $json);
}

/**
* Just sets `isLatest` false on the provider for now. Note that a 'final' `persist()` will
* later hard delete these providers in a full update.
*
* @inheritDoc
*
* @see Database::persist()
*/
public function deactivateProvider(string $name, string $hash): void
{
$qb = new QueryBuilder($this->entityManager);
$qb->update(PackageData::class, 'p')
->set('p.isLatest', ':newIsLatest')
->where('p.isLatest = true')
->andWhere('p.type = :type')
->andWhere('p.name = :name')
->getQuery()
->execute([
'newIsLatest' => false,
'type' => 'provider',
'name' => $name,
]);
}

public function saveRoot(string $json): bool
{
return $this->saveEntity(self::TYPE_ROOT, '', '', $json);
Expand Down
14 changes: 10 additions & 4 deletions src/Storage/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function savePackage(string $packageName, string $hash, string $json): bo
return $this->writeFile($this->getResourcePath($packageName, $hash), $json);
}

public function loadAllPackages($packageNames)
public function loadAllPackages($packageNames): array
{
$json = [];
$toFind = [];
Expand All @@ -91,7 +91,7 @@ public function loadAllPackages($packageNames)
return $json;
}

public function loadAllProviders()
public function loadAllProviders(): array
{
$finder = new Finder();
$finder->files()->in("{$this->basePath}/{$this->packageDir}")->depth(0);
Expand All @@ -115,6 +115,14 @@ public function saveProvider(string $name, string $hash, string $json): bool
return $this->writeFile($this->getResourcePath($name, $hash), $json);
}

public function deactivateProvider(string $name, string $hash): void
{
$path = $this->getResourcePath($name, $hash);
if (file_exists($path)) {
unlink($path);
}
}

public function loadRoot(): ?string
{
return $this->readFile("{$this->basePath}/packages.json");
Expand All @@ -135,6 +143,4 @@ public function persist($final = false): void
$this->packageDir = 'p';
}
}


}
12 changes: 10 additions & 2 deletions src/Storage/PackageStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ abstract public function loadRoot(): ?string;
* @param string[] $packageNames
* @return string[]
*/
abstract public function loadAllPackages(array $packageNames);
abstract public function loadAllPackages(array $packageNames): array;

/**
* @return string[]
*/
abstract public function loadAllProviders();
abstract public function loadAllProviders(): array;

/**
* @param string $packageName
Expand All @@ -50,6 +50,14 @@ abstract public function savePackage(string $packageName, string $hash, string $
*/
abstract public function saveProvider(string $name, string $hash, string $json): bool;

/**
* Mark a provider group as non-latest or delete it.
*
* @param string $name Provider group name
* @param string $hash SHA-256 content hash
*/
abstract public function deactivateProvider(string $name, string $hash): void;

/**
* @param string $json
* @return bool Success or failure.
Expand Down