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); 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 new file mode 100644 index 0000000..03ea7fc --- /dev/null +++ b/tests/Integration/MySQL/TimestampedVersionTest.php @@ -0,0 +1,112 @@ + + * + * 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, + ]; + } +} 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,