From bcfb76314a8bc200883ef0389c1f021e2a30a7c4 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 6 Feb 2020 17:03:10 +0000 Subject: [PATCH 1/4] #53 - Fix condition checking 'tsBased' option --- src/Migrations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Migrations.php b/src/Migrations.php index 2049e12..06be54d 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -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); From 7d1b02ca0e3c1e0b2821e5ab0a6a016c7b934db3 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 6 Feb 2020 17:03:30 +0000 Subject: [PATCH 2/4] #53 - Add tests for timestamped versioning --- .../MySQL/TimestampedVersionTest.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/Integration/MySQL/TimestampedVersionTest.php diff --git a/tests/Integration/MySQL/TimestampedVersionTest.php b/tests/Integration/MySQL/TimestampedVersionTest.php new file mode 100644 index 0000000..49066ab --- /dev/null +++ b/tests/Integration/MySQL/TimestampedVersionTest.php @@ -0,0 +1,111 @@ + + * + * 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->query('DROP TABLE `' . $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->query('DROP TABLE `' . $tableName1 . '`'); + $this->db->query('DROP TABLE `' . $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, + ]; + } +} From cc4631f08c468c0092b1ded76394440250f2c7cc Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 6 Feb 2020 17:07:53 +0000 Subject: [PATCH 3/4] #53 - Add 'migrationsInDb' option --- tests/Integration/MySQL/TimestampedVersionTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Integration/MySQL/TimestampedVersionTest.php b/tests/Integration/MySQL/TimestampedVersionTest.php index 49066ab..bd9c2de 100644 --- a/tests/Integration/MySQL/TimestampedVersionTest.php +++ b/tests/Integration/MySQL/TimestampedVersionTest.php @@ -106,6 +106,7 @@ private function getOptions(string $path): array 'tableName' => '@', 'descr' => '1', 'tsBased' => true, + 'migrationsInDb' => true, ]; } } From f22013f063f030f991d9e5d8a4129f8e785e45c7 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Thu, 6 Feb 2020 17:24:38 +0000 Subject: [PATCH 4/4] #53 - Change DROP TABLE to use via method --- tests/Integration/MySQL/ColumnTypesTest.php | 2 +- tests/Integration/MySQL/TimestampedVersionTest.php | 6 +++--- tests/Integration/PostgreSQL/ColumnTypesTest.php | 2 +- tests/Integration/PostgreSQL/IssuesTest.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Integration/MySQL/ColumnTypesTest.php b/tests/Integration/MySQL/ColumnTypesTest.php index 2dd7359..0232f0b 100644 --- a/tests/Integration/MySQL/ColumnTypesTest.php +++ b/tests/Integration/MySQL/ColumnTypesTest.php @@ -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, diff --git a/tests/Integration/MySQL/TimestampedVersionTest.php b/tests/Integration/MySQL/TimestampedVersionTest.php index bd9c2de..03ea7fc 100644 --- a/tests/Integration/MySQL/TimestampedVersionTest.php +++ b/tests/Integration/MySQL/TimestampedVersionTest.php @@ -42,7 +42,7 @@ public function testSingleVersion(): void ]); Migrations::generate($options); - $this->db->query('DROP TABLE `' . $tableName . '`'); + $this->db->dropTable($tableName); Migrations::run($options); $this->assertTrue($this->db->tableExists($tableName)); @@ -90,8 +90,8 @@ public function testSeveralVersions(): void /** * Drop tables and run migrations */ - $this->db->query('DROP TABLE `' . $tableName1 . '`'); - $this->db->query('DROP TABLE `' . $tableName2 . '`'); + $this->db->dropTable($tableName1); + $this->db->dropTable($tableName2); Migrations::run($options); $this->assertTrue($this->db->tableExists($tableName1)); diff --git a/tests/Integration/PostgreSQL/ColumnTypesTest.php b/tests/Integration/PostgreSQL/ColumnTypesTest.php index 36ba5fe..3036696 100644 --- a/tests/Integration/PostgreSQL/ColumnTypesTest.php +++ b/tests/Integration/PostgreSQL/ColumnTypesTest.php @@ -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, diff --git a/tests/Integration/PostgreSQL/IssuesTest.php b/tests/Integration/PostgreSQL/IssuesTest.php index 55d7e7f..1377975 100644 --- a/tests/Integration/PostgreSQL/IssuesTest.php +++ b/tests/Integration/PostgreSQL/IssuesTest.php @@ -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,