diff --git a/src/CsvSeeder.php b/src/CsvSeeder.php index 3346ebf..63509f9 100644 --- a/src/CsvSeeder.php +++ b/src/CsvSeeder.php @@ -232,7 +232,7 @@ public function createMappingFromRow(array $row): array // skip csv columns that don't exist in the database foreach ($mapping as $index => $fieldname) { - if (!DB::getSchemaBuilder()->hasColumn($this->table, $fieldname)) { + if (!DB::connection($this->connection)->getSchemaBuilder()->hasColumn($this->table, $fieldname)) { if (isset($mapping[$index])) { unset($mapping[$index]); } diff --git a/tests/CsvTest.php b/tests/CsvTest.php index 7c139ec..8267cbe 100644 --- a/tests/CsvTest.php +++ b/tests/CsvTest.php @@ -36,6 +36,11 @@ protected function getEnvironmentSetUp($app) 'database' => ':memory:', 'prefix' => '', ]); + $app['config']->set('database.connections.csvSeederTest2', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); } /** @test */ @@ -436,4 +441,34 @@ public function it_offsets() 'age' => 54 ]); } + /** @test */ + public function it_imports_with_non_default_connection() + { + $seeder = new \Flynsarmy\CsvSeeder\CsvSeeder(); + $seeder->table = 'tests_users2'; + $seeder->filename = __DIR__ . '/csvs/users.csv'; + $seeder->connection = 'csvSeederTest2'; + $seeder->hashable = []; + $seeder->run(); + + // Make sure the rows imported + $this->assertDatabaseHas('tests_users2', [ + 'id' => 1, + 'first_name' => 'Abe', + 'last_name' => 'Abeson', + 'email' => 'abe.abeson@foo.com', + 'age' => 50, + 'created_at' => null, + 'updated_at' => null, + ], 'csvSeederTest2'); + $this->assertDatabaseHas('tests_users2', [ + 'id' => 3, + 'first_name' => 'Charly', + 'last_name' => 'Charlyson', + 'email' => 'charly.charlyson@foo.com', + 'age' => 52, + 'created_at' => null, + 'updated_at' => null, + ], 'csvSeederTest2'); + } } diff --git a/tests/migrations/2021_02_08_000000_create_second_tests_users_table.php b/tests/migrations/2021_02_08_000000_create_second_tests_users_table.php new file mode 100644 index 0000000..4846c0a --- /dev/null +++ b/tests/migrations/2021_02_08_000000_create_second_tests_users_table.php @@ -0,0 +1,34 @@ +create('tests_users2', function ($table) { + $table->increments('id'); + $table->string('first_name')->default(''); + $table->string('last_name')->default(''); + $table->string('email')->default(''); + $table->string('password')->default(''); + $table->string('address')->default(''); + $table->integer('age')->default(0); + $table->timestamps(); + }); + } + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::connection('csvSeederTest2')->drop('tests_users2'); + } +}