-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:doctrine/migrations
- Loading branch information
Showing
10 changed files
with
442 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/Doctrine/DBAL/Migrations/Tests/AbstractMigrationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.