/** * {@inheritdoc} */ protected function migrateAggregateRoot($aggregateClassName, EventStream $eventStream, Version $aggregateVersion, Version $applicationVersion) { parent::migrateAggregateRoot($aggregateClassName, $eventStream, $aggregateVersion, $applicationVersion); // reconstruct the new aggregate root $aggregateRoot = call_user_func(array($aggregateClassName, 'loadFromHistory'), $eventStream); // create the snapshot $snapshot = new Snapshot($this->streamName($aggregateClassName), $aggregateRoot, new \DateTime()); // pserist the new snapshot $this->snapshotStore->persist($snapshot, $applicationVersion); }
/** * @param MigrationsStatusCommand $command */ public function migrationsStatus(MigrationsStatusCommand $command) { try { $status = $this->migrator->status(); $latestMigration = $status->latestMigration(); $rows = array(array(' Current Version', $latestMigration ? sprintf('<c2>%s (%s)</c2>', $latestMigration->version()->__toString(), $latestMigration->createdAt()->format('Y-m-d H:i:s')) : '<c2>0</c2>'), array(' Latest Version', $status->latestAvailableVersion() ? '<c2>' . $status->latestAvailableVersion()->__toString() . '</c2>' : '<c2>none</c2>'), array(' Next Version', $status->nextAvailableVersion() ? '<c2>' . $status->nextAvailableVersion()->__toString() . '</c2>' : '<c2>none</c2>'), array(' Executed Migrations', '<c2>' . $status->numExecutedMigrations() . '</c2>'), array(' Available Migrations', '<c2>' . $status->numAvailableMigrations() . '</c2>'), array(' New Migrations', '<c2>' . $status->numNewMigrations() . '</c2>')); $table = new Table(TableStyle::borderless()); foreach ($rows as $row) { $table->addRow($row); } $table->render($command->getIo()); } catch (\Exception $e) { $command->getIo()->writeLine('<error>' . $e->getMessage() . '</error>'); } }
/** * Test migrate method. */ public function testMigrate() { $this->migrationsDirectory = __DIR__ . '/../../Fixtures/Migrations'; require_once __DIR__ . '/../../Fixtures/BlogEventSourced.php'; // simulate an application and aggregate version state $currentApplicationVersion = Version::fromString('0.1.0'); $aggregateVersion = Version::fromString('0.1.0'); VersionManager::setCurrentApplicationVersion($currentApplicationVersion); VersionManager::persistVersionOfClass(PostEventSourced::class, $aggregateVersion); VersionManager::persistVersionOfClass(\BlogEventSourced::class, $aggregateVersion); // creating migration store $aggregates = [PostEventSourced::class, \BlogEventSourced::class]; $migratorStore = new InMemoryMigrationStore(); $migratorStore->persist(new Migration($aggregates, $aggregateVersion, new \DateTime())); // creating event store $eventStore = new InMemoryEventStore(); // add the event store flow $postId1 = PostId::fromNative(md5(rand())); $postEventStream1 = new EventStream($this->streamName(PostEventSourced::class), $postId1, [new PostWasCreated($postId1, 'Best restaurants in barcelona', 'empty'), new PostTitleWasChanged($postId1, 'Best cuban restaurants in barcelona')]); $postId2 = PostId::fromNative(md5(rand())); $postEventStream2 = new EventStream($this->streamName(PostEventSourced::class), $postId2, [new PostWasCreated($postId2, 'Best things to do with children in barcelona', 'empty'), new PostTitleWasChanged($postId2, 'Things to do with children in barcelona this weekend')]); $eventStore->persist($postEventStream1, $aggregateVersion, $currentApplicationVersion); $eventStore->persist($postEventStream2, $aggregateVersion, $currentApplicationVersion); // fake BlogEventSourced event stream $postId2 = PostId::fromNative(md5(rand())); $postEventStream2 = new EventStream($this->streamName(\BlogEventSourced::class), $postId2, []); $eventStore->persist($postEventStream2, $aggregateVersion, $currentApplicationVersion); // creating the migrator $migrator = new Migrator($this->getClassMetadataFactory(), $migratorStore, $eventStore, $this->migrationsDirectory); $emptyMigrator = new Migrator($this->getClassMetadataFactory(), $migratorStore, $eventStore, __DIR__ . '/../../Fixtures/EmptyMigrations'); $this->given($result = $emptyMigrator->migrate())->then()->boolean($result)->isFalse(); $this->given($status = $migrator->status())->and($nextVersion = $status->nextAvailableVersion())->then()->boolean($migratorStore->hasMigration($nextVersion))->isFalse()->and()->when($result = $migrator->migrate())->and(VersionManager::setCurrentApplicationVersion($migratorStore->getLast()->version()))->then()->boolean($migratorStore->hasMigration($nextVersion))->isTrue()->boolean($result)->isTrue()->and()->exception(function () use($migrator) { // because the V1_0_0\BlogEventSourcedMigration class return an invalid stream $migrator->migrate(); })->isInstanceOf(\RuntimeException::class); }