From 432c6b83b155cbec685aaf70e92aec141113fcb5 Mon Sep 17 00:00:00 2001 From: flynsarmy Date: Fri, 27 Mar 2020 13:59:27 +1000 Subject: [PATCH] Allow specifying DB connection --- readme.md | 1 + src/Flynsarmy/CsvSeeder/CsvSeeder.php | 15 ++++++++++----- tests/CsvTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 70bfdc7..ee02a9b 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/Flynsarmy/CsvSeeder/CsvSeeder.php b/src/Flynsarmy/CsvSeeder/CsvSeeder.php index af7bd0b..7604a17 100644 --- a/src/Flynsarmy/CsvSeeder/CsvSeeder.php +++ b/src/Flynsarmy/CsvSeeder/CsvSeeder.php @@ -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. @@ -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 @@ -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; diff --git a/tests/CsvTest.php b/tests/CsvTest.php index 61880ff..62de246 100644 --- a/tests/CsvTest.php +++ b/tests/CsvTest.php @@ -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() {