瀏覽代碼

Merge pull request #1579 from monkeyless/dev

Fix "import:cities" command
daniel 6 年之前
父節點
當前提交
6e8f3ed5e5
共有 2 個文件被更改,包括 24 次插入24 次删除
  1. 23 23
      app/Console/Commands/ImportCities.php
  2. 1 1
      contrib/docker/php.ini

+ 23 - 23
app/Console/Commands/ImportCities.php

@@ -1,5 +1,4 @@
 <?php
-
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
@@ -14,15 +13,13 @@ class ImportCities extends Command
      *
      * @var string
      */
-    protected $signature = 'import:cities';
-
+    protected $signature = 'import:cities {chunk=1000}';
     /**
      * The console command description.
      *
      * @var string
      */
     protected $description = 'Import Cities to database';
-
     /**
      * Create a new command instance.
      *
@@ -32,7 +29,6 @@ class ImportCities extends Command
     {
         parent::__construct();
     }
-
     /**
      * Execute the console command.
      *
@@ -41,38 +37,42 @@ class ImportCities extends Command
     public function handle()
     {
         $path = storage_path('app/cities.json');
-        if(!is_file($path)) {
+        if (!is_file($path)) {
             $this->error('Missing storage/app/cities.json file!');
             return;
         }
-
-        if(Place::count() > 10) {
+        if (Place::count() > 10) {
             $this->error('Cities already imported, aborting operation...');
             return;
         }
         $this->info('Importing city data into database ...');
-
         $cities = file_get_contents($path);
         $cities = json_decode($cities);
-        $count = count($cities);
-        $this->info("Found {$count} cities to insert ...");
-        $bar = $this->output->createProgressBar($count);
+        $cityCount = count($cities);
+        $this->info("Found {$cityCount} cities to insert ...");
+        $bar = $this->output->createProgressBar($cityCount);
         $bar->start();
-
+        $buffer = [];
+        $count = 0;
         foreach ($cities as $city) {
             $country = $city->country == 'XK' ? 'Kosovo' : (new \League\ISO3166\ISO3166)->alpha2($city->country)['name'];
-            DB::transaction(function () use ($city, $country) {
-                $place = new Place();
-                $place->name = $city->name;
-                $place->slug = Str::slug($city->name);
-                $place->country = $country;
-                $place->lat = $city->lat;
-                $place->long = $city->lng;
-                $place->save();
-            });
-            $bar->advance();
+            $buffer[] = ["name" => $city->name, "slug" => Str::slug($city->name), "country" => $country, "lat" => $city->lat, "long" => $city->lng];
+            $count++;
+            if ($count % $this->argument('chunk') == 0) {
+                $this->insertBuffer($buffer, $count);
+                $bar->advance(count($buffer));
+                $buffer = [];
+            }
         }
+        $this->insertBuffer($buffer, $count);
+        $bar->advance(count($buffer));
         $bar->finish();
+        $this->info('Successfully imported ' . $count . ' entries.');
         return;
     }
+
+    private function insertBuffer($buffer, $count)
+    {
+        DB::table('places')->insert($buffer);
+    }
 }

+ 1 - 1
contrib/docker/php.ini

@@ -1,5 +1,5 @@
 file_uploads = On
-memory_limit = 64M
+memory_limit = 128M
 upload_max_filesize = 64M
 post_max_size = 64M
 max_execution_time = 600