Skip to content

Commit 432c6b8

Browse files
committed
Allow specifying DB connection
1 parent aa0dc76 commit 432c6b8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Drop your CSV into */database/seeds/csvs/your_csv.csv* or whatever path you spec
5151

5252
In addition to setting the database table and CSV filename, the following configuration options are available. They can be set in your class constructor:
5353

54+
- `connection` (string '') Connection to use for inserts. Leave empty for default connection.
5455
- `insert_chunk_size` (int 500) An SQL insert statement will trigger every `insert_chunk_size` number of rows while reading the CSV
5556
- `csv_delimiter` (string ,) The CSV field delimiter.
5657
- `hashable` (array [password]) List of fields to be hashed before import, useful if you are importing users and need their passwords hashed. Uses `Hash::make()`. Note: This is EXTREMELY SLOW. If you have a lot of rows in your CSV your import will take quite a long time.

src/Flynsarmy/CsvSeeder/CsvSeeder.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ class CsvSeeder extends Seeder
1919
*
2020
* @var string
2121
*/
22-
public string $table;
22+
public string $table = '';
2323

2424
/**
2525
* CSV filename
2626
*
2727
* @var string
2828
*/
29-
public string $filename;
29+
public string $filename = '';
30+
31+
/**
32+
* DB connection to use. Leave empty for default connection
33+
*/
34+
public string $connection = '';
3035

3136
/**
3237
* DB fields to be hashed before import, For example a password field.
@@ -63,8 +68,8 @@ class CsvSeeder extends Seeder
6368
* created_at and updated_at values to be added to each row. Only used if
6469
* $this->timestamps is true
6570
*/
66-
public string $created_at;
67-
public string $updated_at;
71+
public string $created_at = '';
72+
public string $updated_at = '';
6873

6974
/**
7075
* The mapping of CSV to DB column. If not specified manually, the first
@@ -300,7 +305,7 @@ public function readRow(array $row, array $mapping): array
300305
public function insert(array $seedData): bool
301306
{
302307
try {
303-
DB::table($this->table)->insert($seedData);
308+
DB::connection($this->connection)->table($this->table)->insert($seedData);
304309
} catch (\Exception $e) {
305310
Log::error("CSV insert failed: " . $e->getMessage() . " - CSV " . $this->filename);
306311
return false;

tests/CsvTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,31 @@ public function it_imports()
239239
]);
240240
}
241241

242+
/** @test */
243+
public function it_uses_provided_connection()
244+
{
245+
$seeder = new \Flynsarmy\CsvSeeder\CsvSeeder;
246+
$seeder->table = 'tests_users';
247+
248+
// Test default connection works
249+
$seeder->insert(['id' => 1, 'first_name' => 'Aaron']);
250+
$this->assertDatabaseHas('tests_users', [
251+
'id' => 1,
252+
'first_name' => 'Aaron',
253+
]);
254+
255+
// Reset users table
256+
\DB::table('tests_users')->truncate();
257+
258+
// Test inserting into a different connection
259+
$seeder->connection = 'some_connection_that_doesnt_exist';
260+
$seeder->insert(['id' => 1, 'first_name' => 'Aaron']);
261+
$this->assertDatabaseMissing('tests_users', [
262+
'id' => 1,
263+
'first_name' => 'Aaron',
264+
]);
265+
}
266+
242267
/** @test */
243268
public function it_ignores_columns_on_import()
244269
{

0 commit comments

Comments
 (0)