Skip to content

Commit

Permalink
Merge branch 'master' of github.com:doctrine/migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeSimonson committed Jun 27, 2015
2 parents 0e60856 + 2c1a57b commit 081155d
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"doctrine/orm": "2.*",
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "~4.7",
"satooshi/php-coveralls": "0.6.*",
"doctrine/coding-standard": "dev-master",
"mockery/mockery": "^0.9.4"
Expand Down
27 changes: 7 additions & 20 deletions lib/Doctrine/DBAL/Migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,15 @@ public function writeSqlFile($path, $to = null)

$direction = $from > $to ? 'down' : 'up';

$string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
$string .= sprintf("# Migrating from %s to %s\n", $from, $to);

foreach ($sql as $version => $queries) {
$string .= "\n# Version " . $version . "\n";
foreach ($queries as $query) {
$string .= $query . ";\n";
}
if ($direction == "down") {
$string .= "DELETE FROM " . $this->configuration->getMigrationsTableName() . " WHERE version = '" . $version . "';\n";
} else {
$string .= "INSERT INTO " . $this->configuration->getMigrationsTableName() . " (version) VALUES ('" . $version . "');\n";
}
}
if (is_dir($path)) {
$path = realpath($path);
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
}
$this->outputWriter->write(sprintf("# Migrating from %s to %s\n", $from, $to));

$this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
$sqlWriter = new SqlFileWriter(
$this->configuration->getMigrationsTableName(),
$path,
$this->outputWriter
);

return file_put_contents($path, $string);
return $sqlWriter->write($sql, $direction);
}

/**
Expand Down
94 changes: 94 additions & 0 deletions lib/Doctrine/DBAL/Migrations/SqlFileWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\DBAL\Migrations;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\InvalidArgumentException;

class SqlFileWriter {

private $migrationsTableName;

private $destPath;

/** @var null|OutputWriter */
private $outputWriter;

public function __construct($migrationsTableName, $destPath, OutputWriter $outputWriter = null)
{
if (empty($migrationsTableName)) {
$this->throwInvalidArgumentException('Migrations table name cannot be empty.');
}
$this->migrationsTableName = $migrationsTableName;

if (empty($destPath)) {
$this->throwInvalidArgumentException('Destination file must be specified.');
}
$this->destPath = $destPath;

$this->outputWriter = $outputWriter;
}

/**
*
* @param $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string)
* @param $direction
* @return int
*/
public function write($queriesByVersion, $direction)
{
$string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));

foreach ($queriesByVersion as $version => $queries) {
$string .= "\n# Version " . $version . "\n";
foreach ($queries as $query) {
$string .= $query . ";\n";
}
if ($direction == "down") {
$string .= "DELETE FROM " . $this->migrationsTableName . " WHERE version = '" . $version . "';\n";
} else {
$string .= "INSERT INTO " . $this->migrationsTableName . " (version) VALUES ('" . $version . "');\n";
}
}
$path = $this->destPath;
if (is_dir($path)) {
$path = realpath($path);
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
}

if ($this->outputWriter) {
$this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
}

return file_put_contents($path, $string);
}

/**
* This only exists for backwards-compatibiliy with DBAL 2.4
*/
protected function throwInvalidArgumentException($message)
{
if (class_exists('Doctrine\DBAL\Exception\InvalidArgumentException')) {
throw new InvalidArgumentException($message);
} else {
throw new DBALException($message);
}
}
}
25 changes: 8 additions & 17 deletions lib/Doctrine/DBAL/Migrations/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,16 @@ public function writeSqlFile($path, $direction = 'up')
{
$queries = $this->execute($direction, true);

$string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
$this->outputWriter->write("\n# Version " . $this->version . "\n");

$string .= "\n# Version " . $this->version . "\n";
foreach ($queries as $query) {
$string .= $query . ";\n";
}
if ($direction == "down") {
$string .= "DELETE FROM " . $this->configuration->getMigrationsTableName() . " WHERE version = '" . $this->version . "';\n";
} else {
$string .= "INSERT INTO " . $this->configuration->getMigrationsTableName() . " (version) VALUES ('" . $this->version . "');\n";
}
if (is_dir($path)) {
$path = realpath($path);
$path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
}

$this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
$sqlQueries = [$this->version => $queries];
$sqlWriter = new SqlFileWriter(
$this->configuration->getMigrationsTableName(),
$path,
$this->outputWriter
);

return file_put_contents($path, $string);
return $sqlWriter->write($sqlQueries, $direction);
}

/**
Expand Down
76 changes: 76 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Doctrine\DBAL\Migrations\Tests;

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Tests\Stub\AbstractMigrationStub;
use Doctrine\DBAL\Migrations\Version;

/**
* Class AbstractMigrationTest
* @package Doctrine\DBAL\Migrations\Tests
*
* @author Robbert van den Bogerd <rvdbogerd@ibuildings.nl>
*/
class AbstractMigrationTest extends MigrationTestCase
{
private $config;
private $version;
private $migration;
private $outputWriter;

public function setUp()
{
$this->outputWriter = $this->getMockBuilder('Doctrine\DBAL\Migrations\OutputWriter')
->getMock();

$this->config = new Configuration($this->getSqliteConnection(), $this->outputWriter);
$this->config->setMigrationsDirectory(\sys_get_temp_dir());
$this->config->setMigrationsNamespace('DoctrineMigrations\\');

$this->version = new Version($this->config, 'Dummy', 'Doctrine\DBAL\Migrations\Tests\Stub\VersionDummy');
$this->migration = new AbstractMigrationStub($this->version);
}

public function testGetDescriptionReturnsEmptyString()
{
$this->assertSame('', $this->migration->getDescription());
}

public function testWarnIfInvokesOutputWriter()
{
$this->outputWriter
->expects($this->once())
->method('write');

$this->migration->warnIf(true, 'Warning was thrown');
}

public function testWriteInvokesOutputWriter()
{
$this->outputWriter
->expects($this->once())
->method('write')
->with('Message');

$this->migration->exposed_Write('Message');
}

public function testAbortIfThrowsException()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\AbortMigrationException', 'Something failed');
$this->migration->abortIf(true, 'Something failed');
}

public function testSkipIfThrowsException()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\SkipMigrationException', 'Something skipped');
$this->migration->skipIf(true, 'Something skipped');
}

public function testThrowIrreversibleMigrationException()
{
$this->setExpectedException('Doctrine\DBAL\Migrations\IrreversibleMigrationException', 'Irreversible migration');
$this->migration->exposed_ThrowIrreversibleMigrationException('Irreversible migration');
}
}
41 changes: 27 additions & 14 deletions tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\DBAL\Migrations\Tests;

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use \Mockery as m;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class MigrationTest extends MigrationTestCase
{
/** @var Configuration */
Expand Down Expand Up @@ -73,6 +94,10 @@ public function getSqlProvider()
*/
public function testWriteSqlFile($path, $from, $to, $getSqlReturn)
{
$expectedReturn = 123;
$sqlWriter = m::instanceMock('overload:Doctrine\DBAL\Migrations\SqlFileWriter');
$sqlWriter->shouldReceive('write')->with(m::type('array'), m::anyOf('up', 'down'))->andReturn($expectedReturn);

$outputWriter = m::mock('Doctrine\DBAL\Migrations\OutputWriter');
$outputWriter->shouldReceive('write');

Expand All @@ -88,26 +113,14 @@ public function testWriteSqlFile($path, $from, $to, $getSqlReturn)
$migration->shouldReceive('getSql')->with($to)->andReturn($getSqlReturn);

$result = $migration->writeSqlFile($path, $to);
$this->assertNotFalse($result);
if (!is_dir($path)) {
$this->assertNotEmpty(file_get_contents($path));
}

// cleanup if necessary
if (is_dir($path)) {
$createdFiles = glob(realpath($path) . '/*.sql');
foreach($createdFiles as $file) {
unlink($file);
}
} elseif(is_file($path)) {
unlink($path);
}
$this->assertEquals($expectedReturn, $result);
}

public function writeSqlFileProvider()
{
return [
[__DIR__, 0, 1, ['1' => ['SHOW DATABASES;']]], // up
[__DIR__, 0, null, ['1' => ['SHOW DATABASES;']]], // up
[__DIR__, 1, 1, ['1' => ['SHOW DATABASES;']]], // up (same)
[__DIR__, 1, 0, ['1' => ['SHOW DATABASES;']]], // down
[__DIR__ . '/tmpfile.sql', 0, 1, ['1' => ['SHOW DATABASES']]], // tests something actually got written
Expand Down
Loading

0 comments on commit 081155d

Please sign in to comment.