diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md
index 392bbb4f1..edd8706ec 100644
--- a/CHANGELOG-2.0.md
+++ b/CHANGELOG-2.0.md
@@ -5,6 +5,7 @@
- [#2857](https://github.com/hyperf/hyperf/pull/2857) Support Consul ACL Token for Service Governance.
- [#2870](https://github.com/hyperf/hyperf/pull/2870) The publish option of `ConfigProvider` allows publish directory.
- [#2875](https://github.com/hyperf/hyperf/pull/2875) Added option `no-restart` for watcher.
+- [#2883](https://github.com/hyperf/hyperf/pull/2883) Added options `--chunk` and `--column|c` into command `scout:import`.
- [#2891](https://github.com/hyperf/hyperf/pull/2891) Added config file for crontab.
## Fixed
diff --git a/phpunit.xml b/phpunit.xml
index ffb219d9a..06cbacbcf 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -48,6 +48,7 @@
./src/retry/tests
./src/rpc/tests
./src/rpc-server/tests
+ ./src/scout/tests
./src/server/tests
./src/service-governance/tests
./src/session/tests
@@ -93,6 +94,7 @@
./src/paginator/src
./src/redis/src
./src/rpc/src
+ ./src/scout/src
./src/server/src
./src/service-governance/src
./src/session/src
diff --git a/src/database/src/Model/Builder.php b/src/database/src/Model/Builder.php
index e6916e4ba..13dd5837a 100755
--- a/src/database/src/Model/Builder.php
+++ b/src/database/src/Model/Builder.php
@@ -664,9 +664,9 @@ class Builder
*/
public function chunkById($count, callable $callback, $column = null, $alias = null)
{
- $column = is_null($column) ? $this->getModel()->getKeyName() : $column;
+ $column = $column ?? $this->getModel()->getKeyName();
- $alias = is_null($alias) ? $column : $alias;
+ $alias = $alias ?? $column;
$lastId = null;
diff --git a/src/scout/src/Console/ImportCommand.php b/src/scout/src/Console/ImportCommand.php
index ebd51c2d4..977fe2a98 100644
--- a/src/scout/src/Console/ImportCommand.php
+++ b/src/scout/src/Console/ImportCommand.php
@@ -16,6 +16,7 @@ use Hyperf\Scout\Event\ModelsImported;
use Hyperf\Utils\ApplicationContext;
use Psr\EventDispatcher\ListenerProviderInterface;
use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
class ImportCommand extends Command
{
@@ -40,16 +41,27 @@ class ImportCommand extends Command
{
define('SCOUT_COMMAND', true);
$class = $this->input->getArgument('model');
+ $chunk = (int) $this->input->getOption('chunk');
+ $column = (string) $this->input->getOption('column');
$model = new $class();
$provider = ApplicationContext::getContainer()->get(ListenerProviderInterface::class);
$provider->on(ModelsImported::class, function ($event) use ($class) {
+ /** @var ModelsImported $event */
$key = $event->models->last()->getScoutKey();
$this->line('Imported [' . $class . '] models up to ID: ' . $key);
});
- $model::makeAllSearchable();
+ $model::makeAllSearchable($chunk ?: null, $column ?: null);
$this->info('All [' . $class . '] records have been imported.');
}
+ protected function getOptions()
+ {
+ return [
+ ['column', 'c', InputOption::VALUE_OPTIONAL, 'Column used in chunking. (Default use primary key)'],
+ ['chunk', '', InputOption::VALUE_OPTIONAL, 'The number of records to import at a time (Defaults to configuration value: `scout.chunk.searchable`)'],
+ ];
+ }
+
protected function getArguments()
{
return [
diff --git a/src/scout/src/Searchable.php b/src/scout/src/Searchable.php
index 9209d6ca1..50d4c382f 100644
--- a/src/scout/src/Searchable.php
+++ b/src/scout/src/Searchable.php
@@ -111,7 +111,7 @@ trait Searchable
/**
* Make all instances of the model searchable.
*/
- public static function makeAllSearchable()
+ public static function makeAllSearchable(?int $chunk = null, ?string $column = null)
{
$self = new static();
$softDelete = static::usesSoftDelete() && config('scout.soft_delete', false);
@@ -119,8 +119,8 @@ trait Searchable
->when($softDelete, function ($query) {
$query->withTrashed();
})
- ->orderBy($self->getKeyName())
- ->searchable();
+ ->orderBy($column ?: $self->getKeyName())
+ ->searchable($chunk, $column);
}
/**
diff --git a/src/scout/src/SearchableScope.php b/src/scout/src/SearchableScope.php
index 87250107e..5601d7089 100644
--- a/src/scout/src/SearchableScope.php
+++ b/src/scout/src/SearchableScope.php
@@ -33,12 +33,16 @@ class SearchableScope implements Scope
*/
public function extend(EloquentBuilder $builder)
{
- $builder->macro('searchable', function (EloquentBuilder $builder, $chunk = null) {
- $builder->chunk($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
+ $builder->macro('searchable', function (EloquentBuilder $builder, $chunk = null, $column = null) {
+ $callback = function ($models) {
$models->filter->shouldBeSearchable()->searchable();
$dispatcher = ApplicationContext::getContainer()->get(EventDispatcherInterface::class);
$dispatcher->dispatch(new ModelsImported($models));
- });
+ };
+
+ $chunk = $chunk ?: config('scout.chunk.searchable', 500);
+
+ $column ? $builder->chunkById($chunk, $callback, $column) : $builder->chunk($chunk, $callback);
});
$builder->macro('unsearchable', function (EloquentBuilder $builder, $chunk = null) {
$builder->chunk($chunk ?: config('scout.chunk.unsearchable', 500), function ($models) {