Skip to content

Commit

Permalink
Merge pull request #54 from phalcon/fix/#53-run-ts-based-migrations
Browse files Browse the repository at this point in the history
#53 - Fix running timestamp based migrations
  • Loading branch information
Jeckerson authored Feb 6, 2020
2 parents aaaa299 + f22013f commit 6843613
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static function run(array $options)
$optionStack->setDefaultOption('verbose', false);

// Define versioning type to be used
if (isset($options['tsBased']) && $optionStack->getOption('tsBased') === true) {
if (!empty($options['tsBased']) || $optionStack->getOption('tsBased')) {
VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED);
} else {
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/MySQL/ColumnTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testColumnDefinition(string $columnName, array $definition, arra
'config' => self::$generateConfig,
'tableName' => $tableName,
]);
$this->db->query('DROP TABLE ' . $tableName);
$this->db->dropTable($tableName);
Migrations::run([
'migrationsDir' => $migrationsDir,
'config' => self::$generateConfig,
Expand Down
112 changes: 112 additions & 0 deletions tests/Integration/MySQL/TimestampedVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* This file is part of the Phalcon Migrations.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Migrations\Tests\Integration\MySQL;

use Phalcon\Db\Column;
use Phalcon\Migrations\Migrations;
use Phalcon\Migrations\Script\ScriptException;
use Phalcon\Mvc\Model\Exception;

use function Phalcon\Migrations\Tests\root_path;

final class TimestampedVersionTest extends MySQLIntegrationTestCase
{
/**
* @throws ScriptException
* @throws Exception
* @throws \Exception
*/
public function testSingleVersion(): void
{
$options = $this->getOptions(root_path('tests/var/output/timestamp-single-version'));

$tableName = 'timestamp-versions-1';
$this->db->createTable($tableName, '', [
'columns' => [
new Column('name', [
'type' => Column::TYPE_VARCHAR,
'size' => 25,
]),
],
]);

Migrations::generate($options);
$this->db->dropTable($tableName);
Migrations::run($options);

$this->assertTrue($this->db->tableExists($tableName));
}

/**
* @throws Exception
* @throws ScriptException
* @throws \Exception
*/
public function testSeveralVersions(): void
{
$options = $this->getOptions(root_path('tests/var/output/timestamp-several-versions'));

/**
* Generate first version
*/
$tableName1 = 'timestamp-versions-2';
$this->db->createTable($tableName1, '', [
'columns' => [
new Column('name', [
'type' => Column::TYPE_VARCHAR,
'size' => 25,
]),
],
]);

Migrations::generate($options);

/**
* Generate second version
*/
$tableName2 = 'timestamp-versions-3';
$this->db->createTable($tableName2, '', [
'columns' => [
new Column('name', [
'type' => Column::TYPE_VARCHAR,
'size' => 25,
]),
],
]);

Migrations::generate($options);

/**
* Drop tables and run migrations
*/
$this->db->dropTable($tableName1);
$this->db->dropTable($tableName2);
Migrations::run($options);

$this->assertTrue($this->db->tableExists($tableName1));
$this->assertTrue($this->db->tableExists($tableName2));
}

private function getOptions(string $path): array
{
return [
'migrationsDir' => $path,
'config' => self::$generateConfig,
'tableName' => '@',
'descr' => '1',
'tsBased' => true,
'migrationsInDb' => true,
];
}
}
2 changes: 1 addition & 1 deletion tests/Integration/PostgreSQL/ColumnTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testColumnDefinition(string $columnName, array $definition, arra
'config' => self::$generateConfig,
'tableName' => $tableName,
]);
$this->db->query('DROP TABLE ' . $tableName);
$this->db->dropTable($tableName);
Migrations::run([
'migrationsDir' => $migrationsDir,
'config' => self::$generateConfig,
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/PostgreSQL/IssuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testIssue1(): void
'config' => self::$generateConfig,
'tableName' => $tableName,
]);
$this->db->query('DROP TABLE ' . $tableName);
$this->db->dropTable($tableName);
Migrations::run([
'migrationsDir' => $migrationsDir,
'config' => self::$generateConfig,
Expand Down

0 comments on commit 6843613

Please sign in to comment.