From 0b4e6a3fd01259b7f0d3c785a368c1b56c03da36 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Tue, 31 Dec 2019 13:07:50 +0000 Subject: [PATCH 1/2] #8 - Add static method resetStorage() --- src/Migrations.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Migrations.php b/src/Migrations.php index d04900a..037b7b2 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -690,4 +690,12 @@ public static function getCompletedVersions(array $options): array return array_flip($completedVersions); } + + /** + * In case we need to renew our DB connection or file + */ + public static function resetStorage(): void + { + self::$storage = null; + } } From 9097c1b112fde4e20fce73d0cd05309732e0bbda Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Tue, 31 Dec 2019 13:08:32 +0000 Subject: [PATCH 2/2] #8 - Add handling of column type TYPE_TIME --- src/Mvc/Model/Migration.php | 3 + tests/Integration/ColumnTypesTest.php | 95 +++++++++++++++++++++++ tests/Integration/IntegrationTestCase.php | 3 + 3 files changed, 101 insertions(+) create mode 100644 tests/Integration/ColumnTypesTest.php diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index 90b2fa4..3b0861b 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -241,6 +241,9 @@ public static function generate( case Column::TYPE_CHAR: $fieldDefinition[] = "'type' => Column::TYPE_CHAR"; break; + case Column::TYPE_TIME: + $fieldDefinition[] = "'type' => Column::TYPE_TIME"; + break; case Column::TYPE_DATE: $fieldDefinition[] = "'type' => Column::TYPE_DATE"; break; diff --git a/tests/Integration/ColumnTypesTest.php b/tests/Integration/ColumnTypesTest.php new file mode 100644 index 0000000..e47279e --- /dev/null +++ b/tests/Integration/ColumnTypesTest.php @@ -0,0 +1,95 @@ + Column::TYPE_INTEGER, + 'size' => 10, + 'unsigned' => true, + 'notNull' => true, + 'first' => true, + ], + [0, 1, 123, 9000], + ], + [ + 'column_time', + [ + 'type' => Column::TYPE_TIME, + ], + ['00:00:00', '23:59:55', '12:00:12'], + ] + ]; + } + + /** + * @dataProvider columnsDataProvider + * + * @param string $columnName + * @param array $definition + * @param array $values + * + * @throws \Phalcon\Migrations\Script\ScriptException + * @throws \Phalcon\Mvc\Model\Exception + * @throws Exception + */ + public function testColumnDefinition(string $columnName, array $definition, array $values): void + { + $tableName = $columnName . '_test'; + $migrationsDir = root_path('tests/var/output/' . __FUNCTION__); + + $this->db->createTable($tableName, getenv('TEST_DB_DATABASE'), [ + 'columns' => [ + new Column($columnName, $definition), + ], + ]); + + /** + * Generate | Drop | Run + */ + Migrations::generate([ + 'migrationsDir' => $migrationsDir, + 'config' => self::$generateConfig, + 'tableName' => $tableName, + ]); + $this->db->query('DROP TABLE ' . $tableName); + Migrations::run([ + 'migrationsDir' => $migrationsDir, + 'config' => self::$generateConfig, + 'migrationsInDb' => true, + ]); + + /** + * Insert values + */ + foreach ($values as $value) { + $this->db->insert($tableName, [$value], [$columnName]); + } + + Migrations::resetStorage(); + remove_dir($migrationsDir); + + /** @var Column $column */ + $column = $this->db->describeColumns($tableName)[0]; + $rows = $this->db->fetchAll("SELECT $columnName FROM $tableName", Enum::FETCH_ASSOC); + $rows = Arr::flatten($rows); + + $this->assertSame($definition['type'], $column->getType()); + $this->assertEquals($values, $rows); + } +} diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index e18539c..1773a55 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -32,6 +32,9 @@ public static function setUpBeforeClass(): void 'password' => getenv('TEST_DB_PASSWORD'), 'dbname' => getenv('TEST_DB_DATABASE'), ], + 'application' => [ + 'logInDb' => true, + ], ]); ob_start();