From d405294cba393cc81a6a0582ec9dd0fdcaacedd7 Mon Sep 17 00:00:00 2001 From: sergeysviridenko Date: Mon, 21 Aug 2017 18:48:37 +0300 Subject: [PATCH 1/4] Fixed query for deleting row from migration log table --- scripts/Phalcon/Migrations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/Phalcon/Migrations.php b/scripts/Phalcon/Migrations.php index 86a309d7b..5eac24c9d 100644 --- a/scripts/Phalcon/Migrations.php +++ b/scripts/Phalcon/Migrations.php @@ -546,7 +546,7 @@ public static function removeCurrentVersion($options, $version) if (isset($options['migrationsInDb']) && (bool)$options['migrationsInDb']) { /** @var AdapterInterface $connection */ $connection = self::$_storage; - $connection->execute('DELETE FROM '. self::MIGRATION_LOG_TABLE .' WHERE version=\'' . $version . '\' LIMIT 1'); + $connection->execute('DELETE FROM '. self::MIGRATION_LOG_TABLE .' WHERE version=\'' . $version . '\''); } else { $currentVersions = self::getCompletedVersions($options); unset($currentVersions[(string)$version]); From 0712fa3f56b51bd5226f2b163751f339298760b5 Mon Sep 17 00:00:00 2001 From: sergeysviridenko Date: Fri, 1 Sep 2017 11:49:47 +0300 Subject: [PATCH 2/4] Fixed add the similar delimiters for data in the migration --- .travis.yml | 12 +++ composer.json | 5 +- scripts/Phalcon/Mvc/Model/Migration.php | 4 +- tests/_ci/setup_dbs.sh | 5 + .../_data/console/.phalcon/migration-version | 0 .../app/migrations/1.0.0/test_migrations.dat | 1 + .../app/migrations/1.0.0/test_migrations.php | 100 ++++++++++++++++++ tests/_data/console/app/mysql/config.php | 21 ++++ tests/_data/schemas/mysql/dump.sql | 26 +++++ tests/_support/ConsoleTester.php | 26 +++++ tests/_support/FunctionalTester.php | 26 +++++ tests/_support/Helper/Console.php | 21 ++++ tests/_support/Helper/Functional.php | 17 +++ tests/_support/helpers.php | 47 ++++++++ tests/console.suite.yml | 12 +++ tests/console/GenerateMysqlMigrationCept.php | 25 +++++ tests/console/RunMysqlMigrationCept.php | 30 ++++++ tests/console/_bootstrap.php | 4 + tests/functional.suite.yml | 13 +++ tests/functional/_bootstrap.php | 2 + 20 files changed, 394 insertions(+), 3 deletions(-) create mode 100644 tests/_data/console/.phalcon/migration-version create mode 100644 tests/_data/console/app/migrations/1.0.0/test_migrations.dat create mode 100644 tests/_data/console/app/migrations/1.0.0/test_migrations.php create mode 100644 tests/_data/console/app/mysql/config.php create mode 100644 tests/_data/schemas/mysql/dump.sql create mode 100644 tests/_support/ConsoleTester.php create mode 100644 tests/_support/FunctionalTester.php create mode 100755 tests/_support/Helper/Console.php create mode 100755 tests/_support/Helper/Functional.php create mode 100755 tests/_support/helpers.php create mode 100755 tests/console.suite.yml create mode 100644 tests/console/GenerateMysqlMigrationCept.php create mode 100644 tests/console/RunMysqlMigrationCept.php create mode 100755 tests/console/_bootstrap.php create mode 100755 tests/functional.suite.yml create mode 100755 tests/functional/_bootstrap.php diff --git a/.travis.yml b/.travis.yml index c27c106d6..e38e1805f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,13 @@ env: - TEST_DB_POSTGRESQL_USER="postgres" - TEST_DB_POSTGRESQL_PASSWD="" - TEST_DB_POSTGRESQL_NAME="devtools" + - TEST_DB_MYSQL_DSN="mysql:host=127.0.0.1;dbname=devtools" + - TEST_DB_MYSQL_HOST="127.0.0.1" + - TEST_DB_MYSQL_PORT="3306" + - TEST_DB_MYSQL_USER="root" + - TEST_DB_MYSQL_PASSWD="" + - TEST_DB_MYSQL_NAME="devtools" + - TEST_DB_MYSQL_CHARSET="utf8" - PATH="$PATH:~/bin" - DISPLAY=":99.0" - PHALCON_VERSION="v3.2.0" @@ -57,9 +64,14 @@ install: - cd $TRAVIS_BUILD_DIR - travis_retry composer install --prefer-dist --no-interaction +before_script: + - ln -s $PWD/phalcon.php ~/bin/phalcon + script: - vendor/bin/codecept build - vendor/bin/codecept run unit -v + - vendor/bin/codecept run functional -v + - vendor/bin/codecept run console -v notifications: email: diff --git a/composer.json b/composer.json index 8ae2dee83..e3ab35f2e 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,10 @@ "Phalcon\\Test\\": "tests/unit", "Phalcon\\Test\\Models\\": "tests/_data/models", "Phalcon\\Test\\Module\\": "tests/_support/Module" - } + }, + "files": [ + "tests/_support/helpers.php" + ] }, "bin": ["phalcon.php"] } diff --git a/scripts/Phalcon/Mvc/Model/Migration.php b/scripts/Phalcon/Mvc/Model/Migration.php index a5b8819c4..f7fdea7ae 100644 --- a/scripts/Phalcon/Mvc/Model/Migration.php +++ b/scripts/Phalcon/Mvc/Model/Migration.php @@ -440,7 +440,7 @@ public static function generate(ItemInterface $version, $table, $exportData = nu unset($value); } - fputcsv($fileHandler, $data); + fputcsv($fileHandler, $data, '|'); unset($row); unset($data); } @@ -852,7 +852,7 @@ public function batchInsert($tableName, $fields) self::$_connection->begin(); self::$_connection->delete($tableName); $batchHandler = fopen($migrationData, 'r'); - while (($line = fgetcsv($batchHandler)) !== false) { + while (($line = fgetcsv($batchHandler, 0, '|')) !== false) { $values = array_map( function ($value) { return null === $value ? null : $value; diff --git a/tests/_ci/setup_dbs.sh b/tests/_ci/setup_dbs.sh index 80a4a9aa5..e4ce52070 100755 --- a/tests/_ci/setup_dbs.sh +++ b/tests/_ci/setup_dbs.sh @@ -19,4 +19,9 @@ psql -c 'create database devtools;' -U postgres psql -U postgres devtools -q -f "${TRAVIS_BUILD_DIR}/tests/_data/schemas/postgresql/dump.sql" echo -e "Done\n" +echo -e "Create MySQL database..." +mysql -u root -e "CREATE DATABASE IF NOT EXISTS devtools charset=utf8 collate=utf8_general_ci;" +cat "${TRAVIS_BUILD_DIR}/tests/_data/schemas/mysql/dump.sql" | mysql -u root devtools +echo -e "Done\n" + wait diff --git a/tests/_data/console/.phalcon/migration-version b/tests/_data/console/.phalcon/migration-version new file mode 100644 index 000000000..e69de29bb diff --git a/tests/_data/console/app/migrations/1.0.0/test_migrations.dat b/tests/_data/console/app/migrations/1.0.0/test_migrations.dat new file mode 100644 index 000000000..54cd9d685 --- /dev/null +++ b/tests/_data/console/app/migrations/1.0.0/test_migrations.dat @@ -0,0 +1 @@ +1|test1|"2017-08-29 13:05:41"|"2017-08-29 13:05:41" diff --git a/tests/_data/console/app/migrations/1.0.0/test_migrations.php b/tests/_data/console/app/migrations/1.0.0/test_migrations.php new file mode 100644 index 000000000..3ed47ad0f --- /dev/null +++ b/tests/_data/console/app/migrations/1.0.0/test_migrations.php @@ -0,0 +1,100 @@ +morphTable('test_migrations', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'updated_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '2', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + $this->batchInsert('test_migrations', [ + 'id', + 'name', + 'created_at', + 'updated_at' + ] + ); + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + $this->batchDelete('test_migrations'); + } + +} diff --git a/tests/_data/console/app/mysql/config.php b/tests/_data/console/app/mysql/config.php new file mode 100644 index 000000000..a20dad095 --- /dev/null +++ b/tests/_data/console/app/mysql/config.php @@ -0,0 +1,21 @@ + [ + 'adapter' => 'Mysql', + 'host' => '127.0.0.1', + 'username' => 'root', + 'password' => '', + 'dbname' => 'devtools' + ], + 'logger' => [ + 'path' => tests_path('_output/logs/console/mysql/'), + 'format' => '%date% [%type%] %message%', + 'date' => 'D j H:i:s', + 'logLevel' => Logger::DEBUG, + 'filename' => 'tests.log', + ] +]); diff --git a/tests/_data/schemas/mysql/dump.sql b/tests/_data/schemas/mysql/dump.sql new file mode 100644 index 000000000..1631e851d --- /dev/null +++ b/tests/_data/schemas/mysql/dump.sql @@ -0,0 +1,26 @@ +-- +-- Table structure for table `phalcon_migrations` +-- +DROP TABLE IF EXISTS `phalcon_migrations`; +CREATE TABLE `phalcon_migrations` ( + `version` varchar(255) NOT NULL, + `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `end_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + KEY `idx_phalcon_migrations_version` (`version`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +DROP TABLE IF EXISTS `test_migrations`; +CREATE TABLE `test_migrations` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(45) NOT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime NOT NULL, + `active` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +INSERT into `test_migrations` SET + `name` = 'test2', + `active` = 1, + `created_at` = NOW(), + `updated_at` = NOW(); diff --git a/tests/_support/ConsoleTester.php b/tests/_support/ConsoleTester.php new file mode 100644 index 000000000..03d7b61b0 --- /dev/null +++ b/tests/_support/ConsoleTester.php @@ -0,0 +1,26 @@ +wantToTest('generating migration for MySQL database'); + +$output=<<amInPath(dirname(app_path())); + +$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.php')); +$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); + +$I->runShellCommand('phalcon migration --action=generate --migrations=app/migrations --table=test_migrations --data=always --config=app/mysql/config.php --log-in-db'); + +$I->seeInShellOutput($output); + +$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.php')); +$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); diff --git a/tests/console/RunMysqlMigrationCept.php b/tests/console/RunMysqlMigrationCept.php new file mode 100644 index 000000000..603b43e3e --- /dev/null +++ b/tests/console/RunMysqlMigrationCept.php @@ -0,0 +1,30 @@ +wantToTest('Running migration for MySQL database'); + +$output=<<amInPath(dirname(app_path())); + +$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.php')); +$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); + +$I->runShellCommand('phalcon migration --action=run --version=1.0.0'); +$I->runShellCommand('phalcon migration --action=run --version=1.0.1'); +$I->runShellCommand('phalcon migration --action=run --version=1.0.0'); +$I->runShellCommand('phalcon migration --action=run --version=1.0.1'); + +$I->seeInShellOutput($output); + +$I->deleteDir(app_path('migrations/1.0.1/')); + +$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.php')); +$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); diff --git a/tests/console/_bootstrap.php b/tests/console/_bootstrap.php new file mode 100755 index 000000000..dd4d9ac4f --- /dev/null +++ b/tests/console/_bootstrap.php @@ -0,0 +1,4 @@ + Date: Fri, 1 Sep 2017 17:13:11 +0300 Subject: [PATCH 3/4] Added option temolate-engine for project command --- scripts/Phalcon/Builder/Project/Micro.php | 10 ++++--- scripts/Phalcon/Builder/Project/Modules.php | 10 ++++--- scripts/Phalcon/Builder/Project/Simple.php | 10 ++++--- scripts/Phalcon/Commands/Builtin/Project.php | 27 ++++++++++-------- scripts/Phalcon/Commands/Builtin/Scaffold.php | 4 +-- templates/project/micro/views/404.volt | 14 ++++++++++ templates/project/micro/views/index.volt | 28 +++++++++++++++++++ 7 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 templates/project/micro/views/404.volt create mode 100644 templates/project/micro/views/index.volt diff --git a/scripts/Phalcon/Builder/Project/Micro.php b/scripts/Phalcon/Builder/Project/Micro.php index 65ed9be52..2a7dca531 100644 --- a/scripts/Phalcon/Builder/Project/Micro.php +++ b/scripts/Phalcon/Builder/Project/Micro.php @@ -86,12 +86,14 @@ private function createHtaccessFiles() */ private function createIndexViewFiles() { - $getFile = $this->options->get('templatePath') . '/project/micro/views/index.phtml'; - $putFile = $this->options->get('projectPath').'app/views/index.phtml'; + $engine = $this->options->get('templateEngine') == 'volt' ? 'volt' : 'phtml'; + + $getFile = $this->options->get('templatePath') . '/project/micro/views/index.' . $engine; + $putFile = $this->options->get('projectPath').'app/views/index.' . $engine; $this->generateFile($getFile, $putFile); - $getFile = $this->options->get('templatePath') . '/project/micro/views/404.phtml'; - $putFile = $this->options->get('projectPath').'app/views/404.phtml'; + $getFile = $this->options->get('templatePath') . '/project/micro/views/404.' . $engine; + $putFile = $this->options->get('projectPath').'app/views/404.' . $engine; $this->generateFile($getFile, $putFile); return $this; diff --git a/scripts/Phalcon/Builder/Project/Modules.php b/scripts/Phalcon/Builder/Project/Modules.php index e4d224d39..5083f5e88 100644 --- a/scripts/Phalcon/Builder/Project/Modules.php +++ b/scripts/Phalcon/Builder/Project/Modules.php @@ -147,12 +147,14 @@ private function createHtaccessFiles() */ private function createIndexViewFiles() { - $getFile = $this->options->get('templatePath') . '/project/modules/views/index.volt'; - $putFile = $this->options->get('projectPath') . 'app/modules/frontend/views/index.volt'; + $engine = $this->options->get('templateEngine') == 'volt' ? 'volt' : 'phtml'; + + $getFile = $this->options->get('templatePath') . '/project/modules/views/index.' . $engine; + $putFile = $this->options->get('projectPath') . 'app/modules/frontend/views/index.' . $engine; $this->generateFile($getFile, $putFile); - $getFile = $this->options->get('templatePath') . '/project/modules/views/index/index.volt'; - $putFile = $this->options->get('projectPath') . 'app/modules/frontend/views/index/index.volt'; + $getFile = $this->options->get('templatePath') . '/project/modules/views/index/index.' . $engine; + $putFile = $this->options->get('projectPath') . 'app/modules/frontend/views/index/index.' . $engine; $this->generateFile($getFile, $putFile); return $this; diff --git a/scripts/Phalcon/Builder/Project/Simple.php b/scripts/Phalcon/Builder/Project/Simple.php index bbe669352..258d382c9 100644 --- a/scripts/Phalcon/Builder/Project/Simple.php +++ b/scripts/Phalcon/Builder/Project/Simple.php @@ -116,12 +116,14 @@ private function createHtaccessFiles() */ private function createIndexViewFiles() { - $getFile = $this->options->get('templatePath') . '/project/simple/views/index.volt'; - $putFile = $this->options->get('projectPath').'app/views/index.volt'; + $engine = $this->options->get('templateEngine') == 'volt' ? 'volt' : 'phtml'; + + $getFile = $this->options->get('templatePath') . '/project/simple/views/index.' . $engine; + $putFile = $this->options->get('projectPath').'app/views/index.' . $engine; $this->generateFile($getFile, $putFile); - $getFile = $this->options->get('templatePath') . '/project/simple/views/index/index.volt'; - $putFile = $this->options->get('projectPath').'app/views/index/index.volt'; + $getFile = $this->options->get('templatePath') . '/project/simple/views/index/index.' . $engine; + $putFile = $this->options->get('projectPath').'app/views/index/index.' . $engine; $this->generateFile($getFile, $putFile); return $this; diff --git a/scripts/Phalcon/Commands/Builtin/Project.php b/scripts/Phalcon/Commands/Builtin/Project.php index 9f3f96517..cbcf5a234 100644 --- a/scripts/Phalcon/Commands/Builtin/Project.php +++ b/scripts/Phalcon/Commands/Builtin/Project.php @@ -42,13 +42,14 @@ public function getPossibleParams() { return [ 'name=s' => 'Name of the new project', - 'enable-webtools' => 'Determines if webtools should be enabled [optional]', - 'directory=s' => 'Base path on which project will be created [optional]', - 'type=s' => 'Type of the application to be generated (cli, micro, simple, modules)', - 'template-path=s' => 'Specify a template path [optional]', - 'use-config-ini' => 'Use a ini file as configuration file [optional]', - 'trace' => 'Shows the trace of the framework in case of exception [optional]', - 'help' => 'Shows this help [optional]', + 'enable-webtools' => 'Determines if webtools should be enabled [optional]', + 'directory=s' => 'Base path on which project will be created [optional]', + 'type=s' => 'Type of the application to be generated (cli, micro, simple, modules)', + 'template-path=s' => 'Specify a template path [optional]', + 'template-engine=s' => 'Define the template engine, default phtml (phtml, volt) [optional]', + 'use-config-ini' => 'Use a ini file as configuration file [optional]', + 'trace' => 'Shows the trace of the framework in case of exception [optional]', + 'help' => 'Shows this help [optional]', ]; } @@ -60,12 +61,13 @@ public function getPossibleParams() */ public function run(array $parameters) { - $projectName = $this->getOption(['name', 1], null, 'default'); - $projectType = $this->getOption(['type', 2], null, 'simple'); - $projectPath = $this->getOption(['directory', 3]); - $templatePath = $this->getOption(['template-path'], null, TEMPLATE_PATH); + $projectName = $this->getOption(['name', 1], null, 'default'); + $projectType = $this->getOption(['type', 2], null, 'simple'); + $projectPath = $this->getOption(['directory', 3]); + $templatePath = $this->getOption(['template-path'], null, TEMPLATE_PATH); $enableWebtools = $this->getOption(['enable-webtools', 4], null, false); - $useConfigIni = $this->getOption('use-config-ini'); + $useConfigIni = $this->getOption('use-config-ini'); + $templateEngine = $this->getOption(['template-engine'], null, "phtml"); $builder = new ProjectBuilder([ 'name' => $projectName, @@ -73,6 +75,7 @@ public function run(array $parameters) 'directory' => $projectPath, 'enableWebTools' => $enableWebtools, 'templatePath' => $templatePath, + 'templateEngine' => $templateEngine, 'useConfigIni' => $useConfigIni ]); diff --git a/scripts/Phalcon/Commands/Builtin/Scaffold.php b/scripts/Phalcon/Commands/Builtin/Scaffold.php index 3af401a9a..f499bee95 100644 --- a/scripts/Phalcon/Commands/Builtin/Scaffold.php +++ b/scripts/Phalcon/Commands/Builtin/Scaffold.php @@ -47,7 +47,7 @@ public function getPossibleParams() 'get-set' => 'Attributes will be protected and have setters/getters. [optional]', 'directory=s' => 'Base path on which project was created [optional]', 'template-path=s' => 'Specify a template path [optional]', - 'template-engine=s' => 'Define the template engine, default php (php, volt) [optional]', + 'template-engine=s' => 'Define the template engine, default phtml (phtml, volt) [optional]', 'force' => 'Forces to rewrite generated code if they already exists [optional]', 'trace' => 'Shows the trace of the framework in case of exception [optional]', 'ns-models=s' => "Model's namespace [optional]", @@ -67,7 +67,7 @@ public function run(array $parameters) $name = $this->getOption(['table-name', 1]); $templatePath = $this->getOption(['template-path'], null, TEMPLATE_PATH); $schema = $this->getOption('schema'); - $templateEngine = $this->getOption(['template-engine'], null, "php"); + $templateEngine = $this->getOption(['template-engine'], null, "phtml"); $scaffoldBuilder = new ScaffoldBuilder([ 'name' => $name, diff --git a/templates/project/micro/views/404.volt b/templates/project/micro/views/404.volt new file mode 100644 index 000000000..49345e2e1 --- /dev/null +++ b/templates/project/micro/views/404.volt @@ -0,0 +1,14 @@ + + + + + + Phalcon PHP Framework + + +
+

Not found :(

+ This page is located in views/404.volt +
+ + diff --git a/templates/project/micro/views/index.volt b/templates/project/micro/views/index.volt new file mode 100644 index 000000000..2459aff78 --- /dev/null +++ b/templates/project/micro/views/index.volt @@ -0,0 +1,28 @@ + + + + + + + + Phalcon PHP Framework + + + +
+ + + +

You're now flying with Phalcon. Great things are about to happen!

+ +

This page is located at views/index.volt

+
+ + + + + + + From 6a1f9df09fdd0fc4a24bfc616bd084c464b67fce Mon Sep 17 00:00:00 2001 From: sergeysviridenko Date: Fri, 1 Sep 2017 19:47:27 +0300 Subject: [PATCH 4/4] Changed delimiter in .dat files and improve tests --- scripts/Phalcon/Mvc/Model/Migration.php | 16 ++- .../app/migrations/1.0.0/test_migrations.dat | 1 - .../app/migrations/1.0.1/test_migrations.dat | 1 + .../app/migrations/1.0.1/test_migrations.php | 100 ++++++++++++++++++ tests/console/GenerateMysqlMigrationCept.php | 10 +- tests/console/RunMysqlMigrationCept.php | 28 +++-- 6 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 tests/_data/console/app/migrations/1.0.1/test_migrations.dat create mode 100644 tests/_data/console/app/migrations/1.0.1/test_migrations.php diff --git a/scripts/Phalcon/Mvc/Model/Migration.php b/scripts/Phalcon/Mvc/Model/Migration.php index f7fdea7ae..76faa4ee1 100644 --- a/scripts/Phalcon/Mvc/Model/Migration.php +++ b/scripts/Phalcon/Mvc/Model/Migration.php @@ -440,7 +440,7 @@ public static function generate(ItemInterface $version, $table, $exportData = nu unset($value); } - fputcsv($fileHandler, $data, '|'); + fputcsv($fileHandler, $data); unset($row); unset($data); } @@ -852,7 +852,7 @@ public function batchInsert($tableName, $fields) self::$_connection->begin(); self::$_connection->delete($tableName); $batchHandler = fopen($migrationData, 'r'); - while (($line = fgetcsv($batchHandler, 0, '|')) !== false) { + while (($line = fgetcsv($batchHandler)) !== false) { $values = array_map( function ($value) { return null === $value ? null : $value; @@ -882,9 +882,15 @@ public function batchDelete($tableName) self::$_connection->begin(); self::$_connection->delete($tableName); $batchHandler = fopen($migrationData, 'r'); - while (($line = fgets($batchHandler)) !== false) { - $data = explode('|', rtrim($line), 2); - self::$_connection->delete($tableName, 'id=?', [$data[0]]); + while (($line = fgetcsv($batchHandler)) !== false) { + $values = array_map( + function ($value) { + return null === $value ? null : $value; + }, + $line + ); + + self::$_connection->delete($tableName, 'id=?', [$values[0]]); unset($line); } fclose($batchHandler); diff --git a/tests/_data/console/app/migrations/1.0.0/test_migrations.dat b/tests/_data/console/app/migrations/1.0.0/test_migrations.dat index 54cd9d685..e69de29bb 100644 --- a/tests/_data/console/app/migrations/1.0.0/test_migrations.dat +++ b/tests/_data/console/app/migrations/1.0.0/test_migrations.dat @@ -1 +0,0 @@ -1|test1|"2017-08-29 13:05:41"|"2017-08-29 13:05:41" diff --git a/tests/_data/console/app/migrations/1.0.1/test_migrations.dat b/tests/_data/console/app/migrations/1.0.1/test_migrations.dat new file mode 100644 index 000000000..5870573bc --- /dev/null +++ b/tests/_data/console/app/migrations/1.0.1/test_migrations.dat @@ -0,0 +1 @@ +1,test1,"2017-08-29 13:05:41","2017-08-29 13:05:41" diff --git a/tests/_data/console/app/migrations/1.0.1/test_migrations.php b/tests/_data/console/app/migrations/1.0.1/test_migrations.php new file mode 100644 index 000000000..03bec8026 --- /dev/null +++ b/tests/_data/console/app/migrations/1.0.1/test_migrations.php @@ -0,0 +1,100 @@ +morphTable('test_migrations', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'updated_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '2', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + $this->batchInsert('test_migrations', [ + 'id', + 'name', + 'created_at', + 'updated_at' + ] + ); + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + $this->batchDelete('test_migrations'); + } + +} diff --git a/tests/console/GenerateMysqlMigrationCept.php b/tests/console/GenerateMysqlMigrationCept.php index 2817c563b..3ca5a89b9 100644 --- a/tests/console/GenerateMysqlMigrationCept.php +++ b/tests/console/GenerateMysqlMigrationCept.php @@ -9,17 +9,17 @@ $I->wantToTest('generating migration for MySQL database'); $output=<<amInPath(dirname(app_path())); -$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.php')); -$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); +$I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.php')); +$I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); $I->runShellCommand('phalcon migration --action=generate --migrations=app/migrations --table=test_migrations --data=always --config=app/mysql/config.php --log-in-db'); $I->seeInShellOutput($output); -$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.php')); -$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); +$I->seeFileFound(app_path('migrations/1.0.2/test_migrations.php')); +$I->seeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); diff --git a/tests/console/RunMysqlMigrationCept.php b/tests/console/RunMysqlMigrationCept.php index 603b43e3e..b41af1b2a 100644 --- a/tests/console/RunMysqlMigrationCept.php +++ b/tests/console/RunMysqlMigrationCept.php @@ -8,23 +8,31 @@ $I->wantToTest('Running migration for MySQL database'); -$output=<<amInPath(dirname(app_path())); -$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.php')); -$I->seeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); +$I->seeFileFound(app_path('migrations/1.0.2/test_migrations.php')); +$I->seeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); + +$I->runShellCommand('phalcon migration --action=run --version=1.0.2'); -$I->runShellCommand('phalcon migration --action=run --version=1.0.0'); $I->runShellCommand('phalcon migration --action=run --version=1.0.1'); +$I->seeInShellOutput('Success: Version 1.0.2 was successfully rolled back'); + $I->runShellCommand('phalcon migration --action=run --version=1.0.0'); +$I->seeInShellOutput('Success: Version 1.0.1 was successfully rolled back'); + $I->runShellCommand('phalcon migration --action=run --version=1.0.1'); +$I->seeInShellOutput('Success: Version 1.0.1 was successfully migrated'); + +$I->runShellCommand('phalcon migration --action=run --version=1.0.2'); +$I->seeInShellOutput('Success: Version 1.0.2 was successfully migrated'); -$I->seeInShellOutput($output); -$I->deleteDir(app_path('migrations/1.0.1/')); +$I->deleteDir(app_path('migrations/1.0.2/')); -$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.php')); -$I->dontSeeFileFound(app_path('migrations/1.0.1/test_migrations.dat')); +$I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.php')); +$I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.dat'));