Skip to content

Commit 3837b84

Browse files
committed
migration order
1 parent 5fc779f commit 3837b84

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

config/laravel-automatic-migrations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
|--------------------------------------------------------------------------
99
|
1010
| This value is the path to the stubs the package should use when executing
11-
| commands. To use your own stubs, make sure you vendor:publish the package
12-
| stubs and update this value to point to the resource path.
11+
| commands. To use your own stubs, vendor:publish the package stubs and set
12+
| this to: resource_path('stubs/vendor/laravel-automatic-migrations')
1313
|
1414
*/
1515

readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This package works fine alongside traditional Laravel migration files, for the c
1111
- [Commands](#commands)
1212
- [Making Models](#making-models)
1313
- [Running Migrations](#running-migrations)
14+
- [Migration Order](#migration-order)
1415
- [Publishing Stubs](#publishing-stubs)
1516
- [Traits](#traits)
1617
- [HasHashes](#hashashes)
@@ -73,6 +74,27 @@ php artisan migrate:auto {--f|--fresh} {--s|--seed} {--force}
7374

7475
Use `-f` to wipe the database, `-s` to seed after migration, and `--force` to run migrations in production.
7576

77+
## Migration Order
78+
79+
You can specify the order to run your model migrations by adding a public `migrationOrder` property to your models. This is useful for pivot tables or situations where you must create a certain table before another.
80+
81+
```php
82+
class MyModel extends Model
83+
{
84+
public $migrationOrder = 1;
85+
86+
public function migration(Blueprint $table)
87+
{
88+
$table->id();
89+
$table->string('name');
90+
$table->timestamp('created_at')->nullable();
91+
$table->timestamp('updated_at')->nullable();
92+
}
93+
}
94+
```
95+
96+
The `migrate:auto` command will run the automatic migrations in the order specified. If no order is declared for a model, it will default to `0`. Thanks to [@vincentkedison](https://github.com/vincentkedison) for this idea.
97+
7698
## Publishing Stubs
7799

78100
Use your own model and factory stubs by publishing package files:

resources/stubs/Model.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class DummyModelClass extends Model
1313

1414
protected $guarded = [];
1515

16-
public $migration_sequence = 1;
17-
1816
public function migration(Blueprint $table)
1917
{
2018
$table->id();

resources/stubs/UserModel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class DummyModelClass extends Authenticatable
1818
protected $hashes = ['password'];
1919
protected $hidden = ['password', 'remember_token'];
2020
protected $casts = ['email_verified_at' => 'datetime'];
21-
public $migration_sequence = 0;
2221

2322
public function migration(Blueprint $table)
2423
{

src/Commands/MigrateAutoCommand.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ private function runTraditionalMigrations()
4646

4747
private function runAutomaticMigrations()
4848
{
49-
$path = app_path('Models');
49+
$path = is_dir(app_path('Models')) ? app_path('Models') : app_path();
5050
$namespace = app()->getNamespace();
51+
$models = collect();
5152

5253
if (!is_dir($path)) {
5354
return;
@@ -61,35 +62,20 @@ private function runAutomaticMigrations()
6162
);
6263

6364
if (method_exists($model, 'migration')) {
64-
$this->migrate($model);
65+
$models->push([
66+
'object' => $object = app($model),
67+
'order' => $object->migrationOrder ?? 0,
68+
]);
6569
}
6670
}
6771

68-
$models_to_migrate = collect([]);
69-
70-
foreach ((new Finder)->in($path) as $model) {
71-
$model = $namespace . str_replace(
72-
['/', '.php'],
73-
['\\', ''],
74-
Str::after($model->getRealPath(), realpath(app_path()) . DIRECTORY_SEPARATOR)
75-
);
76-
77-
if (method_exists($model, 'migration')) {
78-
$model_object = app($model);
79-
$models_to_migrate[] = ['sequence' => $model_object->migration_sequence ?? 1, 'model' => $model];
80-
}
81-
}
82-
83-
$sorted_models_to_migrate = $models_to_migrate->sortBy('sequence');
84-
85-
foreach ($sorted_models_to_migrate as $model) {
86-
$this->migrate($model['model']);
72+
foreach ($models->sortBy('order') as $model) {
73+
$this->migrate($model['object']);
8774
}
8875
}
8976

90-
private function migrate($class)
77+
private function migrate($model)
9178
{
92-
$model = app($class);
9379
$modelTable = $model->getTable();
9480
$tempTable = 'table_' . $modelTable;
9581

@@ -120,14 +106,16 @@ private function migrate($class)
120106

121107
private function seed()
122108
{
123-
if ($this->option('seed')) {
124-
$command = 'db:seed';
109+
if (!$this->option('seed')) {
110+
return;
111+
}
125112

126-
if ($this->option('force')) {
127-
$command .= ' --force';
128-
}
113+
$command = 'db:seed';
129114

130-
Artisan::call($command, [], $this->getOutput());
115+
if ($this->option('force')) {
116+
$command .= ' --force';
131117
}
118+
119+
Artisan::call($command, [], $this->getOutput());
132120
}
133121
}

0 commit comments

Comments
 (0)