Skip to content

Commit

Permalink
FEAT: Allow circular dependency resolution (#26)
Browse files Browse the repository at this point in the history
* FEAT: Allow one level of circular dependency resolution (where one fixture refers to another that hasn't been defined yet)

* - Don't loop over all objects and delete individually, as it needlessly creates versions for each deletion, and we TRUNCATE TABLE anyway
- Allow recursion over the list of fixtures to allow multiple attempts to create fixtures (still won't help with actual circular dependencies, but will help when A depends on B depends on C, C will get created in the first loop, then B gets created, then A gets created. Script will fail when two subsequent runs over failed fixtures doesn't change the number of fixtures that remain (e.g. we've deadlocked).

* Update PopulateFactory to correctly create / modify existing files

* Fixing mistake from rebase

* Try/catch/log File creation. Linting

Co-authored-by: Chris Penny <[email protected]>
  • Loading branch information
madmatt and chrispenny authored Oct 20, 2021
1 parent cdf58a5 commit 91af156
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 138 deletions.
15 changes: 14 additions & 1 deletion code/Populate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace DNADesign\Populate;

use Exception;
use SilverStripe\Assets\File;
use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\YamlFixture;
use SilverStripe\ORM\Connect\DatabaseException;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
Expand Down Expand Up @@ -76,7 +78,8 @@ public static function requireRecords($force = false): bool
throw new Exception('requireRecords can only be run in development or test environments');
}

$factory = Injector::inst()->create('DNADesign\Populate\PopulateFactory');
/** @var PopulateFactory $factory */
$factory = Injector::inst()->create(PopulateFactory::class);

foreach (self::config()->get('truncate_objects') as $className) {
self::truncateObject($className);
Expand All @@ -87,12 +90,15 @@ public static function requireRecords($force = false): bool
}

foreach (self::config()->get('include_yaml_fixtures') as $fixtureFile) {
DB::alteration_message(sprintf('Processing %s', $fixtureFile), 'created');
$fixture = new YamlFixture($fixtureFile);
$fixture->writeInto($factory);

$fixture = null;
}

$factory->processFailedFixtures();

$populate = Injector::inst()->create(Populate::class);
$populate->extend('onAfterPopulateRecords');

Expand All @@ -106,6 +112,13 @@ public static function requireRecords($force = false): bool
*/
private static function truncateObject(string $className): void
{
if (in_array($className, ClassInfo::subclassesFor(File::class))) {
foreach (DataList::create($className) as $obj) {
/** @var File $obj */
$obj->deleteFile();
}
}

$tables = [];

// All ancestors or children with tables
Expand Down
Loading

0 comments on commit 91af156

Please sign in to comment.