Skip to content

Commit

Permalink
Add after migration callback (#108)
Browse files Browse the repository at this point in the history
* Add after migration callback

* tweak

---------

Co-authored-by: Caleb Porzio <calebporzio@gmail.com>
  • Loading branch information
Robertbaelde and calebporzio committed Jan 21, 2024
1 parent 8e4187d commit 808dff0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ class Products extends Model
}
```

## Advanced Usage
When you need more flexibility, you can implement the `runAfterMigrating(BluePrint $table)` method, allowing you to customize the table after it has been created. This might be useful for adding indexes to certain columns.

```php
class Products extends Model
{
use \Sushi\Sushi;

protected $rows = [
['name' => 'Lawn Mower', 'price' => '226.99'],
['name' => 'Leaf Blower', 'price' => '134.99'],
['name' => 'Rake', 'price' => '9.99'],
];

protected function runAfterMigrating(Blueprint $table)
{
$table->index('name');
}
}
```

## How It Works
Under the hood, this package creates and caches a SQLite database JUST for this model. It creates a table and populates the rows. If, for whatever reason, it can't cache a .sqlite file, it will default to using an in-memory sqlite database.

Expand Down
8 changes: 8 additions & 0 deletions src/Sushi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;

trait Sushi
Expand Down Expand Up @@ -164,9 +165,16 @@ public function createTable(string $tableName, $firstRow)
if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($firstRow)) || ! in_array('created_at', array_keys($firstRow)))) {
$table->timestamps();
}

$this->afterMigrate($table);
});
}

protected function afterMigrate(BluePrint $table)
{
//
}

public function createTableWithNoData(string $tableName)
{
$this->createTableSafely($tableName, function ($table) {
Expand Down
27 changes: 25 additions & 2 deletions tests/SushiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -182,8 +183,15 @@ function can_use_exists_validation_rule()
: $this->assertFalse(Validator::make(['bob' => 'ble'], ['bob' => 'exists:'.ModelWithNonStandardKeys::class.'.model_with_non_standard_keys'])->passes());
}

/**
* @test
/** @test */
public function it_runs_method_after_migration_when_defined()
{
$model = ModelWithAddedTableOperations::all();
$this->assertEquals('columnWasAdded', $model->first()->columnAdded, 'The runAfterMigrating method was not triggered.');
}

/**
* @test
* @define-env usesSqliteConnection
* */
function sushi_models_can_relate_to_models_in_regular_sqlite_databases()
Expand Down Expand Up @@ -272,6 +280,21 @@ class ModelWithCustomSchema extends Model
];
}

class ModelWithAddedTableOperations extends Model
{
use \Sushi\Sushi;

protected $rows = [[
'float' => 123.456,
'string' => 'foo',
]];

protected function afterMigrate(Blueprint $table)
{
$table->string('columnAdded')->default('columnWasAdded');
}
}

class Bar extends Model
{
use \Sushi\Sushi;
Expand Down

0 comments on commit 808dff0

Please sign in to comment.