Index load keeps the index when it refuses an empty listing; counters of unchanged tables are repaired too, never moved backwards

This commit is contained in:
2026-10-21 09:08:50 +02:00
parent 85ea0acc50
commit 177361acae
2 changed files with 11 additions and 4 deletions
+8 -2
View File
@@ -216,7 +216,8 @@ final class DbDumper {
} }
$report['restored'][] = $table; $report['restored'][] = $table;
} }
$this->restoreSequences($manifest['sequences'] ?? [], $report['missing'], $report['restored']); // unchanged tables too: a restore that stopped earlier may have left their counters at 1
$this->restoreSequences($manifest['sequences'] ?? [], $report['missing'], array_merge($report['restored'], $report['skipped']));
$this->resumeForeignKeys($foreignKeys); $this->resumeForeignKeys($foreignKeys);
$this->db->commit(); $this->db->commit();
// Foreign keys of the tables created above, now their rows are in. Not inside the // Foreign keys of the tables created above, now their rows are in. Not inside the
@@ -575,7 +576,7 @@ final class DbDumper {
/** /**
* @param list<string> $leftOut tables not restored (--skip-missing-tables): they do not exist here * @param list<string> $leftOut tables not restored (--skip-missing-tables): they do not exist here
* @param list<string> $restored tables restored: their other counters go after their data * @param list<string> $restored tables restored or unchanged: their other counters go after their data
*/ */
private function restoreSequences(array $sequences, array $leftOut = [], array $restored = []): void { private function restoreSequences(array $sequences, array $leftOut = [], array $restored = []): void {
if (!$this->db->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) { if (!$this->db->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform) {
@@ -616,7 +617,12 @@ final class DbDumper {
} }
$max = (int)$this->db->fetchOne(sprintf('SELECT COALESCE(MAX(%s), 0) FROM %s', $max = (int)$this->db->fetchOne(sprintf('SELECT COALESCE(MAX(%s), 0) FROM %s',
$this->db->quoteIdentifier($r['column_name']), $this->db->quoteIdentifier($r['table_name']))); $this->db->quoteIdentifier($r['column_name']), $this->db->quoteIdentifier($r['table_name'])));
// never backwards: a counter already past the data stays where it is
$state = $this->db->fetchAssociative('SELECT last_value, is_called FROM ' . self::sequenceIdentifier($r['seq']));
$next = $state === false ? 1 : (int)$state['last_value'] + ($state['is_called'] ? 1 : 0);
if ($next <= $max) {
$this->db->executeQuery('SELECT setval(CAST(? AS regclass), ?, false)', [$r['seq'], $max + 1]); $this->db->executeQuery('SELECT setval(CAST(? AS regclass), ?, false)', [$r['seq'], $max + 1]);
} }
} }
}
} }
+2 -1
View File
@@ -94,7 +94,6 @@ final class BlobIndex {
*/ */
public function load(bool $verify = false, ?callable $progress = null): void { public function load(bool $verify = false, ?callable $progress = null): void {
$hadIndex = $this->blobs !== []; $hadIndex = $this->blobs !== [];
$this->blobs = [];
$cached = $verify ? [] : ($this->cache?->all($this->repositoryId) ?? []); $cached = $verify ? [] : ($this->cache?->all($this->repositoryId) ?? []);
$present = []; $present = [];
$this->downloaded = 0; $this->downloaded = 0;
@@ -114,6 +113,8 @@ final class BlobIndex {
if ($present === [] && ($hadIndex || $cached !== [] || ($verify && ($this->cache?->all($this->repositoryId) ?? []) !== []))) { if ($present === [] && ($hadIndex || $cached !== [] || ($verify && ($this->cache?->all($this->repositoryId) ?? []) !== []))) {
throw new BackendException('The location listed no index files, while some are known: not trusting that listing'); throw new BackendException('The location listed no index files, while some are known: not trusting that listing');
} }
// only now: an empty listing that was refused above leaves the index as it was
$this->blobs = [];
$this->loaded = $present; $this->loaded = $present;
// The rest is downloaded several at a time where the location allows it: on a large // The rest is downloaded several at a time where the location allows it: on a large
// repository these are tens of thousands of small files. getMany() keeps their order, so // repository these are tens of thousands of small files. getMany() keeps their order, so