From 808dff009d1ea02281b29080a0d142616568983b Mon Sep 17 00:00:00 2001 From: Robertbaelde Date: Sun, 21 Jan 2024 22:22:51 +0100 Subject: [PATCH] Add after migration callback (#108) * Add after migration callback * tweak --------- Co-authored-by: Caleb Porzio --- README.md | 21 +++++++++++++++++++++ src/Sushi.php | 8 ++++++++ tests/SushiTest.php | 27 +++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 56f9f5c..1c11852 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Sushi.php b/src/Sushi.php index 6c7fc95..410f657 100644 --- a/src/Sushi.php +++ b/src/Sushi.php @@ -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 @@ -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) { diff --git a/tests/SushiTest.php b/tests/SushiTest.php index 70d5975..6d16159 100644 --- a/tests/SushiTest.php +++ b/tests/SushiTest.php @@ -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; @@ -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() @@ -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;