Skip to content

Commit 5fc779f

Browse files
authored
Merge pull request #4 from vincentkedison/master
Add migration sequence to accomodate ->constrained() migration
2 parents 0668920 + a387754 commit 5fc779f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

resources/stubs/Model.php

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

1414
protected $guarded = [];
1515

16+
public $migration_sequence = 1;
17+
1618
public function migration(Blueprint $table)
1719
{
1820
$table->id();

resources/stubs/UserModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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;
2122

2223
public function migration(Blueprint $table)
2324
{

src/Commands/MigrateAutoCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ private function runAutomaticMigrations()
6464
$this->migrate($model);
6565
}
6666
}
67+
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']);
87+
}
6788
}
6889

6990
private function migrate($class)

0 commit comments

Comments
 (0)