diff --git a/src/Fogger/Data/ChunkProducer.php b/src/Fogger/Data/ChunkProducer.php index 257bf9e..2413fd4 100644 --- a/src/Fogger/Data/ChunkProducer.php +++ b/src/Fogger/Data/ChunkProducer.php @@ -36,19 +36,31 @@ private function queueTableChunks(Table $table) return; } - - $result = $this->sourceQuery->getAllKeysQuery($table)->execute(); + + $query = $this->sourceQuery->getAllKeysQuery($table); + $query = $query->orderBy($table->getSortBy()); + $result = $query->execute(); $counter = 0; $keys = []; + $last_key = null; - while ($key = $result->fetchColumn()) { + while (($key = $result->fetchColumn()) !== False) { $keys[] = $key; $counter++; if (0 === $counter % $table->getChunkSize()) { + + do { + $last_key = $key; + $key = $result->fetchColumn(); + $counter++; + } while ($last_key === $key); + $this->chunkCache->pushMessage($table, $keys); $keys = []; + $keys[] = $key; } + $last_key = $key; } if (0 !== $counter % $table->getChunkSize()) { $this->chunkCache->pushMessage($table, $keys); diff --git a/src/Fogger/Recipe/RecipeTableFactory.php b/src/Fogger/Recipe/RecipeTableFactory.php index ff3bdce..a6b171b 100644 --- a/src/Fogger/Recipe/RecipeTableFactory.php +++ b/src/Fogger/Recipe/RecipeTableFactory.php @@ -32,6 +32,18 @@ private function findSortBy(DBAL\Table $table): ?string return $index->getColumns()[0]; } } + if ($table->getPrimaryKey()) { + return $table->getPrimaryKeyColumns()[0]; + } + foreach ($table->getIndexes() as $index) { + if ($index->isUnique()) { + foreach ($index->getColumns() as $column) { + if ($table->getColumn($column)->getNotnull()) { + return $column; + } + } + } + } return null; }