From 8d0165f7337de3770f62a9c6438463bdb3959b09 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Fri, 22 Mar 2019 14:56:03 +0100 Subject: [PATCH 1/9] ignore non SQL migration files, e.g. backup files ignore missing SQL migration files --database option, so that similar databases can be migrated with generic env file. For e.g., testing scripts use environment editor --- Migrate/Command/AbstractEnvCommand.php | 11 +++++++---- Migrate/Command/AddEnvCommand.php | 2 +- Migrate/Command/CreateCommand.php | 15 ++++++++++----- Migrate/Command/DownCommand.php | 10 ++++++++++ Migrate/Command/StatusCommand.php | 11 +++++++++++ Migrate/Command/UpCommand.php | 10 ++++++++++ Migrate/Migration.php | 16 +++++++++------- README.md | 10 +++++++--- composer.json | 8 ++++++-- 9 files changed, 71 insertions(+), 22 deletions(-) diff --git a/Migrate/Command/AbstractEnvCommand.php b/Migrate/Command/AbstractEnvCommand.php index e9b6f6f..994f25c 100644 --- a/Migrate/Command/AbstractEnvCommand.php +++ b/Migrate/Command/AbstractEnvCommand.php @@ -70,7 +70,6 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n $parser = $configLocator->locate($env); $conf = $parser->parse(); - $this->config = $conf; $driver = ArrayUtil::get($conf['connection'], 'driver'); @@ -80,7 +79,9 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n $username = ArrayUtil::get($conf['connection'], 'username'); $password = ArrayUtil::get($conf['connection'], 'password'); $charset = ArrayUtil::get($conf['connection'], 'charset'); - + if (empty($host)) $host="localhost"; + if (empty($dbname)) $dbname=$input->getOption('database'); + if (empty($driver)) $dbname=$input->getOption('driver'); $uri = $driver; if ($driver == 'sqlite') { @@ -111,8 +112,10 @@ public function getLocalMigrations() $migrations = array(); foreach ($fileList as $file) { - $migration = Migration::createFromFile($file, $this->getMigrationDir()); - $migrations[$migration->getId()] = $migration; + if (substr($file,-4)==".sql") { // Skip backup files, etcx + $migration = Migration::createFromFile($file, $this->getMigrationDir()); + $migrations[$migration->getId()] = $migration; + } } ksort($migrations); diff --git a/Migrate/Command/AddEnvCommand.php b/Migrate/Command/AddEnvCommand.php index 5789c66..39d95bc 100644 --- a/Migrate/Command/AddEnvCommand.php +++ b/Migrate/Command/AddEnvCommand.php @@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $driverQuestion = new ChoiceQuestion("Please chose your pdo driver", $drivers); $driver = $questions->ask($input, $output, $driverQuestion); - $dbNameQuestion = new Question("Please enter the database name (or the database file location): ", "~"); + $dbNameQuestion = new Question("Please enter the database name (or the database file location) (if needed): ", "~"); $dbName = $questions->ask($input, $output, $dbNameQuestion); $dbHostQuestion = new Question("Please enter the database host (if needed): ", "~"); diff --git a/Migrate/Command/CreateCommand.php b/Migrate/Command/CreateCommand.php index 051c1ed..0d2892a 100644 --- a/Migrate/Command/CreateCommand.php +++ b/Migrate/Command/CreateCommand.php @@ -31,16 +31,21 @@ protected function execute(InputInterface $input, OutputInterface $output) $descriptionQuestion = new Question("Please enter a description: "); $description = $questions->ask($input, $output, $descriptionQuestion); - - $editorQuestion = new Question("Please chose which editor to use (default vim): ", "vim"); - $questions->ask($input, $output, $editorQuestion); + $editor=getenv("EDITOR"); + if (empty($editor)) $editor="vi"; + $editorQuestion = new Question("Please chose which editor to use (default $editor): ", "$editor"); + $editor=$questions->ask($input, $output, $editorQuestion); $slugger = new Slugify(); $filename = $slugger->slugify($description); $timestamp = str_pad(str_replace(".", "", microtime(true)), 14, "0"); $filename = $timestamp . '_' . $filename . '.sql'; - $templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl'); + if (file_exists($this->getMigrationDir() . '/../' .'migration.tpl')) { + $templateFile = file_get_contents($this->getMigrationDir() . '/../' .'migration.tpl'); + } else { + $templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl'); + } $templateFile = str_replace('{DESCRIPTION}', $description, $templateFile); $migrationFullPath = $this->getMigrationDir() . '/' . $filename; @@ -48,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("$migrationFullPath created"); if (!defined('PHPUNIT')) { - system("vim $migrationFullPath > `tty`"); + system("$editor $migrationFullPath > `tty`"); } } diff --git a/Migrate/Command/DownCommand.php b/Migrate/Command/DownCommand.php index 918c545..decd6ca 100644 --- a/Migrate/Command/DownCommand.php +++ b/Migrate/Command/DownCommand.php @@ -44,6 +44,16 @@ protected function configure() null, InputOption::VALUE_NONE, 'Mark as applied without executing SQL ' + )->addOption( + 'database', + null, + InputOption::VALUE_REQUIRED, + 'Database ' + )->addOption( + 'driver', + null, + InputOption::VALUE_REQUIRED, + 'DB driver' ) ; } diff --git a/Migrate/Command/StatusCommand.php b/Migrate/Command/StatusCommand.php index b4a220b..7d191bb 100644 --- a/Migrate/Command/StatusCommand.php +++ b/Migrate/Command/StatusCommand.php @@ -11,6 +11,7 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -26,6 +27,16 @@ protected function configure() 'env', InputArgument::REQUIRED, 'Environment' + )->addOption( + 'database', + null, + InputOption::VALUE_REQUIRED, + 'Database ' + )->addOption( + 'driver', + null, + InputOption::VALUE_REQUIRED, + 'DB driver' ) ; } diff --git a/Migrate/Command/UpCommand.php b/Migrate/Command/UpCommand.php index 7c42dc2..f646908 100644 --- a/Migrate/Command/UpCommand.php +++ b/Migrate/Command/UpCommand.php @@ -42,6 +42,16 @@ protected function configure() null, InputOption::VALUE_NONE, 'Mark as applied without executing SQL ' + )->addOption( + 'database', + null, + InputOption::VALUE_REQUIRED, + 'Database ' + )->addOption( + 'driver', + null, + InputOption::VALUE_REQUIRED, + 'DB driver' ) ; } diff --git a/Migrate/Migration.php b/Migrate/Migration.php index 1efeef3..b0d9e32 100644 --- a/Migrate/Migration.php +++ b/Migrate/Migration.php @@ -159,7 +159,6 @@ public static function createFromRow(array $data, $migrationDir) $slugger = new Slugify(); $filename = $migration->getId() . '_' . $slugger->slugify($migration->getDescription()) . '.sql'; $migration->setFile($filename); - $migration->load($migrationDir); return $migration; @@ -177,11 +176,14 @@ public function toArray() public function load($migrationDir) { - $content = file_get_contents($migrationDir . '/' . $this->getFile()); - if ($content && strpos($content, '@UNDO') > 0) { - $sql = explode('-- @UNDO', $content); - $this->setSqlUp($sql[0]); - $this->setSqlDown($sql[1]); - } + $filePath=$migrationDir . '/' . $this->getFile(); + if (file_exists($filePath)) { + $content = file_get_contents($filePath); + if ($content && strpos($content, '@UNDO') > 0) { + $sql = explode('-- @UNDO', $content); + $this->setSqlUp($sql[0]); + $this->setSqlDown($sql[1]); + } + } } } diff --git a/README.md b/README.md index ec51786..0f2555e 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,12 @@ The first thing to do before playing with SQL migrations is to add an environmen $ ./bin/migrate migrate:addenv ``` -You will be prompted to answer a series of questions about your environment, and then a config file will be saved -in `./.php-database-migration/environments/[env].yml`. +You will be prompted to answer a series of questions about your +environment, and then a config file will be saved in +`./.php-database-migration/environments/[env].yml`. You can skip all +questions except the databse driver. But if you do not enter a database name, +then you have to suppy it as an option in the following commands ( +--database=foo ). This is useful for e.g. testing scripts that create and copy databases on the fly. Initialization -------------- @@ -81,7 +85,7 @@ Once the environment is added, you have to initialize it. This verifies that the creates a new database table for tracking the current database changes: ``` -$ ./bin/migrate migrate:init [env] +$ ./bin/migrate migrate:init env ``` Create a migration diff --git a/composer.json b/composer.json index e5b17d3..8a32d9b 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "alwex/php-database-migration", + "name": "elgaard/php-database-migration", "description": "rake and mybatis SQL migration tool", - "homepage": "https://github.com/alwex/php-database-migration", + "homepage": "https://github.com/elgaard/php-database-migration", "keywords": ["rake", "mybatis", "migration", "sql"], "type": "library", "license": "MIT", @@ -9,6 +9,10 @@ { "name": "GUIDET Alexandre", "email": "alwex@free.fr" + }, + { + "name": "Niels Elgaard Larsen", + "email": "elgaard@agol.dk" } ], "require": { From a9188b12033f167566a692a637a39b454e55db09 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Fri, 22 Mar 2019 15:00:14 +0100 Subject: [PATCH 2/9] set original urls --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8a32d9b..e0f94df 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "elgaard/php-database-migration", + "name": "alwex/php-database-migration", "description": "rake and mybatis SQL migration tool", - "homepage": "https://github.com/elgaard/php-database-migration", + "homepage": "https://github.com/alwex/php-database-migration", "keywords": ["rake", "mybatis", "migration", "sql"], "type": "library", "license": "MIT", From 83914bb9494b223519db52445b1809125385b716 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Tue, 26 Mar 2019 13:50:09 +0100 Subject: [PATCH 3/9] spell --- Migrate/Command/AddEnvCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Migrate/Command/AddEnvCommand.php b/Migrate/Command/AddEnvCommand.php index 46bb22d..6a7651b 100644 --- a/Migrate/Command/AddEnvCommand.php +++ b/Migrate/Command/AddEnvCommand.php @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \InvalidArgumentException("environment [$envName] is already defined!"); } - $driverQuestion = new ChoiceQuestion("Please chose your pdo driver", $drivers); + $driverQuestion = new ChoiceQuestion("Please choose your pdo driver", $drivers); $driver = $questions->ask($input, $output, $driverQuestion); $dbNameQuestion = From e70c0e88dc5780c8f3d4a15a270e95d6651b76c3 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Tue, 26 Mar 2019 13:50:38 +0100 Subject: [PATCH 4/9] check that sqlite driver exists --- tests/Command/AddenvCommandTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Command/AddenvCommandTest.php b/tests/Command/AddenvCommandTest.php index e603f41..e6e270f 100644 --- a/tests/Command/AddenvCommandTest.php +++ b/tests/Command/AddenvCommandTest.php @@ -36,7 +36,10 @@ public function testExecute() $pdoDrivers = pdo_drivers(); $driverKey = array_search('sqlite', $pdoDrivers); - + if (empty($driverKey)) { + echo "install sqlite php driver (php-sqlite3)\n"; + exit(-1); + } $driverSelect = ''; foreach ($pdoDrivers as $key => $driver) { $driverSelect .= " [$key] $driver\n"; @@ -49,7 +52,7 @@ public function testExecute() $commandTester->execute(array('command' => $command->getName())); $expected = "Please enter the name of the new environment (default dev): Please chose your pdo driver\n$driverSelect > 0\nPlease enter the database name (or the database file location): Please enter the database host (if needed): Please enter the database port (if needed): Please enter the database user name (if needed): Please enter the database user password (if needed): Please enter the changelog table (default changelog): Please enter the text editor to use by default (default vim): "; - + $this->assertRegExp('/Please enter the name of the new environment/', $commandTester->getDisplay()); $envDir = Directory::getEnvPath(); From d1a09ac1341d0445f6c716748128a43c76215558 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Wed, 27 Mar 2019 16:36:08 +0100 Subject: [PATCH 5/9] unit teste for new options --- Migrate/Command/InitCommand.php | 11 ++++++ README.md | 2 +- tests/Command/AbstractCommandTester.php | 12 +++++- tests/Command/UpDownCommandTest.php | 50 ++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Migrate/Command/InitCommand.php b/Migrate/Command/InitCommand.php index 43d1201..35e0400 100644 --- a/Migrate/Command/InitCommand.php +++ b/Migrate/Command/InitCommand.php @@ -8,6 +8,7 @@ namespace Migrate\Command; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -23,6 +24,16 @@ protected function configure() 'env', InputArgument::REQUIRED, 'Environment' + )->addOption( + 'database', + null, + InputOption::VALUE_REQUIRED, + 'Database ' + )->addOption( + 'driver', + null, + InputOption::VALUE_REQUIRED, + 'DB driver' ); } diff --git a/README.md b/README.md index 0f2555e..7eba200 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ $ ./bin/migrate migrate:addenv You will be prompted to answer a series of questions about your environment, and then a config file will be saved in `./.php-database-migration/environments/[env].yml`. You can skip all -questions except the databse driver. But if you do not enter a database name, +questions except the database driver. But if you do not enter a database name, then you have to suppy it as an option in the following commands ( --database=foo ). This is useful for e.g. testing scripts that create and copy databases on the fly. diff --git a/tests/Command/AbstractCommandTester.php b/tests/Command/AbstractCommandTester.php index 67de6c2..be0a52f 100644 --- a/tests/Command/AbstractCommandTester.php +++ b/tests/Command/AbstractCommandTester.php @@ -48,7 +48,9 @@ public function createEnv($format = 'yml') /* @var $question QuestionHelper */ $question = $command->getHelper('question'); $question->setInputStream(InputStreamUtil::type("testing\n$driverKey\ntest.sqlite\n\n\n\n\n\nchangelog\nvim\n")); - + $commandTester->execute(array('command' => $command->getName(), 'format' => $format)); + $question = $command->getHelper('question'); + $question->setInputStream(InputStreamUtil::type("minimal\nsqlite\n\n\n\n\n\n\nchangelog\n\n")); $commandTester->execute(array('command' => $command->getName(), 'format' => $format)); } @@ -64,6 +66,12 @@ public function initEnv() 'command' => $command->getName(), 'env' => 'testing' )); + $commandTester->execute(array( + 'command' => $command->getName(), + 'env' => 'minimal', + '--database' => 'migrate_test', + '--driver' => 'sqlite' + )); } public function createMigration($timestamp, $sqlUp, $sqlDown) @@ -83,4 +91,4 @@ public function createMigration($timestamp, $sqlUp, $sqlDown) file_put_contents($filename, $content); } -} \ No newline at end of file +} diff --git a/tests/Command/UpDownCommandTest.php b/tests/Command/UpDownCommandTest.php index 7fd7d60..aafb3a6 100644 --- a/tests/Command/UpDownCommandTest.php +++ b/tests/Command/UpDownCommandTest.php @@ -104,6 +104,26 @@ public function testUpAllPendingMigrations() 2/3 [==================>---------] 66 % [migration] 3/3 [============================] 100 % [migration] +EXPECTED; + + $this->assertEquals($expected, $commandTester->getDisplay()); + } + + public function testUpAllPendingMigrationsInMinimal() + { + $command = self::$application->find('migrate:up'); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + 'command' => $command->getName(), + 'env' => 'minimal', + '--database' => 'migrate_test', + '--driver' => 'sqlite' + )); + + $expected =<<assertEquals($expected, $commandTester->getDisplay()); @@ -138,6 +158,32 @@ public function testDownLastMigration() Are you sure? (yes/no) [no]: 0/1 [>---------------------------] 0 % [] 1/1 [============================] 100 % [migration] +EXPECTED; + + $this->assertEquals($expected, $commandTester->getDisplay()); + } + + public function testDownLastMigrationInMinimal() + { + + $command = self::$application->find('migrate:down'); + $commandTester = new CommandTester($command); + + /* @var $question QuestionHelper */ + $question = $command->getHelper('question'); + $question->setInputStream(InputStreamUtil::type("yes\n")); + + $commandTester->execute(array( + 'command' => $command->getName(), + 'env' => 'testing', + '--database' => 'migrate_test', + '--driver' => 'sqlite' + )); + + $expected =<<assertEquals($expected, $commandTester->getDisplay()); @@ -147,6 +193,7 @@ public function testUpOnly() { $command = self::$application->find('migrate:up'); $commandTester = new CommandTester($command); + $currentDate = date('Y-m-d H:i:s'); $commandTester->execute(array( 'command' => $command->getName(), @@ -166,7 +213,6 @@ public function testUpOnly() $command = self::$application->find('migrate:status'); $commandTester = new CommandTester($command); - $currentDate = date('Y-m-d H:i:s'); $commandTester->execute(array( 'command' => $command->getName(), @@ -412,4 +458,4 @@ public function testDownInANotInitializedDirectory() '--to' => '1' )); } -} \ No newline at end of file +} From d004aa7403d760463b7c8b71c8d836863072689f Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Fri, 29 Mar 2019 12:23:23 +0100 Subject: [PATCH 6/9] handle off by one second in unit test --- tests/Command/UpDownCommandTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/Command/UpDownCommandTest.php b/tests/Command/UpDownCommandTest.php index aafb3a6..d3f124d 100644 --- a/tests/Command/UpDownCommandTest.php +++ b/tests/Command/UpDownCommandTest.php @@ -303,6 +303,7 @@ public function testDownOnly() public function testUpTo() { + $time0=time(); $command = self::$application->find('migrate:up'); $commandTester = new CommandTester($command); @@ -325,7 +326,7 @@ public function testUpTo() $command = self::$application->find('migrate:status'); $commandTester = new CommandTester($command); - $currentDate = date('Y-m-d H:i:s'); + $currentDate = date('Y-m-d H:i:s',$time0); $commandTester->execute(array( 'command' => $command->getName(), @@ -345,11 +346,14 @@ public function testUpTo() EXPECTED; + $testResult=$commandTester->getDisplay(); + $testResult=str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); $this->assertEquals($expected, $commandTester->getDisplay()); } public function testDownTo() { + $time0=time(); $command = self::$application->find('migrate:up'); $commandTester = new CommandTester($command); @@ -384,12 +388,13 @@ public function testDownTo() $command = self::$application->find('migrate:status'); $commandTester = new CommandTester($command); - $currentDate = date('Y-m-d H:i:s'); + $currentDate = date('Y-m-d H:i:s',$time0); $commandTester->execute(array( 'command' => $command->getName(), 'env' => 'testing' )); + $currentDate = date('Y-m-d H:i:s',$time0); $expected =<<assertEquals($expected, $commandTester->getDisplay()); + $testResult=$commandTester->getDisplay(); + $testResult=str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); + $this->assertEquals($expected, $testResult); } /** From 170d057efce2d798c7b7951b123dbce65a880cc9 Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Tue, 2 Apr 2019 17:12:15 +0200 Subject: [PATCH 7/9] more tests --- tests/Command/AddenvCommandTest.php | 4 +- tests/Command/OptionTest.php | 99 +++++++++++++++++++++++++++++ tests/Command/UpDownCommandTest.php | 47 +------------- 3 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 tests/Command/OptionTest.php diff --git a/tests/Command/AddenvCommandTest.php b/tests/Command/AddenvCommandTest.php index e6e270f..962a1f9 100644 --- a/tests/Command/AddenvCommandTest.php +++ b/tests/Command/AddenvCommandTest.php @@ -44,15 +44,13 @@ public function testExecute() foreach ($pdoDrivers as $key => $driver) { $driverSelect .= " [$key] $driver\n"; } - + echo "drivers: $driverKey \n$driverSelect\n"; /* @var $question QuestionHelper */ $question = $command->getHelper('question'); $question->setInputStream(InputStreamUtil::type("testing\n$driverKey\nmigrate_test\nlocalhost\n5432\naguidet\naguidet\nutf8\nchangelog\nvim\n")); $commandTester->execute(array('command' => $command->getName())); - $expected = "Please enter the name of the new environment (default dev): Please chose your pdo driver\n$driverSelect > 0\nPlease enter the database name (or the database file location): Please enter the database host (if needed): Please enter the database port (if needed): Please enter the database user name (if needed): Please enter the database user password (if needed): Please enter the changelog table (default changelog): Please enter the text editor to use by default (default vim): "; - $this->assertRegExp('/Please enter the name of the new environment/', $commandTester->getDisplay()); $envDir = Directory::getEnvPath(); diff --git a/tests/Command/OptionTest.php b/tests/Command/OptionTest.php new file mode 100644 index 0000000..ae90987 --- /dev/null +++ b/tests/Command/OptionTest.php @@ -0,0 +1,99 @@ +cleanEnv(); + $this->createEnv(); + $this->initEnv(); + + $this->createMigration('0', "CREATE TABLE test (id INTEGER, thevalue TEXT);", "DROP TABLE test;"); + $this->createMigration('1', "SELECT 1", "DELETE FROM test WHERE id = 1;"); + $this->createMigration('2', "INSERT INTO test VALUES (2, 'two');", "DELETE FROM test WHERE id = 2;"); + + self::$application = new Application(); + self::$application->add(new UpCommand()); + self::$application->add(new DownCommand()); + self::$application->add(new StatusCommand()); + } + + public function tearDown() + { + $this->cleanEnv(); + } + + public function testUpAllPendingMigrationsInMinimal() + { + $command = self::$application->find('migrate:up'); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + 'command' => $command->getName(), + 'env' => 'minimal', + '--database' => 'migrate_test', + '--driver' => 'sqlite' + )); + + $expected =<<---------------------------] 0 % [] +1/3 [=========>------------------] 33 % [migration] +2/3 [==================>---------] 66 % [migration] +3/3 [============================] 100 % [migration] + +EXPECTED; + + $this->assertEquals($expected, $commandTester->getDisplay()); + } + + + + public function testDownLastMigrationInMinimal() + { + + $command = self::$application->find('migrate:down'); + $commandTester = new CommandTester($command); + + /* @var $question QuestionHelper */ + $question = $command->getHelper('question'); + $question->setInputStream(InputStreamUtil::type("yes\n")); + + $commandTester->execute(array( + 'command' => $command->getName(), + 'env' => 'testing', + '--database' => 'migrate_test', + '--driver' => 'sqlite' + )); + + $expected =<<assertEquals($expected, $commandTester->getDisplay()); + } + + + + +} diff --git a/tests/Command/UpDownCommandTest.php b/tests/Command/UpDownCommandTest.php index d3f124d..4939a27 100644 --- a/tests/Command/UpDownCommandTest.php +++ b/tests/Command/UpDownCommandTest.php @@ -104,26 +104,6 @@ public function testUpAllPendingMigrations() 2/3 [==================>---------] 66 % [migration] 3/3 [============================] 100 % [migration] -EXPECTED; - - $this->assertEquals($expected, $commandTester->getDisplay()); - } - - public function testUpAllPendingMigrationsInMinimal() - { - $command = self::$application->find('migrate:up'); - $commandTester = new CommandTester($command); - $commandTester->execute(array( - 'command' => $command->getName(), - 'env' => 'minimal', - '--database' => 'migrate_test', - '--driver' => 'sqlite' - )); - - $expected =<<assertEquals($expected, $commandTester->getDisplay()); @@ -158,32 +138,6 @@ public function testDownLastMigration() Are you sure? (yes/no) [no]: 0/1 [>---------------------------] 0 % [] 1/1 [============================] 100 % [migration] -EXPECTED; - - $this->assertEquals($expected, $commandTester->getDisplay()); - } - - public function testDownLastMigrationInMinimal() - { - - $command = self::$application->find('migrate:down'); - $commandTester = new CommandTester($command); - - /* @var $question QuestionHelper */ - $question = $command->getHelper('question'); - $question->setInputStream(InputStreamUtil::type("yes\n")); - - $commandTester->execute(array( - 'command' => $command->getName(), - 'env' => 'testing', - '--database' => 'migrate_test', - '--driver' => 'sqlite' - )); - - $expected =<<assertEquals($expected, $commandTester->getDisplay()); @@ -348,6 +302,7 @@ public function testUpTo() $testResult=$commandTester->getDisplay(); $testResult=str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); + $testResult=str_replace(date('Y-m-d H:i:s',$time0+2),$currentDate,$testResult); $this->assertEquals($expected, $commandTester->getDisplay()); } From 7a93909939c00beaabb4f93b89c7d0c7b32f152c Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Tue, 2 Apr 2019 17:18:45 +0200 Subject: [PATCH 8/9] rm debug output --- tests/Command/AddenvCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Command/AddenvCommandTest.php b/tests/Command/AddenvCommandTest.php index 962a1f9..1a093d4 100644 --- a/tests/Command/AddenvCommandTest.php +++ b/tests/Command/AddenvCommandTest.php @@ -44,7 +44,6 @@ public function testExecute() foreach ($pdoDrivers as $key => $driver) { $driverSelect .= " [$key] $driver\n"; } - echo "drivers: $driverKey \n$driverSelect\n"; /* @var $question QuestionHelper */ $question = $command->getHelper('question'); $question->setInputStream(InputStreamUtil::type("testing\n$driverKey\nmigrate_test\nlocalhost\n5432\naguidet\naguidet\nutf8\nchangelog\nvim\n")); From 72054efd2fd439c698c1a20c75771a5d3d6d683b Mon Sep 17 00:00:00 2001 From: Niels Elgaard Larsen Date: Mon, 8 Apr 2019 14:40:03 +0200 Subject: [PATCH 9/9] whitespace cleanup --- Migrate/Command/AbstractEnvCommand.php | 9 ++++----- tests/Command/UpDownCommandTest.php | 20 ++++++-------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Migrate/Command/AbstractEnvCommand.php b/Migrate/Command/AbstractEnvCommand.php index 1dd8912..d7ed7c1 100644 --- a/Migrate/Command/AbstractEnvCommand.php +++ b/Migrate/Command/AbstractEnvCommand.php @@ -79,16 +79,15 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n $password = ArrayUtil::get($conf['connection'], 'password'); $charset = ArrayUtil::get($conf['connection'], 'charset'); if (empty($host)) { - $host="localhost"; + $host = "localhost"; } if (empty($dbname)) { - $dbname=$input->getOption('database'); + $dbname = $input->getOption('database'); } if (empty($driver)) { - $dbname=$input->getOption('driver'); + $dbname = $input->getOption('driver'); } $uri = $driver; - if ($driver == 'sqlite') { $uri .= ":$dbname"; } else { @@ -117,7 +116,7 @@ public function getLocalMigrations() $migrations = array(); foreach ($fileList as $file) { - if (substr($file, -4)==".sql") { // Skip backup files, etcx + if (substr($file, -4) == ".sql") { // Skip backup files, etc. $migration = Migration::createFromFile($file, $this->getMigrationDir()); $migrations[$migration->getId()] = $migration; } diff --git a/tests/Command/UpDownCommandTest.php b/tests/Command/UpDownCommandTest.php index 4939a27..2f65322 100644 --- a/tests/Command/UpDownCommandTest.php +++ b/tests/Command/UpDownCommandTest.php @@ -257,7 +257,7 @@ public function testDownOnly() public function testUpTo() { - $time0=time(); + $time0 = time(); $command = self::$application->find('migrate:up'); $commandTester = new CommandTester($command); @@ -279,9 +279,7 @@ public function testUpTo() $command = self::$application->find('migrate:status'); $commandTester = new CommandTester($command); - $currentDate = date('Y-m-d H:i:s',$time0); - $commandTester->execute(array( 'command' => $command->getName(), 'env' => 'testing' @@ -300,9 +298,9 @@ public function testUpTo() EXPECTED; - $testResult=$commandTester->getDisplay(); - $testResult=str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); - $testResult=str_replace(date('Y-m-d H:i:s',$time0+2),$currentDate,$testResult); + $testResult = $commandTester->getDisplay(); + $testResult = str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); + $testResult = str_replace(date('Y-m-d H:i:s',$time0+2),$currentDate,$testResult); $this->assertEquals($expected, $commandTester->getDisplay()); } @@ -311,7 +309,6 @@ public function testDownTo() $time0=time(); $command = self::$application->find('migrate:up'); $commandTester = new CommandTester($command); - $commandTester->execute(array( 'command' => $command->getName(), 'env' => 'testing', @@ -339,19 +336,14 @@ public function testDownTo() EXPECTED; $this->assertEquals($expected, $commandTester->getDisplay()); - $command = self::$application->find('migrate:status'); $commandTester = new CommandTester($command); - $currentDate = date('Y-m-d H:i:s',$time0); - $commandTester->execute(array( 'command' => $command->getName(), 'env' => 'testing' )); $currentDate = date('Y-m-d H:i:s',$time0); - - $expected =<<getDisplay(); - $testResult=str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); + $testResult = $commandTester->getDisplay(); + $testResult = str_replace(date('Y-m-d H:i:s',$time0+1),$currentDate,$testResult); $this->assertEquals($expected, $testResult); }