Skip to content

Commit 768b94d

Browse files
committed
Added initialize method
1 parent 0ce5b1f commit 768b94d

File tree

10 files changed

+154
-10
lines changed

10 files changed

+154
-10
lines changed

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
8+
before_script:
9+
- composer install
10+
11+
script: phpunit
12+
13+
matrix:
14+
allow_failures:
15+
- env: SYMFONY_VERSION=dev-master
16+

lib/Migrator.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ public function __construct(
3232
$this->versionStorage = $versionStorage;
3333
}
3434

35+
/**
36+
* Add all the migrations without running them.
37+
* This should be executed on new database installations.
38+
*/
39+
public function initialize()
40+
{
41+
if ($this->versionStorage->hasVersioningNode()) {
42+
throw MigratorException::cannotInitializeAlreadyHasVersions();
43+
}
44+
45+
foreach (array_keys($this->versionCollection->getAllVersions()) as $timestamp) {
46+
$this->versionStorage->add($timestamp);
47+
}
48+
49+
$this->session->save();
50+
}
51+
3552
public function migrate($to = null, OutputInterface $output)
3653
{
3754
if ($to === null) {
@@ -60,8 +77,11 @@ public function migrate($to = null, OutputInterface $output)
6077
}
6178

6279
$start = microtime(true);
80+
$position = 0;
81+
$output->writeln(sprintf('<comment>%s</comment> %d version(s):', ($direction == 'up' ? 'Upgrading' : 'Reverting'), count($versionsToExecute)));
6382
foreach ($versionsToExecute as $timestamp => $version) {
64-
$output->writeln('<info>Executing migration version</info>: %s', $timestamp);
83+
$position++;
84+
$output->writeln(sprintf(' %s [<info>%d/%d</info>]: %s', $direction == 'up' ? '+' : '-', $position, count($versionsToExecute), $timestamp));
6585
$version->$direction($this->session);
6686

6787
if ($direction === 'down') {
@@ -70,14 +90,10 @@ public function migrate($to = null, OutputInterface $output)
7090
$this->versionStorage->add($timestamp);
7191
}
7292

93+
$this->session->save();
7394
}
7495

75-
$this->session->save();
7696

77-
$output->writeln(sprintf(
78-
'<info>Done. Executed </info>%s<info> migration versions</info> %s',
79-
count($versionsToExecute), number_format(microtime(true) - $start, 4)
80-
));
8197

8298
return $versionsToExecute;
8399
}

lib/MigratorException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public static function versionNotInstance($className)
3131
{
3232
return new self(sprintf('Version class "%s" is not an instance of DTL\PhpcrMigrations\VersionInterface', $className));
3333
}
34+
35+
public static function cannotInitializeAlreadyHasVersions()
36+
{
37+
return new self('Cannot initiaialize a content repository with previously existing migrations.');
38+
}
3439
}

lib/MigratorFactory.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/*
3+
* This file is part of the <package> package.
4+
*
5+
* (c) 2011-2015 Daniel Leech
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace DTL\PhpcrMigrations;
12+
13+
use DTL\PhpcrMigrations\VersionStorage;
14+
use DTL\PhpcrMigrations\VersionFinder;
15+
use PHPCR\SessionInterface;
16+
17+
class MigratorFactory
18+
{
19+
private $storage;
20+
private $finder;
21+
private $session;
22+
23+
public function __construct(
24+
VersionStorage $storage,
25+
VersionFinder $finder,
26+
SessionInterface $session
27+
)
28+
{
29+
$this->storage = $storage;
30+
$this->finder = $finder;
31+
$this->session = $session;
32+
}
33+
34+
public function getMigrator()
35+
{
36+
return new Migrator($this->session, $this->finder->getCollection(), $this->storage);
37+
}
38+
}

lib/VersionCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function has($versionTimestamp)
2525
return isset($this->versions[$versionTimestamp]);
2626
}
2727

28-
public function toArray()
28+
public function getAllVersions()
2929
{
3030
return $this->versions;
3131
}

lib/VersionManager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/*
3+
* This file is part of the <package> package.
4+
*
5+
* (c) Daniel Leech <daniel@dantleech.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace DTL\PhpcrMigrations;
12+
13+
class VersionManager
14+
{
15+
public function __construct(
16+
VersionFinder $versionFinder,
17+
VersionStorage
18+
}

lib/VersionStorage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function init()
3636
if (!$nodeTypeManager->hasNodeType('phpcrMigration:version')) {
3737
$nodeTypeManager->registerNodeTypesCnd(<<<EOT
3838
<phpcrMigrations = 'http://www.danteech.com/phpcr-migrations'>
39-
[phpcrMigrations:version] > nt:base
39+
[phpcrMigrations:version] > nt:base, mix:created
40+
4041
[phpcrMigrations:versions] > nt:base
4142
+* (phpcrMigrations:version)
4243
EOT
@@ -62,6 +63,11 @@ public function getPersistedVersions()
6263
return $versions;
6364
}
6465

66+
public function hasVersioningNode()
67+
{
68+
return $this->session->nodeExists('/' . $this->storageNodeName);
69+
}
70+
6571
public function getCurrentVersion()
6672
{
6773
$this->init();
@@ -88,6 +94,6 @@ public function add($timestamp)
8894
{
8995
$this->init();
9096

91-
$this->storageNode->addNode($timestamp, 'phpcrMigrations:version');
97+
$node = $this->storageNode->addNode($timestamp, 'phpcrMigrations:version');
9298
}
9399
}

tests/Functional/MigrationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public function testMigrateToCurrentVersionFromCurrent()
136136
$this->assertCount(0, $migratedVersions);
137137
}
138138

139+
/**
140+
* It should add all migrations
141+
*/
142+
public function testInitialize()
143+
{
144+
$this->addVersion(self::VERSION1);
145+
$this->addVersion(self::VERSION2);
146+
$this->getMigrator()->initialize();
147+
148+
$nodes = $this->session->getNode('/phpcrMigrations:versions')->getNodes();
149+
150+
$this->assertCount(2, $nodes);
151+
}
152+
139153
private function addVersion($version)
140154
{
141155
$this->filesystem->copy(

tests/Unit/MigratorFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DTL\PhpcrMigrations\Tests\Unit;
4+
5+
use DTL\PhpcrMigrations\VersionFinder;
6+
use DTL\PhpcrMigrations\VersionCollection;
7+
use DTL\PhpcrMigrations\VersionInterface;
8+
use DTL\PhpcrMigrations\VersionStorage;
9+
use DTL\PhpcrMigrations\Migrator;
10+
use DTL\PhpcrMigrations\MigratorFactory;
11+
use PHPCR\SessionInterface;
12+
13+
class MigratorFactoryTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function testFactory()
16+
{
17+
$storage = $this->prophesize(VersionStorage::class);
18+
$finder = $this->prophesize(VersionFinder::class);
19+
$session = $this->prophesize(SessionInterface::class);
20+
$finder->getCollection()->willReturn($this->prophesize(VersionCollection::class)->reveal());
21+
22+
$factory = new MigratorFactory(
23+
$storage->reveal(),
24+
$finder->reveal(),
25+
$session->reveal()
26+
);
27+
$migrator = $factory->getMigrator();
28+
$this->assertInstanceOf(Migrator::class, $migrator);
29+
}
30+
}
31+

tests/Unit/VersionFinderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testGetCollection()
2121
{
2222
$collection = $this->finder->getCollection();
2323
$this->assertInstanceOf(VersionCollection::class, $collection);
24-
$versions = $collection->toArray();
24+
$versions = $collection->getAllVersions();
2525
$this->assertCount(2, $versions);
2626
}
2727

0 commit comments

Comments
 (0)