Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Feb 4, 2024
1 parent b27ef2d commit b0fb1e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ public function migrate()
$this->createTableWithNoData($tableName);
}

if (count($this->casts) === 0) {
if (count($this->casts) > 0) {
foreach ($rows as $row) {
// If $casts are present, use Eloquent's "create" instead of a plain insert so they are used and applied...
static::forceCreate($row);
}
} else {
foreach (array_chunk($rows, $this->getSushiInsertChunkSize()) ?? [] as $inserts) {
if (!empty($inserts)) {
if (! empty($inserts)) {
static::insert($inserts);
}
}
} else { //casts are necessary, create each model singly so Eloquent can cast attributes as necessary
foreach ($rows as $row) {
static::create($row);
}
}

}

public function createTable(string $tableName, $firstRow)
Expand Down
24 changes: 24 additions & 0 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ function columns_with_varying_types()
$this->assertEquals(null, $row->null);
}

/** @test */
function model_with_casts()
{
$model = ModelWithCasts::first();

$this->assertTrue(is_array($model->is_array));
$this->assertTrue(is_bool($model->is_boolean));
}

/** @test */
function model_with_custom_schema()
{
Expand Down Expand Up @@ -373,3 +382,18 @@ public function maki()
return $this->belongsTo(Maki::class);
}
}


class ModelWithCasts extends Model
{
use \Sushi\Sushi;

protected $casts = [
'is_array' => 'array',
'is_boolean' => 'boolean',
];

protected $rows = [
['is_array' => [1, 2, 3], 'is_boolean' => true],
];
}

0 comments on commit b0fb1e2

Please sign in to comment.