Skip to content

Commit

Permalink
Allow specifying DB connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Flynsarmy committed Mar 27, 2020
1 parent aa0dc76 commit 432c6b8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Drop your CSV into */database/seeds/csvs/your_csv.csv* or whatever path you spec

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

- `connection` (string '') Connection to use for inserts. Leave empty for default connection.
- `insert_chunk_size` (int 500) An SQL insert statement will trigger every `insert_chunk_size` number of rows while reading the CSV
- `csv_delimiter` (string ,) The CSV field delimiter.
- `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.
Expand Down
15 changes: 10 additions & 5 deletions src/Flynsarmy/CsvSeeder/CsvSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ class CsvSeeder extends Seeder
*
* @var string
*/
public string $table;
public string $table = '';

/**
* CSV filename
*
* @var string
*/
public string $filename;
public string $filename = '';

/**
* DB connection to use. Leave empty for default connection
*/
public string $connection = '';

/**
* DB fields to be hashed before import, For example a password field.
Expand Down Expand Up @@ -63,8 +68,8 @@ class CsvSeeder extends Seeder
* created_at and updated_at values to be added to each row. Only used if
* $this->timestamps is true
*/
public string $created_at;
public string $updated_at;
public string $created_at = '';
public string $updated_at = '';

/**
* The mapping of CSV to DB column. If not specified manually, the first
Expand Down Expand Up @@ -300,7 +305,7 @@ public function readRow(array $row, array $mapping): array
public function insert(array $seedData): bool
{
try {
DB::table($this->table)->insert($seedData);
DB::connection($this->connection)->table($this->table)->insert($seedData);
} catch (\Exception $e) {
Log::error("CSV insert failed: " . $e->getMessage() . " - CSV " . $this->filename);
return false;
Expand Down
25 changes: 25 additions & 0 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ public function it_imports()
]);
}

/** @test */
public function it_uses_provided_connection()
{
$seeder = new \Flynsarmy\CsvSeeder\CsvSeeder;
$seeder->table = 'tests_users';

// Test default connection works
$seeder->insert(['id' => 1, 'first_name' => 'Aaron']);
$this->assertDatabaseHas('tests_users', [
'id' => 1,
'first_name' => 'Aaron',
]);

// Reset users table
\DB::table('tests_users')->truncate();

// Test inserting into a different connection
$seeder->connection = 'some_connection_that_doesnt_exist';
$seeder->insert(['id' => 1, 'first_name' => 'Aaron']);
$this->assertDatabaseMissing('tests_users', [
'id' => 1,
'first_name' => 'Aaron',
]);
}

/** @test */
public function it_ignores_columns_on_import()
{
Expand Down

0 comments on commit 432c6b8

Please sign in to comment.