Skip to content

Commit

Permalink
add db importer
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed May 2, 2024
1 parent a8106e1 commit 0fa5add
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions app/Console/Commands/DbImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Psl\File;
use Psl\Json;
use Psl\Vec;
use Symfony\Component\Finder\Finder;

class DbImport extends Command
{
protected $signature = 'db:import {path}';

public function handle(): void
{
$files = (new Finder())->in($this->argument('path'))->files();

foreach ($files as $file) {
$table = $file->getBasename('.json');
/** @phpstan-ignore-next-line */
$data = Json\decode(File\read($file->getPathname()));

$this->info("Importing {$table} table...");

$this->withProgressBar(
/** @phpstan-ignore-next-line */
Vec\chunk($data, 200),
fn (array $d) => DB::table($table)->insert($d),
);

$this->newLine();
}
}
}
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => true,
'foreign_key_constraints' => false,
],

'testing' => [
Expand Down

0 comments on commit 0fa5add

Please sign in to comment.