Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add after migration callback #108

Merged
merged 3 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading