Skip to content

Commit 2f58b1d

Browse files
committed
Added validateMigrationDependencies helper method to BaseSeed
1 parent 6ba90a6 commit 2f58b1d

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- `sprinkle:list` bakery command
1212
- `NoCache` middleware to prevent caching of routes with dynamic content
1313
- Sample test environment for Docker
14+
- Added `validateMigrationDependencies` helper method to `BaseSeed`
1415

1516
### Changed
1617
- Sprinkle list in the bakery `debug` command to uses the new `sprinkle:list` table

app/sprinkles/core/src/Database/Seeder/BaseSeed.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ public function __construct(ContainerInterface $ci)
3434
$this->ci = $ci;
3535
}
3636

37+
/**
38+
* Validate if a specific set of migrations have been ran
39+
*
40+
* @param string|array $migrations List of migraiton or specific migration required
41+
* @throws \Exception If dependent migration is not available
42+
* @return bool True on success
43+
*/
44+
protected function validateMigrationDependencies($migrations)
45+
{
46+
if (!is_array($migrations)) {
47+
$migrations = [$migrations];
48+
}
49+
50+
/** @var \UserFrosting\Sprinkle\Core\Database\Migrator\Migrator; */
51+
$migrator = $this->ci->migrator;
52+
53+
// Get ran migrations list
54+
$ranMigrations = $migrator->getRepository()->getMigrationsList();
55+
56+
// Make sure required migrations are in the ran list. Throw exception if it isn't.
57+
foreach ($migrations as $migration) {
58+
if (!in_array($migration, $ranMigrations)) {
59+
throw new \Exception("Migration `$migration` doesn't appear to have been run!");
60+
}
61+
}
62+
63+
return true;
64+
}
65+
3766
/**
3867
* Function used to execute the seed
3968
*/

0 commit comments

Comments
 (0)