Files
investbrain/database/seeders/MarketDataSeeder.php
T

94 lines
2.2 KiB
PHP
Raw Normal View History

2024-08-24 22:19:40 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-24 22:19:40 -05:00
namespace Database\Seeders;
2025-01-28 17:14:49 -06:00
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
2024-08-24 22:19:40 -05:00
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class MarketDataSeeder extends Seeder
{
use WithoutModelEvents;
public array $rows = [];
2024-08-24 22:19:40 -05:00
/**
* Run the database seeds.
*/
public function run(): void
2025-01-28 17:14:49 -06:00
{
2024-08-24 22:19:40 -05:00
$chunkSize = 500;
// Path to the CSV file
$csvFilePath = realpath(__DIR__.'/market_data_seed.csv');
2024-08-24 22:19:40 -05:00
// Open the file in read mode
if (($handle = fopen($csvFilePath, 'r')) !== false) {
$header = null;
$rowCount = 0;
while (($row = fgetcsv($handle, 0, ',')) !== false) {
2025-01-28 17:14:49 -06:00
if (! $header) {
2024-08-24 22:19:40 -05:00
// header must be the first row
$header = $row;
} else {
$data = array_combine($header, $row);
$this->rows[] = [
'symbol' => $data['symbol'],
'name' => $data['name'],
'meta_data' => json_encode([
'country' => $data['country'],
'first_trade_year' => $data['first_trade_year'],
'sector' => $data['sector'],
'industry' => $data['industry'],
]),
];
$rowCount++;
if ($rowCount % $chunkSize == 0) {
$this->bulkInsert($this->rows);
2024-08-24 22:19:40 -05:00
}
}
}
// final clean up
if (! empty($this->rows)) {
2025-03-07 19:15:10 -06:00
$this->bulkInsert($this->rows);
2024-08-24 22:19:40 -05:00
}
// Close the CSV file
fclose($handle);
2025-03-07 17:45:54 -06:00
echo "\n > Imported $rowCount market data items successfully!";
2024-08-24 22:19:40 -05:00
} else {
echo "Failed to open the CSV.\n";
}
}
public function bulkInsert(array $rows)
{
try {
DB::table('market_data')->insertOrIgnore($rows);
$this->rows = [];
} catch (\Throwable $e) {
throw new \Exception('Error: '.$e->getMessage());
}
}
2024-08-24 22:19:40 -05:00
}