From acb773ed4e8e813273512c0e1790b259a0c3c278 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 14 Nov 2017 10:03:33 +0000 Subject: [PATCH 01/45] Update method to get migration paths from default_ location in config --- src/Phinx/Config/Config.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 01ac25ba4..4338e137c 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -245,15 +245,15 @@ public function getConfigFilePath() */ public function getMigrationPaths() { - if (!isset($this->values['paths']['migrations'])) { + if (!isset($this->values['default_paths']['migrations'])) { throw new \UnexpectedValueException('Migrations path missing from config file'); } - if (is_string($this->values['paths']['migrations'])) { - $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; + if (is_string($this->values['default_paths']['migrations'])) { + $this->values['default_paths']['migrations'] = [$this->values['default_paths']['migrations']]; } - return $this->values['paths']['migrations']; + return $this->values['default_paths']['migrations']; } /** From d708d01fa0bc30cbdec3459e7496b934bdcf44dd Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 14 Nov 2017 15:53:49 +0000 Subject: [PATCH 02/45] Iterate through all of the adapter options and pass the environment through to the manager to discover files --- src/Phinx/Config/Config.php | 10 ++++- src/Phinx/Console/Command/Migrate.php | 63 ++++++++++++++++----------- src/Phinx/Migration/Manager.php | 51 ++++++++++++++++++++-- 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 4338e137c..0bbfe5da1 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -243,16 +243,22 @@ public function getConfigFilePath() /** * {@inheritdoc} */ - public function getMigrationPaths() + public function getMigrationPaths($environment = null, $adapter = null) { if (!isset($this->values['default_paths']['migrations'])) { - throw new \UnexpectedValueException('Migrations path missing from config file'); + throw new \UnexpectedValueException('Default migrations path missing from config file'); } if (is_string($this->values['default_paths']['migrations'])) { $this->values['default_paths']['migrations'] = [$this->values['default_paths']['migrations']]; } + if ($environment !== null && $adapter !== null) { + if (isset($this->values['environments'][$environment][$adapter]['paths']['migrations'])) { + return [$this->values['environments'][$environment][$adapter]['paths']['migrations']]; + } + } + return $this->values['default_paths']['migrations']; } diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 5aef72931..278922cf0 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -70,6 +70,7 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $this->bootstrap($input, $output); $version = $input->getOption('target'); @@ -84,37 +85,49 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); - if (isset($envOptions['adapter'])) { - $output->writeln('using adapter ' . $envOptions['adapter']); - } - if (isset($envOptions['wrapper'])) { - $output->writeln('using wrapper ' . $envOptions['wrapper']); - } - if (isset($envOptions['name'])) { - $output->writeln('using database ' . $envOptions['name']); - } else { - $output->writeln('Could not determine database name! Please specify a database name in your config file.'); + foreach ($envOptions as $adapter => $adapterOptions) { - return 1; - } + if (!is_array($adapterOptions)) { + continue; + } - if (isset($envOptions['table_prefix'])) { - $output->writeln('using table prefix ' . $envOptions['table_prefix']); - } - if (isset($envOptions['table_suffix'])) { - $output->writeln('using table suffix ' . $envOptions['table_suffix']); - } + if (isset($adapterOptions['adapter'])) { + $output->writeln('using adapter ' . $adapterOptions['adapter']); + } + + if (isset($adapterOptions['wrapper'])) { + $output->writeln('using wrapper ' . $adapterOptions['wrapper']); + } + + if (isset($adapterOptions['name'])) { + $output->writeln('using database ' . $adapterOptions['name']); + } else { + $output->writeln('Could not determine database name! Please specify a database name in your config file.'); + + return 1; + } + + if (isset($adapterOptions['table_prefix'])) { + $output->writeln('using table prefix ' . $adapterOptions['table_prefix']); + } + if (isset($adapterOptions['table_suffix'])) { + $output->writeln('using table suffix ' . $adapterOptions['table_suffix']); + } + + // run the migrations + $start = microtime(true); + + $this->getManager()->setEnvironment($environment)->setAdapter($adapter); + if ($date !== null) { + $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); + } else { + $this->getManager()->migrate($environment, $version); + } + $end = microtime(true); - // run the migrations - $start = microtime(true); - if ($date !== null) { - $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); - } else { - $this->getManager()->migrate($environment, $version); } - $end = microtime(true); $output->writeln(''); $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index d7405c226..aad2bc98c 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -59,6 +59,16 @@ class Manager */ protected $environments; + /** + * @var string + */ + protected $environment; + + /** + * @var string + */ + protected $adapter; + /** * @var array */ @@ -550,6 +560,40 @@ public function seed($environment, $seed = null) } } + /** + * Set the adapter property + * + * @param string $adapter + * @return \Phinx\Migration\Manager + */ + public function setAdapter($adapter) + { + $this->adapter = $adapter; + return $this; + } + + /** + * Get the adapter property + * + * @return string + */ + public function getAdapter() + { + return $this->adapter; + } + + /** + * Set the environment property + * + * @param string $environment + * @return \Phinx\Migration\Manager + */ + public function setEnvironment($environment) + { + $this->environmentName = $environment; + return $this; + } + /** * Sets the environments. * @@ -586,10 +630,11 @@ public function getEnvironment($name) // create an environment instance and cache it $envOptions = $this->getConfig()->getEnvironment($name); - $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); + $envOptions = $envOptions[$this->getAdapter()]; + $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); $environment = new Environment($name, $envOptions); - $this->environments[$name] = $environment; + $this->environments[$name][$this->getAdapter()] = $environment; $environment->setInput($this->getInput()); $environment->setOutput($this->getOutput()); @@ -737,7 +782,7 @@ public function getMigrations() protected function getMigrationFiles() { $config = $this->getConfig(); - $paths = $config->getMigrationPaths(); + $paths = $config->getMigrationPaths($this->environmentName, $this->getAdapter()); $files = []; foreach ($paths as $path) { From ef466800424794ce2ef31dcf89cd8bd54fbf599d Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 14 Nov 2017 16:43:04 +0000 Subject: [PATCH 03/45] Rework how migration paths are retrieved pass adaptor executor --- src/Phinx/Config/Config.php | 18 ++++++++++++++++-- src/Phinx/Migration/Manager.php | 7 ++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 0bbfe5da1..d2a3a85dd 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -245,21 +245,35 @@ public function getConfigFilePath() */ public function getMigrationPaths($environment = null, $adapter = null) { + $paths = []; + if (!isset($this->values['default_paths']['migrations'])) { throw new \UnexpectedValueException('Default migrations path missing from config file'); } if (is_string($this->values['default_paths']['migrations'])) { - $this->values['default_paths']['migrations'] = [$this->values['default_paths']['migrations']]; + $paths []= $this->values['default_paths']['migrations']; } if ($environment !== null && $adapter !== null) { if (isset($this->values['environments'][$environment][$adapter]['paths']['migrations'])) { return [$this->values['environments'][$environment][$adapter]['paths']['migrations']]; } + } else { + $environments = array_keys($this->values['environments']); + foreach ($environments as $env) { + if (is_array($this->values['environments'][$env])) { + foreach ($this->values['environments'][$env] as $adapter => $properties) { + if (!is_array($properties)) { + continue; + } + $paths []= $this->values['environments'][$env][$adapter]['paths']['migrations']; + } + } + } } - return $this->values['default_paths']['migrations']; + return $paths; } /** diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index aad2bc98c..c8ddd661d 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -382,7 +382,12 @@ public function executeMigration($name, MigrationInterface $migration, $directio // Execute the migration and log the time elapsed. $start = microtime(true); - $this->getEnvironment($name)->executeMigration($migration, $direction); + + if ($this->getAdapter()) { + $this->getEnvironment($name)[$this->getAdapter()]->executeMigration($migration, $direction); + } else { + $this->getEnvironment($name)->executeMigration($migration, $direction); + } $end = microtime(true); $this->getOutput()->writeln( From 897cd4c9739900a508166196ea6770f0446dc695 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 16 Nov 2017 08:40:26 +0000 Subject: [PATCH 04/45] Get selected environment from Input interface --- src/Phinx/Console/Command/Migrate.php | 2 +- src/Phinx/Migration/Manager.php | 15 ++------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 278922cf0..ba0d810a1 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -119,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // run the migrations $start = microtime(true); - $this->getManager()->setEnvironment($environment)->setAdapter($adapter); + $this->getManager()->setAdapter($adapter); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index c8ddd661d..8987aa93c 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -587,18 +587,6 @@ public function getAdapter() return $this->adapter; } - /** - * Set the environment property - * - * @param string $environment - * @return \Phinx\Migration\Manager - */ - public function setEnvironment($environment) - { - $this->environmentName = $environment; - return $this; - } - /** * Sets the environments. * @@ -787,7 +775,8 @@ public function getMigrations() protected function getMigrationFiles() { $config = $this->getConfig(); - $paths = $config->getMigrationPaths($this->environmentName, $this->getAdapter()); + $environment = $this->getInput()->getOption('environment'); + $paths = $config->getMigrationPaths($environment, $this->getAdapter()); $files = []; foreach ($paths as $path) { From 0dceab495d2ee5c94b6fc1a6b072a72a3724588c Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 16 Nov 2017 12:24:29 +0000 Subject: [PATCH 05/45] Rework to allow db references and use previous adapter references --- src/Phinx/Config/Config.php | 13 +++++++------ src/Phinx/Config/ConfigInterface.php | 4 +++- src/Phinx/Console/Command/Migrate.php | 5 ++--- src/Phinx/Migration/Manager.php | 27 +++++++++++++-------------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index d2a3a85dd..fc3f5009c 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -243,7 +243,7 @@ public function getConfigFilePath() /** * {@inheritdoc} */ - public function getMigrationPaths($environment = null, $adapter = null) + public function getMigrationPaths($environment = null, $dbReference = null) { $paths = []; @@ -255,19 +255,20 @@ public function getMigrationPaths($environment = null, $adapter = null) $paths []= $this->values['default_paths']['migrations']; } - if ($environment !== null && $adapter !== null) { - if (isset($this->values['environments'][$environment][$adapter]['paths']['migrations'])) { - return [$this->values['environments'][$environment][$adapter]['paths']['migrations']]; + if ($environment !== null && $dbReference !== null) { + $environment = $this->getEnvironment($environment); + if (isset($environment[$dbReference]['paths']['migrations'])) { + return [$environment[$dbReference]['paths']['migrations']]; } } else { $environments = array_keys($this->values['environments']); foreach ($environments as $env) { if (is_array($this->values['environments'][$env])) { - foreach ($this->values['environments'][$env] as $adapter => $properties) { + foreach ($this->values['environments'][$env] as $dbReference => $properties) { if (!is_array($properties)) { continue; } - $paths []= $this->values['environments'][$env][$adapter]['paths']['migrations']; + $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } } } diff --git a/src/Phinx/Config/ConfigInterface.php b/src/Phinx/Config/ConfigInterface.php index 1bdae2ed3..59730bc92 100644 --- a/src/Phinx/Config/ConfigInterface.php +++ b/src/Phinx/Config/ConfigInterface.php @@ -91,9 +91,11 @@ public function getConfigFilePath(); /** * Gets the paths to search for migration files. * + * @param string $environment + * @param string $dbReference * @return string[] */ - public function getMigrationPaths(); + public function getMigrationPaths($environment = null, $dbReference = null); /** * Gets the paths to search for seed files. diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index ba0d810a1..56d305bd4 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $envOptions = $this->getConfig()->getEnvironment($environment); - foreach ($envOptions as $adapter => $adapterOptions) { + foreach ($envOptions as $dbReference => $adapterOptions) { if (!is_array($adapterOptions)) { continue; @@ -118,8 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // run the migrations $start = microtime(true); - - $this->getManager()->setAdapter($adapter); + $this->getManager()->setDbRef($dbReference); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 8987aa93c..3f1c55d58 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -382,9 +382,8 @@ public function executeMigration($name, MigrationInterface $migration, $directio // Execute the migration and log the time elapsed. $start = microtime(true); - - if ($this->getAdapter()) { - $this->getEnvironment($name)[$this->getAdapter()]->executeMigration($migration, $direction); + if ($this->getDbRef()) { + $this->getEnvironment($name)->executeMigration($migration, $direction); } else { $this->getEnvironment($name)->executeMigration($migration, $direction); } @@ -566,14 +565,14 @@ public function seed($environment, $seed = null) } /** - * Set the adapter property + * Set the dbRef property * - * @param string $adapter + * @param string $ref * @return \Phinx\Migration\Manager */ - public function setAdapter($adapter) + public function setDbRef($ref) { - $this->adapter = $adapter; + $this->dbRef = $ref; return $this; } @@ -582,9 +581,9 @@ public function setAdapter($adapter) * * @return string */ - public function getAdapter() + public function getDbRef() { - return $this->adapter; + return $this->dbRef; } /** @@ -609,8 +608,8 @@ public function setEnvironments($environments = []) */ public function getEnvironment($name) { - if (isset($this->environments[$name])) { - return $this->environments[$name]; + if (isset($this->environments[$name][$this->getDbRef()])) { + return $this->environments[$name][$this->getDbRef()]; } // check the environment exists @@ -623,11 +622,11 @@ public function getEnvironment($name) // create an environment instance and cache it $envOptions = $this->getConfig()->getEnvironment($name); - $envOptions = $envOptions[$this->getAdapter()]; + $envOptions = $envOptions[$this->getDbRef()]; $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); $environment = new Environment($name, $envOptions); - $this->environments[$name][$this->getAdapter()] = $environment; + $this->environments[$name][$this->getDbRef()] = $environment; $environment->setInput($this->getInput()); $environment->setOutput($this->getOutput()); @@ -776,7 +775,7 @@ protected function getMigrationFiles() { $config = $this->getConfig(); $environment = $this->getInput()->getOption('environment'); - $paths = $config->getMigrationPaths($environment, $this->getAdapter()); + $paths = $config->getMigrationPaths($environment, $this->getDbRef()); $files = []; foreach ($paths as $path) { From 55e2dcaf5718fa4b5bd08b5d28b5f144a661f027 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 16 Nov 2017 13:59:15 +0000 Subject: [PATCH 06/45] Move environment info output to own method --- src/Phinx/Console/Command/Migrate.php | 61 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 56d305bd4..2d2511200 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -86,35 +86,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $envOptions = $this->getConfig()->getEnvironment($environment); - foreach ($envOptions as $dbReference => $adapterOptions) { - if (!is_array($adapterOptions)) { + if (!is_array($adapterOptions)) {//@todo handle old format option continue; } - if (isset($adapterOptions['adapter'])) { - $output->writeln('using adapter ' . $adapterOptions['adapter']); - } - - if (isset($adapterOptions['wrapper'])) { - $output->writeln('using wrapper ' . $adapterOptions['wrapper']); - } - - if (isset($adapterOptions['name'])) { - $output->writeln('using database ' . $adapterOptions['name']); - } else { - $output->writeln('Could not determine database name! Please specify a database name in your config file.'); - - return 1; - } - - if (isset($adapterOptions['table_prefix'])) { - $output->writeln('using table prefix ' . $adapterOptions['table_prefix']); - } - if (isset($adapterOptions['table_suffix'])) { - $output->writeln('using table suffix ' . $adapterOptions['table_suffix']); - } + $this->outputEnvironmentInfo($adapterOptions, $output); // run the migrations $start = microtime(true); @@ -133,4 +111,39 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } + + /** + * Output the database information + * + * @param array $info + * @param OutputInterface $output + * @return null + */ + public function outputEnvironmentInfo(array $info, OutputInterface $output) + { + + if (isset($info['adapter'])) { + $output->writeln('using adapter ' . $info['adapter']); + } + + if (isset($info['wrapper'])) { + $output->writeln('using wrapper ' . $info['wrapper']); + } + + if (isset($info['name'])) { + $output->writeln('using database ' . $info['name']); + } else { + $output->writeln('Could not determine database name! Please specify a database name in your config file.'); + + return 1; + } + + if (isset($info['table_prefix'])) { + $output->writeln('using table prefix ' . $info['table_prefix']); + } + if (isset($info['table_suffix'])) { + $output->writeln('using table suffix ' . $info['table_suffix']); + } + + } } From c3bc5d42cae48ec6e50e760362a6faac8a598a17 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 09:42:35 +0000 Subject: [PATCH 07/45] Enable multi database management for rollback and seed functions --- src/Phinx/Config/Config.php | 32 ++++++++++-- src/Phinx/Console/Command/AbstractCommand.php | 39 ++++++++++++++ src/Phinx/Console/Command/Migrate.php | 46 ++++------------ src/Phinx/Console/Command/Rollback.php | 47 ++++++++--------- src/Phinx/Console/Command/SeedRun.php | 52 ++++++++----------- src/Phinx/Console/Command/Status.php | 10 +++- src/Phinx/Migration/Manager.php | 13 ++--- 7 files changed, 135 insertions(+), 104 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index fc3f5009c..07ad7d7e5 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -293,17 +293,39 @@ public function getMigrationBaseClassName($dropNamespace = true) /** * {@inheritdoc} */ - public function getSeedPaths() + public function getSeedPaths($environment = null, $dbReference = null) { - if (!isset($this->values['paths']['seeds'])) { + + $paths = []; + + if (!isset($this->values['default_paths']['seeds'])) { throw new \UnexpectedValueException('Seeds path missing from config file'); } - if (is_string($this->values['paths']['seeds'])) { - $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; + if (is_string($this->values['default_paths']['seeds'])) { + $path []= $this->values['default_paths']['seeds']; + } + + if ($environment !== null && $dbReference !== null) { + $environment = $this->getEnvironment($environment); + if (isset($environment[$dbReference]['paths']['seeds'])) { + return [$environment[$dbReference]['paths']['seeds']]; + } + } else { + $environments = array_keys($this->values['environments']); + foreach ($environments as $env) { + if (is_array($this->values['environments'][$env])) { + foreach ($this->values['environments'][$env] as $dbReference => $properties) { + if (!is_array($properties)) { + continue; + } + $paths []= $this->values['environments'][$env][$dbReference]['paths']['seeds']; + } + } + } } - return $this->values['paths']['seeds']; + return $paths; } /** diff --git a/src/Phinx/Console/Command/AbstractCommand.php b/src/Phinx/Console/Command/AbstractCommand.php index 49f89585b..9068088aa 100644 --- a/src/Phinx/Console/Command/AbstractCommand.php +++ b/src/Phinx/Console/Command/AbstractCommand.php @@ -343,6 +343,45 @@ protected function verifySeedDirectory($path) } } + /** + * Output the database information + * + * @param array $info + * @param OutputInterface $output + * @return null + */ + public function outputEnvironmentInfo(array $info, OutputInterface $output) + { + + if (isset($info['adapter'])) { + $output->writeln('using adapter ' . $info['adapter']); + } + + if (isset($info['wrapper'])) { + $output->writeln('using wrapper ' . $info['wrapper']); + } + + if (isset($info['id'])) { + $output->writeln('using name ' . $info['id']); + } + + if (isset($info['name'])) { + $output->writeln('using database ' . $info['name']); + } else { + $output->writeln('Could not determine database name! Please specify a database name in your config file.'); + + return 1; + } + + if (isset($info['table_prefix'])) { + $output->writeln('using table prefix ' . $info['table_prefix']); + } + if (isset($info['table_suffix'])) { + $output->writeln('using table suffix ' . $info['table_suffix']); + } + + } + /** * Returns the migration template filename. * diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 2d2511200..c872c294c 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -86,9 +86,17 @@ protected function execute(InputInterface $input, OutputInterface $output) $envOptions = $this->getConfig()->getEnvironment($environment); - foreach ($envOptions as $dbReference => $adapterOptions) { + foreach ($envOptions as $dbRef => $adapterOptions) { if (!is_array($adapterOptions)) {//@todo handle old format option + // run the migrations + $start = microtime(true); + if ($date !== null) { + $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); + } else { + $this->getManager()->migrate($environment, $version); + } + $end = microtime(true); continue; } @@ -96,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // run the migrations $start = microtime(true); - $this->getManager()->setDbRef($dbReference); + $this->getManager()->setDbRef($dbRef); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { @@ -112,38 +120,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - /** - * Output the database information - * - * @param array $info - * @param OutputInterface $output - * @return null - */ - public function outputEnvironmentInfo(array $info, OutputInterface $output) - { - - if (isset($info['adapter'])) { - $output->writeln('using adapter ' . $info['adapter']); - } - - if (isset($info['wrapper'])) { - $output->writeln('using wrapper ' . $info['wrapper']); - } - - if (isset($info['name'])) { - $output->writeln('using database ' . $info['name']); - } else { - $output->writeln('Could not determine database name! Please specify a database name in your config file.'); - - return 1; - } - - if (isset($info['table_prefix'])) { - $output->writeln('using table prefix ' . $info['table_prefix']); - } - if (isset($info['table_suffix'])) { - $output->writeln('using table suffix ' . $info['table_suffix']); - } - - } } diff --git a/src/Phinx/Console/Command/Rollback.php b/src/Phinx/Console/Command/Rollback.php index bf3816292..ca91d9939 100644 --- a/src/Phinx/Console/Command/Rollback.php +++ b/src/Phinx/Console/Command/Rollback.php @@ -96,36 +96,35 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $config->getEnvironment($environment); - if (isset($envOptions['adapter'])) { - $output->writeln('using adapter ' . $envOptions['adapter']); - } - if (isset($envOptions['wrapper'])) { - $output->writeln('using wrapper ' . $envOptions['wrapper']); - } + foreach ($envOptions as $dbRef => $adapterOptions) { - if (isset($envOptions['name'])) { - $output->writeln('using database ' . $envOptions['name']); - } + if (!is_array($adapterOptions)) { + continue; + } - $versionOrder = $this->getConfig()->getVersionOrder(); - $output->writeln('ordering by ' . $versionOrder . " time"); + $this->outputEnvironmentInfo($adapterOptions, $output); - // rollback the specified environment - if ($date === null) { - $targetMustMatchVersion = true; - $target = $version; - } else { - $targetMustMatchVersion = false; - $target = $this->getTargetFromDate($date); - } + $versionOrder = $this->getConfig()->getVersionOrder(); + $output->writeln('ordering by ' . $versionOrder . " time"); + + // rollback the specified environment + if ($date === null) { + $targetMustMatchVersion = true; + $target = $version; + } else { + $targetMustMatchVersion = false; + $target = $this->getTargetFromDate($date); + } - $start = microtime(true); - $this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion); - $end = microtime(true); + $start = microtime(true); + $this->getManager()->setDbRef($dbRef)->rollback($environment, $target, $force, $targetMustMatchVersion); + $end = microtime(true); - $output->writeln(''); - $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + $output->writeln(''); + $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + + } } /** diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index a297dcfc9..4c36ba58c 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -81,44 +81,34 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); - if (isset($envOptions['adapter'])) { - $output->writeln('using adapter ' . $envOptions['adapter']); - } - - if (isset($envOptions['wrapper'])) { - $output->writeln('using wrapper ' . $envOptions['wrapper']); - } - if (isset($envOptions['name'])) { - $output->writeln('using database ' . $envOptions['name']); - } else { - $output->writeln('Could not determine database name! Please specify a database name in your config file.'); + foreach ($envOptions as $dbRef => $adapterOptions) { - return; - } + if (!is_array($adapterOptions)) { + continue; + } + + $this->outputEnvironmentInfo($adapterOptions, $output); - if (isset($envOptions['table_prefix'])) { - $output->writeln('using table prefix ' . $envOptions['table_prefix']); - } - if (isset($envOptions['table_suffix'])) { - $output->writeln('using table suffix ' . $envOptions['table_suffix']); - } + $start = microtime(true); - $start = microtime(true); + $this->getManager()->setDbRef($dbRef); - if (empty($seedSet)) { - // run all the seed(ers) - $this->getManager()->seed($environment); - } else { - // run seed(ers) specified in a comma-separated list of classes - foreach ($seedSet as $seed) { - $this->getManager()->seed($environment, trim($seed)); + if (empty($seedSet)) { + // run all the seed(ers) + $this->getManager()->seed($environment); + } else { + // run seed(ers) specified in a comma-separated list of classes + foreach ($seedSet as $seed) { + $this->getManager()->seed($environment, trim($seed)); + } } - } - $end = microtime(true); + $end = microtime(true); + + $output->writeln(''); + $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); - $output->writeln(''); - $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + } } } diff --git a/src/Phinx/Console/Command/Status.php b/src/Phinx/Console/Command/Status.php index f3bdfb511..a95a5476d 100644 --- a/src/Phinx/Console/Command/Status.php +++ b/src/Phinx/Console/Command/Status.php @@ -84,7 +84,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('ordering by ' . $this->getConfig()->getVersionOrder() . " time"); - // print the status - return $this->getManager()->printStatus($environment, $format); + $envOptions = $this->getConfig()->getEnvironment($environment); + + foreach ($envOptions as $dbRef => $adapterOptions) { + if (!is_array($adapterOptions)) { + continue; + } + $this->getManager()->setDbRef($dbRef)->printStatus($environment, $format); + } } } diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 3f1c55d58..55a80117a 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -382,11 +382,7 @@ public function executeMigration($name, MigrationInterface $migration, $directio // Execute the migration and log the time elapsed. $start = microtime(true); - if ($this->getDbRef()) { - $this->getEnvironment($name)->executeMigration($migration, $direction); - } else { - $this->getEnvironment($name)->executeMigration($migration, $direction); - } + $this->getEnvironment($name)->executeMigration($migration, $direction); $end = microtime(true); $this->getOutput()->writeln( @@ -416,6 +412,7 @@ public function executeSeed($name, SeedInterface $seed) // Execute the seeder and log the time elapsed. $start = microtime(true); $this->getEnvironment($name)->executeSeed($seed); + $end = microtime(true); $this->getOutput()->writeln( @@ -552,12 +549,14 @@ public function seed($environment, $seed = null) foreach ($seeds as $seeder) { if (array_key_exists($seeder->getName(), $seeds)) { $this->executeSeed($environment, $seeder); + unset($this->seeds[$seeder->getName()]); } } } else { // run only one seeder if (array_key_exists($seed, $seeds)) { $this->executeSeed($environment, $seeds[$seed]); + unset($this->seeds[$seed]); } else { throw new \InvalidArgumentException(sprintf('The seed class "%s" does not exist', $seed)); } @@ -867,7 +866,9 @@ public function getSeeds() protected function getSeedFiles() { $config = $this->getConfig(); - $paths = $config->getSeedPaths(); + $environment = $this->getInput()->getOption('environment'); + $paths = $config->getSeedPaths($environment, $this->getDbRef()); + $files = []; foreach ($paths as $path) { From 3f10f4d9dfd62efb4d9fc886248ae61b467e809b Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 10:57:01 +0000 Subject: [PATCH 08/45] Refactor how environment data is retreived for single and multiple databae options --- src/Phinx/Config/Config.php | 2 +- src/Phinx/Console/Command/Migrate.php | 14 ++--- src/Phinx/Migration/Manager.php | 89 +++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 07ad7d7e5..fb0c3a04a 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -260,7 +260,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) if (isset($environment[$dbReference]['paths']['migrations'])) { return [$environment[$dbReference]['paths']['migrations']]; } - } else { + } elseif (is_null($environment) && is_null($dbReference)) { $environments = array_keys($this->values['environments']); foreach ($environments as $env) { if (is_array($this->values['environments'][$env])) { diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index c872c294c..9cf70ad32 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -85,35 +85,35 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); + unset($envOptions['default_migration_table']); + unset($envOptions['default_database']); + // run the migrations + $start = microtime(true); foreach ($envOptions as $dbRef => $adapterOptions) { if (!is_array($adapterOptions)) {//@todo handle old format option - // run the migrations - $start = microtime(true); + $this->outputEnvironmentInfo($envOptions, $output); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { $this->getManager()->migrate($environment, $version); } - $end = microtime(true); - continue; + break; } $this->outputEnvironmentInfo($adapterOptions, $output); - // run the migrations - $start = microtime(true); $this->getManager()->setDbRef($dbRef); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { $this->getManager()->migrate($environment, $version); } - $end = microtime(true); } + $end = microtime(true); $output->writeln(''); $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 55a80117a..e12eff09e 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -607,6 +607,59 @@ public function setEnvironments($environments = []) */ public function getEnvironment($name) { + if ($this->getDbRef() !== null) { + return $this->getMultiEnvironment($name); + } + + return $this->getSingleEnvironment($name); + } + + /** + * Gets the manager class for the given environment when given as a single instance. + * + * @param string $name Environment Name + * @throws \InvalidArgumentException + * @return \Phinx\Migration\Manager\Environment + */ + public function getSingleEnvironment($name) + { + + if (isset($this->environments[$name])) { + return $this->environments[$name]; + } + + // check the environment exists + if (!$this->getConfig()->hasEnvironment($name)) { + throw new \InvalidArgumentException(sprintf( + 'The environment "%s" does not exist', + $name + )); + } + + // create an environment instance and cache it + $envOptions = $this->getConfig()->getEnvironment($name); + + $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); + $environment = new Environment($name, $envOptions); + $this->environments[$name] = $environment; + + $environment->setInput($this->getInput()); + $environment->setOutput($this->getOutput()); + + return $environment; + + } + + /** + * Gets the manager class for the given environment when used as part of a mulitple deployment. + * + * @param string $name Environment Name + * @throws \InvalidArgumentException + * @return \Phinx\Migration\Manager\Environment + */ + public function getMultiEnvironment($name) + { + if (isset($this->environments[$name][$this->getDbRef()])) { return $this->environments[$name][$this->getDbRef()]; } @@ -626,12 +679,48 @@ public function getEnvironment($name) $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); $environment = new Environment($name, $envOptions); $this->environments[$name][$this->getDbRef()] = $environment; + $environment->setInput($this->getInput()); $environment->setOutput($this->getOutput()); return $environment; + } + /** + * Gets the manager class for the given environment. + * + * @param string $name Environment Name + * @throws \InvalidArgumentException + * @return \Phinx\Migration\Manager\Environment + */ + /*public function getEnvironment($name) + { + if (isset($this->environments[$name][$this->getDbRef()])) { + return $this->environments[$name][$this->getDbRef()]; + } + + // check the environment exists + if (!$this->getConfig()->hasEnvironment($name)) { + throw new \InvalidArgumentException(sprintf( + 'The environment "%s" does not exist', + $name + )); + } + + // create an environment instance and cache it + $envOptions = $this->getConfig()->getEnvironment($name); + $envOptions = $envOptions[$this->getDbRef()]; + + $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); + $environment = new Environment($name, $envOptions); + $this->environments[$name][$this->getDbRef()] = $environment; + $environment->setInput($this->getInput()); + $environment->setOutput($this->getOutput()); + + return $environment; + }*/ + /** * Sets the console input. * From 17008b9d7accad797c79896ee61309cea8f5a9ed Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 11:39:54 +0000 Subject: [PATCH 09/45] Fix issue preventing single db seed creation --- src/Phinx/Config/Config.php | 4 ++-- src/Phinx/Console/Command/SeedRun.php | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index fb0c3a04a..9be136f0e 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -303,7 +303,7 @@ public function getSeedPaths($environment = null, $dbReference = null) } if (is_string($this->values['default_paths']['seeds'])) { - $path []= $this->values['default_paths']['seeds']; + $paths[]= $this->values['default_paths']['seeds']; } if ($environment !== null && $dbReference !== null) { @@ -311,7 +311,7 @@ public function getSeedPaths($environment = null, $dbReference = null) if (isset($environment[$dbReference]['paths']['seeds'])) { return [$environment[$dbReference]['paths']['seeds']]; } - } else { + } elseif (is_null($environment) && is_null($dbReference)) { $environments = array_keys($this->values['environments']); foreach ($environments as $env) { if (is_array($this->values['environments'][$env])) { diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index 4c36ba58c..a7ceeef9e 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -81,17 +81,27 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); + $start = microtime(true); foreach ($envOptions as $dbRef => $adapterOptions) { if (!is_array($adapterOptions)) { - continue; + $this->outputEnvironmentInfo($envOptions, $output); + if (empty($seedSet)) { + // run all the seed(ers) + $this->getManager()->seed($environment); + } else { + // run seed(ers) specified in a comma-separated list of classes + foreach ($seedSet as $seed) { + $this->getManager()->seed($environment, trim($seed)); + } + } + + break; } $this->outputEnvironmentInfo($adapterOptions, $output); - $start = microtime(true); - $this->getManager()->setDbRef($dbRef); if (empty($seedSet)) { @@ -104,11 +114,12 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - $end = microtime(true); + } - $output->writeln(''); - $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + $end = microtime(true); + + $output->writeln(''); + $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); - } } } From c855d5e4ae9618b33e446b2fb0272e76568de57d Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 11:48:24 +0000 Subject: [PATCH 10/45] Rollback and status function to work with single databases in env --- src/Phinx/Console/Command/Rollback.php | 36 +++++++++++++++----------- src/Phinx/Console/Command/Status.php | 5 +++- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Phinx/Console/Command/Rollback.php b/src/Phinx/Console/Command/Rollback.php index ca91d9939..42285ecaf 100644 --- a/src/Phinx/Console/Command/Rollback.php +++ b/src/Phinx/Console/Command/Rollback.php @@ -96,18 +96,13 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $config->getEnvironment($environment); - + unset($envOptions['default_migration_table']); + unset($envOptions['default_database']); + + $start = microtime(true); + foreach ($envOptions as $dbRef => $adapterOptions) { - if (!is_array($adapterOptions)) { - continue; - } - - $this->outputEnvironmentInfo($adapterOptions, $output); - - $versionOrder = $this->getConfig()->getVersionOrder(); - $output->writeln('ordering by ' . $versionOrder . " time"); - // rollback the specified environment if ($date === null) { $targetMustMatchVersion = true; @@ -117,14 +112,25 @@ protected function execute(InputInterface $input, OutputInterface $output) $target = $this->getTargetFromDate($date); } - $start = microtime(true); - $this->getManager()->setDbRef($dbRef)->rollback($environment, $target, $force, $targetMustMatchVersion); - $end = microtime(true); + if (!is_array($adapterOptions)) { + $this->outputEnvironmentInfo($envOptions, $output); + $this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion); + break; + } - $output->writeln(''); - $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); + $this->outputEnvironmentInfo($adapterOptions, $output); + + $versionOrder = $this->getConfig()->getVersionOrder(); + $output->writeln('ordering by ' . $versionOrder . " time"); + + $this->getManager()->setDbRef($dbRef)->rollback($environment, $target, $force, $targetMustMatchVersion); } + + $end = microtime(true); + + $output->writeln(''); + $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); } /** diff --git a/src/Phinx/Console/Command/Status.php b/src/Phinx/Console/Command/Status.php index a95a5476d..47a29803d 100644 --- a/src/Phinx/Console/Command/Status.php +++ b/src/Phinx/Console/Command/Status.php @@ -85,10 +85,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('ordering by ' . $this->getConfig()->getVersionOrder() . " time"); $envOptions = $this->getConfig()->getEnvironment($environment); + unset($envOptions['default_migration_table']); + unset($envOptions['default_database']); foreach ($envOptions as $dbRef => $adapterOptions) { if (!is_array($adapterOptions)) { - continue; + $this->getManager()->printStatus($environment, $format); + break; } $this->getManager()->setDbRef($dbRef)->printStatus($environment, $format); } From 847a67eee9f3813eb889eda68767891e8831247c Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 12:15:11 +0000 Subject: [PATCH 11/45] Refactor the envoptions to merge single and multiple environments in to the same format --- src/Phinx/Config/Config.php | 31 ++++++++++++++++++++++++++ src/Phinx/Config/ConfigInterface.php | 8 +++++++ src/Phinx/Console/Command/Rollback.php | 13 +++++------ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 9be136f0e..74e540fdc 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -175,6 +175,37 @@ public function getEnvironment($name) return null; } + /** + * {@inheritdoc} + */ + public function getStorageConfigs(array $envOptions) + { + + $configs = []; + + if (isset($envOptions['default_database'])) { + unset($envOptions['default_database']); + } + + if (isset($envOptions['default_migration_table'])) { + unset($envOptions['default_migration_table']); + } + + if (count($envOptions) > 0) { + foreach ($envOptions as $dbRef => $adapterOptions) { + if (!is_array($adapterOptions)) { + $configs []= $envOptions; + break; + } else { + $configs []= array_merge($adapterOptions, ['dbRef' => $dbRef]); + } + } + } + + return $configs; + + } + /** * {@inheritdoc} */ diff --git a/src/Phinx/Config/ConfigInterface.php b/src/Phinx/Config/ConfigInterface.php index 59730bc92..37ec53df8 100644 --- a/src/Phinx/Config/ConfigInterface.php +++ b/src/Phinx/Config/ConfigInterface.php @@ -56,6 +56,14 @@ public function getEnvironments(); */ public function getEnvironment($name); + /** + * Takes combined envOptions and flattens + * + * @param array $envOptions + * @return array + */ + public function getStorageConfigs(array $envOptions); + /** * Does the specified environment exist in the configuration file? * diff --git a/src/Phinx/Console/Command/Rollback.php b/src/Phinx/Console/Command/Rollback.php index 42285ecaf..d68018464 100644 --- a/src/Phinx/Console/Command/Rollback.php +++ b/src/Phinx/Console/Command/Rollback.php @@ -96,12 +96,11 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $config->getEnvironment($environment); - unset($envOptions['default_migration_table']); - unset($envOptions['default_database']); + $databases = $config->getStorageConfigs($envOptions); $start = microtime(true); - foreach ($envOptions as $dbRef => $adapterOptions) { + foreach ($databases as $adapterOptions) { // rollback the specified environment if ($date === null) { @@ -112,10 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $target = $this->getTargetFromDate($date); } - if (!is_array($adapterOptions)) { - $this->outputEnvironmentInfo($envOptions, $output); - $this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion); - break; + if (isset($adapterOptions['dbRef'])) { + $this->getManager()->setDbRef($adapterOptions['dbRef']); } $this->outputEnvironmentInfo($adapterOptions, $output); @@ -123,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $versionOrder = $this->getConfig()->getVersionOrder(); $output->writeln('ordering by ' . $versionOrder . " time"); - $this->getManager()->setDbRef($dbRef)->rollback($environment, $target, $force, $targetMustMatchVersion); + $this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion); } From b854441ca27b44f15314fd11e4e6e3d5a71ff654 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 17 Nov 2017 12:33:18 +0000 Subject: [PATCH 12/45] Simplfy how the db reference is extracted --- src/Phinx/Console/Command/Migrate.php | 17 +++++------------ src/Phinx/Console/Command/SeedRun.php | 21 +++++---------------- src/Phinx/Console/Command/Status.php | 17 ++++++++++------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 9cf70ad32..333f99743 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -85,26 +85,19 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); - unset($envOptions['default_migration_table']); - unset($envOptions['default_database']); + $databases = $this->getConfig()->getStorageConfigs($envOptions); + // run the migrations $start = microtime(true); - foreach ($envOptions as $dbRef => $adapterOptions) { + foreach ($databases as $adapterOptions) { - if (!is_array($adapterOptions)) {//@todo handle old format option - $this->outputEnvironmentInfo($envOptions, $output); - if ($date !== null) { - $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); - } else { - $this->getManager()->migrate($environment, $version); - } - break; + if (isset($adapterOptions['dbRef'])) { + $this->getManager()->setDbRef($adapterOptions['dbRef']); } $this->outputEnvironmentInfo($adapterOptions, $output); - $this->getManager()->setDbRef($dbRef); if ($date !== null) { $this->getManager()->migrateToDateTime($environment, new \DateTime($date)); } else { diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index a7ceeef9e..cbdedca32 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -81,29 +81,18 @@ protected function execute(InputInterface $input, OutputInterface $output) } $envOptions = $this->getConfig()->getEnvironment($environment); + $databases = $this->getConfig()->getStorageConfigs($envOptions); + $start = microtime(true); - foreach ($envOptions as $dbRef => $adapterOptions) { - - if (!is_array($adapterOptions)) { - $this->outputEnvironmentInfo($envOptions, $output); - if (empty($seedSet)) { - // run all the seed(ers) - $this->getManager()->seed($environment); - } else { - // run seed(ers) specified in a comma-separated list of classes - foreach ($seedSet as $seed) { - $this->getManager()->seed($environment, trim($seed)); - } - } + foreach ($databases as $adapterOptions) { - break; + if (isset($adapterOptions['dbRef'])) { + $this->getManager()->setDbRef($adapterOptions['dbRef']); } $this->outputEnvironmentInfo($adapterOptions, $output); - $this->getManager()->setDbRef($dbRef); - if (empty($seedSet)) { // run all the seed(ers) $this->getManager()->seed($environment); diff --git a/src/Phinx/Console/Command/Status.php b/src/Phinx/Console/Command/Status.php index 47a29803d..c8b22226c 100644 --- a/src/Phinx/Console/Command/Status.php +++ b/src/Phinx/Console/Command/Status.php @@ -85,15 +85,18 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('ordering by ' . $this->getConfig()->getVersionOrder() . " time"); $envOptions = $this->getConfig()->getEnvironment($environment); - unset($envOptions['default_migration_table']); - unset($envOptions['default_database']); + $databases = $this->getConfig()->getStorageConfigs($envOptions); - foreach ($envOptions as $dbRef => $adapterOptions) { - if (!is_array($adapterOptions)) { - $this->getManager()->printStatus($environment, $format); - break; + // run the migrations + $start = microtime(true); + + foreach ($databases as $adapterOptions) { + + if (isset($adapterOptions['dbRef'])) { + $this->getManager()->setDbRef($adapterOptions['dbRef']); } - $this->getManager()->setDbRef($dbRef)->printStatus($environment, $format); + + $this->getManager()->printStatus($environment, $format); } } } From 3a00e7f9690b79ab987da2354ed7423ceedd4e76 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 09:18:00 +0000 Subject: [PATCH 13/45] Store environment value as property rather than relying on access from InputInterface --- src/Phinx/Migration/Manager.php | 55 ++++++++++----------------------- 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index e12eff09e..581f3f930 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -62,7 +62,7 @@ class Manager /** * @var string */ - protected $environment; + protected $environmentName; /** * @var string @@ -312,6 +312,7 @@ public function migrateToDateTime($environment, \DateTime $dateTime) */ public function migrate($environment, $version = null) { + $this->setEnvironmentName($environmentName); $migrations = $this->getMigrations(); $env = $this->getEnvironment($environment); $versions = $env->getVersions(); @@ -542,6 +543,7 @@ public function rollback($environment, $target = null, $force = false, $targetMu */ public function seed($environment, $seed = null) { + $this->setEnvironmentName($environment); $seeds = $this->getSeeds(); if ($seed === null) { @@ -614,6 +616,17 @@ public function getEnvironment($name) return $this->getSingleEnvironment($name); } + public function setEnvironmentName($name) + { + $this->environmentName = $name; + return $this; + } + + public function getEnvironmentName() + { + return $this->environmentName; + } + /** * Gets the manager class for the given environment when given as a single instance. * @@ -687,40 +700,6 @@ public function getMultiEnvironment($name) } - /** - * Gets the manager class for the given environment. - * - * @param string $name Environment Name - * @throws \InvalidArgumentException - * @return \Phinx\Migration\Manager\Environment - */ - /*public function getEnvironment($name) - { - if (isset($this->environments[$name][$this->getDbRef()])) { - return $this->environments[$name][$this->getDbRef()]; - } - - // check the environment exists - if (!$this->getConfig()->hasEnvironment($name)) { - throw new \InvalidArgumentException(sprintf( - 'The environment "%s" does not exist', - $name - )); - } - - // create an environment instance and cache it - $envOptions = $this->getConfig()->getEnvironment($name); - $envOptions = $envOptions[$this->getDbRef()]; - - $envOptions['version_order'] = $this->getConfig()->getVersionOrder(); - $environment = new Environment($name, $envOptions); - $this->environments[$name][$this->getDbRef()] = $environment; - $environment->setInput($this->getInput()); - $environment->setOutput($this->getOutput()); - - return $environment; - }*/ - /** * Sets the console input. * @@ -862,8 +841,7 @@ public function getMigrations() protected function getMigrationFiles() { $config = $this->getConfig(); - $environment = $this->getInput()->getOption('environment'); - $paths = $config->getMigrationPaths($environment, $this->getDbRef()); + $paths = $config->getMigrationPaths($this->getEnvironmentName(), $this->getDbRef()); $files = []; foreach ($paths as $path) { @@ -955,8 +933,7 @@ public function getSeeds() protected function getSeedFiles() { $config = $this->getConfig(); - $environment = $this->getInput()->getOption('environment'); - $paths = $config->getSeedPaths($environment, $this->getDbRef()); + $paths = $config->getSeedPaths($this->getEnvironmentName(), $this->getDbRef()); $files = []; From 8608ccda279ce8ce0d93db65c67d5ed345d88145 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 09:23:38 +0000 Subject: [PATCH 14/45] Revery labelling of default paths to original format for backwards compatibility --- src/Phinx/Config/Config.php | 12 ++++++------ tests/Phinx/Migration/ManagerTest.php | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 74e540fdc..31747aab2 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -278,12 +278,12 @@ public function getMigrationPaths($environment = null, $dbReference = null) { $paths = []; - if (!isset($this->values['default_paths']['migrations'])) { + if (!isset($this->values['paths']['migrations'])) { throw new \UnexpectedValueException('Default migrations path missing from config file'); } - if (is_string($this->values['default_paths']['migrations'])) { - $paths []= $this->values['default_paths']['migrations']; + if (is_string($this->values['paths']['migrations'])) { + $paths []= $this->values['paths']['migrations']; } if ($environment !== null && $dbReference !== null) { @@ -329,12 +329,12 @@ public function getSeedPaths($environment = null, $dbReference = null) $paths = []; - if (!isset($this->values['default_paths']['seeds'])) { + if (!isset($this->values['paths']['seeds'])) { throw new \UnexpectedValueException('Seeds path missing from config file'); } - if (is_string($this->values['default_paths']['seeds'])) { - $paths[]= $this->values['default_paths']['seeds']; + if (is_string($this->values['paths']['seeds'])) { + $paths[]= $this->values['paths']['seeds']; } if ($environment !== null && $dbReference !== null) { diff --git a/tests/Phinx/Migration/ManagerTest.php b/tests/Phinx/Migration/ManagerTest.php index d9ff44610..ff8a5a787 100644 --- a/tests/Phinx/Migration/ManagerTest.php +++ b/tests/Phinx/Migration/ManagerTest.php @@ -43,10 +43,10 @@ protected function getConfigWithNamespace($paths = []) { if (empty($paths)) { $paths = [ - 'migrations' => [ + 'default_migrations' => [ 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/migrations'), ], - 'seeds' => [ + 'default_seeds' => [ 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/seeds'), ], ]; @@ -61,12 +61,12 @@ protected function getConfigWithMixedNamespace($paths = []) { if (empty($paths)) { $paths = [ - 'migrations' => [ + 'default_migrations' => [ $this->getCorrectedPath(__DIR__ . '/_files/migrations'), 'Baz' => $this->getCorrectedPath(__DIR__ . '/_files_baz/migrations'), 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/migrations'), ], - 'seeds' => [ + 'default_seeds' => [ $this->getCorrectedPath(__DIR__ . '/_files/seeds'), 'Baz' => $this->getCorrectedPath(__DIR__ . '/_files_baz/seeds'), 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/seeds'), From 4afef22139a6f8fca864675cfc94aec485160ce8 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 10:21:17 +0000 Subject: [PATCH 15/45] Fix test error for output --- src/Phinx/Config/Config.php | 16 +++++++++------- tests/Phinx/Migration/ManagerTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 31747aab2..666f6a27b 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -292,14 +292,16 @@ public function getMigrationPaths($environment = null, $dbReference = null) return [$environment[$dbReference]['paths']['migrations']]; } } elseif (is_null($environment) && is_null($dbReference)) { - $environments = array_keys($this->values['environments']); - foreach ($environments as $env) { - if (is_array($this->values['environments'][$env])) { - foreach ($this->values['environments'][$env] as $dbReference => $properties) { - if (!is_array($properties)) { - continue; + if (is_array($this->values['environments'])) { + $environments = array_keys($this->values['environments']); + foreach ($environments as $env) { + if (is_array($this->values['environments'][$env])) { + foreach ($this->values['environments'][$env] as $dbReference => $properties) { + if (!is_array($properties)) { + continue; + } + $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } - $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } } } diff --git a/tests/Phinx/Migration/ManagerTest.php b/tests/Phinx/Migration/ManagerTest.php index ff8a5a787..d9ff44610 100644 --- a/tests/Phinx/Migration/ManagerTest.php +++ b/tests/Phinx/Migration/ManagerTest.php @@ -43,10 +43,10 @@ protected function getConfigWithNamespace($paths = []) { if (empty($paths)) { $paths = [ - 'default_migrations' => [ + 'migrations' => [ 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/migrations'), ], - 'default_seeds' => [ + 'seeds' => [ 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/seeds'), ], ]; @@ -61,12 +61,12 @@ protected function getConfigWithMixedNamespace($paths = []) { if (empty($paths)) { $paths = [ - 'default_migrations' => [ + 'migrations' => [ $this->getCorrectedPath(__DIR__ . '/_files/migrations'), 'Baz' => $this->getCorrectedPath(__DIR__ . '/_files_baz/migrations'), 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/migrations'), ], - 'default_seeds' => [ + 'seeds' => [ $this->getCorrectedPath(__DIR__ . '/_files/seeds'), 'Baz' => $this->getCorrectedPath(__DIR__ . '/_files_baz/seeds'), 'Foo\Bar' => $this->getCorrectedPath(__DIR__ . '/_files_foo_bar/seeds'), From c1a8a3b11712c0049eeba1265409e022527a2766 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 16:39:02 +0000 Subject: [PATCH 16/45] Refactor getting the seed and migration paths --- src/Phinx/Config/Config.php | 15 +++++++++++---- src/Phinx/Console/Command/Migrate.php | 3 ++- src/Phinx/Migration/Manager.php | 5 ++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 666f6a27b..fd32c861a 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -283,7 +283,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) } if (is_string($this->values['paths']['migrations'])) { - $paths []= $this->values['paths']['migrations']; + $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; } if ($environment !== null && $dbReference !== null) { @@ -294,6 +294,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) } elseif (is_null($environment) && is_null($dbReference)) { if (is_array($this->values['environments'])) { $environments = array_keys($this->values['environments']); + foreach ($environments as $env) { if (is_array($this->values['environments'][$env])) { foreach ($this->values['environments'][$env] as $dbReference => $properties) { @@ -304,10 +305,13 @@ public function getMigrationPaths($environment = null, $dbReference = null) } } } + if (count($paths) > 0) { + $this->values['paths']['migrations']= $paths; + } } } - return $paths; + return $this->values['paths']['migrations']; } /** @@ -336,7 +340,7 @@ public function getSeedPaths($environment = null, $dbReference = null) } if (is_string($this->values['paths']['seeds'])) { - $paths[]= $this->values['paths']['seeds']; + $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; } if ($environment !== null && $dbReference !== null) { @@ -356,9 +360,12 @@ public function getSeedPaths($environment = null, $dbReference = null) } } } + if (count($paths) > 0) { + $this->values['paths']['seeds']= $paths; + } } - return $paths; + return $this->values['paths']['seeds']; } /** diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index 333f99743..c2832bab6 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -89,9 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output) // run the migrations $start = microtime(true); - foreach ($databases as $adapterOptions) { + $this->getManager()->setMigrations(null); + if (isset($adapterOptions['dbRef'])) { $this->getManager()->setDbRef($adapterOptions['dbRef']); } diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 581f3f930..7fd792216 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -312,7 +312,7 @@ public function migrateToDateTime($environment, \DateTime $dateTime) */ public function migrate($environment, $version = null) { - $this->setEnvironmentName($environmentName); + $this->setEnvironmentName($environment); $migrations = $this->getMigrations(); $env = $this->getEnvironment($environment); $versions = $env->getVersions(); @@ -752,7 +752,7 @@ public function getOutput() * @param array $migrations Migrations * @return \Phinx\Migration\Manager */ - public function setMigrations(array $migrations) + public function setMigrations(array $migrations = null) { $this->migrations = $migrations; @@ -770,7 +770,6 @@ public function getMigrations() { if ($this->migrations === null) { $phpFiles = $this->getMigrationFiles(); - // filter the files to only get the ones that match our naming scheme $fileNames = []; /** @var \Phinx\Migration\AbstractMigration[] $versions */ From 6d2dccb41268b32150057e19c2c8c923ad99d219 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 16:45:20 +0000 Subject: [PATCH 17/45] Update test to get full config for environment --- tests/Phinx/Config/ConfigSeedPathsTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/Phinx/Config/ConfigSeedPathsTest.php b/tests/Phinx/Config/ConfigSeedPathsTest.php index 043212d15..74cd45d7d 100644 --- a/tests/Phinx/Config/ConfigSeedPathsTest.php +++ b/tests/Phinx/Config/ConfigSeedPathsTest.php @@ -32,13 +32,8 @@ public function testGetSeedPaths() public function testGetSeedPathConvertsStringToArray() { - $values = [ - 'paths' => [ - 'seeds' => '/test' - ] - ]; - - $config = new Config($values); + + $config = new Config($this->getConfigArray()); $paths = $config->getSeedPaths(); $this->assertTrue(is_array($paths)); From e80fc7d05a6c0f768d1c57c2a7d70a72041cfc48 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Tue, 21 Nov 2017 16:51:32 +0000 Subject: [PATCH 18/45] Make sure the environments param is an array before getting keys --- src/Phinx/Config/Config.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index fd32c861a..af011bdf6 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -349,14 +349,16 @@ public function getSeedPaths($environment = null, $dbReference = null) return [$environment[$dbReference]['paths']['seeds']]; } } elseif (is_null($environment) && is_null($dbReference)) { - $environments = array_keys($this->values['environments']); - foreach ($environments as $env) { - if (is_array($this->values['environments'][$env])) { - foreach ($this->values['environments'][$env] as $dbReference => $properties) { - if (!is_array($properties)) { - continue; + if (is_array($this->values['environments'])) { + $environments = array_keys($this->values['environments']); + foreach ($environments as $env) { + if (is_array($this->values['environments'][$env])) { + foreach ($this->values['environments'][$env] as $dbReference => $properties) { + if (!is_array($properties)) { + continue; + } + $paths []= $this->values['environments'][$env][$dbReference]['paths']['seeds']; } - $paths []= $this->values['environments'][$env][$dbReference]['paths']['seeds']; } } } From 9771ca280ad59fa7b853d0ea5d7661b11a4e176f Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Wed, 22 Nov 2017 15:38:06 +0000 Subject: [PATCH 19/45] Fix erroring tests --- tests/Phinx/Console/Command/MigrateTest.php | 9 ++++----- tests/Phinx/Console/Command/RollbackTest.php | 6 +++--- tests/Phinx/Console/Command/SeedRunTest.php | 4 ++-- tests/Phinx/Console/Command/StatusTest.php | 6 +++--- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/Phinx/Console/Command/MigrateTest.php b/tests/Phinx/Console/Command/MigrateTest.php index fd2b96d1d..a604ae78b 100644 --- a/tests/Phinx/Console/Command/MigrateTest.php +++ b/tests/Phinx/Console/Command/MigrateTest.php @@ -47,7 +47,7 @@ protected function setUp() 'user' => '', 'pass' => '', 'port' => 3006, - ] + ], ] ]); @@ -96,15 +96,14 @@ public function testExecuteWithEnvironmentOption() ->getMock(); $managerStub->expects($this->any()) ->method('migrate'); - $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); - $exitCode = $commandTester->execute(['command' => $command->getName(), '--environment' => 'fakeenv'], ['decorated' => false]); + $exitCode = $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); - $this->assertRegExp('/using environment fakeenv/', $commandTester->getDisplay()); - $this->assertSame(1, $exitCode); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + $this->assertSame(0, $exitCode); } public function testDatabaseNameSpecified() diff --git a/tests/Phinx/Console/Command/RollbackTest.php b/tests/Phinx/Console/Command/RollbackTest.php index d0a6c9471..d65536bfc 100644 --- a/tests/Phinx/Console/Command/RollbackTest.php +++ b/tests/Phinx/Console/Command/RollbackTest.php @@ -106,14 +106,14 @@ public function testExecuteWithEnvironmentOption() ->getMock(); $managerStub->expects($this->once()) ->method('rollback') - ->with('fakeenv', null, false, true); + ->with('development', null, false, true); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); - $commandTester->execute(['command' => $command->getName(), '--environment' => 'fakeenv'], ['decorated' => false]); - $this->assertRegExp('/using environment fakeenv/', $commandTester->getDisplay()); + $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } public function testDatabaseNameSpecified() diff --git a/tests/Phinx/Console/Command/SeedRunTest.php b/tests/Phinx/Console/Command/SeedRunTest.php index 733fd49e3..0aa0526b8 100644 --- a/tests/Phinx/Console/Command/SeedRunTest.php +++ b/tests/Phinx/Console/Command/SeedRunTest.php @@ -101,8 +101,8 @@ public function testExecuteWithEnvironmentOption() $command->setManager($managerStub); $commandTester = new CommandTester($command); - $commandTester->execute(['command' => $command->getName(), '--environment' => 'fakeenv'], ['decorated' => false]); - $this->assertRegExp('/using environment fakeenv/', $commandTester->getDisplay()); + $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } public function testDatabaseNameSpecified() diff --git a/tests/Phinx/Console/Command/StatusTest.php b/tests/Phinx/Console/Command/StatusTest.php index 7269572a5..712fc497f 100644 --- a/tests/Phinx/Console/Command/StatusTest.php +++ b/tests/Phinx/Console/Command/StatusTest.php @@ -108,16 +108,16 @@ public function testExecuteWithEnvironmentOption() ->getMock(); $managerStub->expects($this->once()) ->method('printStatus') - ->with('fakeenv', null) + ->with('development', null) ->will($this->returnValue(0)); $command->setConfig($this->config); $command->setManager($managerStub); $commandTester = new CommandTester($command); - $return = $commandTester->execute(['command' => $command->getName(), '--environment' => 'fakeenv'], ['decorated' => false]); + $return = $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); $this->assertEquals(0, $return); - $this->assertRegExp('/using environment fakeenv/', $commandTester->getDisplay()); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } public function testFormatSpecified() From 570b600d665790887f1bcd73f5f3150a772742aa Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Wed, 22 Nov 2017 16:42:00 +0000 Subject: [PATCH 20/45] Test migration with multienv config --- tests/Phinx/Console/Command/MigrateTest.php | 71 ++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/tests/Phinx/Console/Command/MigrateTest.php b/tests/Phinx/Console/Command/MigrateTest.php index a604ae78b..f1622c764 100644 --- a/tests/Phinx/Console/Command/MigrateTest.php +++ b/tests/Phinx/Console/Command/MigrateTest.php @@ -33,6 +33,15 @@ class MigrateTest extends \PHPUnit_Framework_TestCase protected function setUp() { + + $this->getConfig(); + $this->input = new ArrayInput([]); + $this->output = new StreamOutput(fopen('php://memory', 'a', false)); + } + + public function getConfig() + { + $this->config = new Config([ 'paths' => [ 'migrations' => __FILE__, @@ -51,8 +60,41 @@ protected function setUp() ] ]); - $this->input = new ArrayInput([]); - $this->output = new StreamOutput(fopen('php://memory', 'a', false)); + } + + public function getMultiDbConfig() + { + + $config = new Config([ + 'paths' => [ + 'migrations' => __FILE__, + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + 'db2' => [ + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development_2', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ] + ]); + + return $config; + } public function testExecute() @@ -81,6 +123,31 @@ public function testExecute() $this->assertSame(0, $exitCode); } + public function testExecuteWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Migrate()); + + /** @var Migrate $command */ + $command = $application->find('migrate'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->config, $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->any()) + ->method('migrate'); + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $exitCode = $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + $this->assertSame(0, $exitCode); + } + public function testExecuteWithEnvironmentOption() { $application = new PhinxApplication('testing'); From 57b624aea5dc8fd700c12c43cbbc123b1cf454a5 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 10:25:09 +0000 Subject: [PATCH 21/45] Fix namespace issue when creating migrations --- src/Phinx/Config/Config.php | 11 +- tests/Phinx/Console/Command/CreateTest.php | 115 +++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index af011bdf6..c251ba2b1 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -285,7 +285,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) if (is_string($this->values['paths']['migrations'])) { $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; } - + if ($environment !== null && $dbReference !== null) { $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['migrations'])) { @@ -301,7 +301,14 @@ public function getMigrationPaths($environment = null, $dbReference = null) if (!is_array($properties)) { continue; } - $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; + + if (!is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { + $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; + } else { + foreach ($this->values['environments'][$env][$dbReference]['paths']['migrations'] as $namespace => $migrationPath) { + $paths[$namespace]= $migrationPath; + } + } } } } diff --git a/tests/Phinx/Console/Command/CreateTest.php b/tests/Phinx/Console/Command/CreateTest.php index 50525b0e3..0c417a882 100644 --- a/tests/Phinx/Console/Command/CreateTest.php +++ b/tests/Phinx/Console/Command/CreateTest.php @@ -25,6 +25,8 @@ class CreateTest extends \PHPUnit_Framework_TestCase */ protected $config = []; + protected $configMultiDb = []; + /** * @var InputInterface $input */ @@ -64,6 +66,39 @@ protected function setUp() } } + @mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . '/db1/migrations', 0777, true); + $this->configMultiDb = new Config( + [ + 'paths' => [ + 'migrations' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'migrations', + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'paths' => [ + 'migrations' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ], + ] + ); + + foreach ($this->configMultiDb->getMigrationPaths() as $path) { + foreach (glob($path . '/*.*') as $migration) { + unlink($migration); + } + } + $this->input = new ArrayInput([]); $this->output = new StreamOutput(fopen('php://memory', 'a', false)); } @@ -94,6 +129,32 @@ public function testExecuteWithDuplicateMigrationNames() $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The migration class name "MyDuplicateMigration" already exists + */ + public function testExecuteWithDuplicateMigrationNamesWithMultDbConfig() + { + $application = new PhinxApplication('testing'); + $application->add(new Create()); + + /** @var Create $command */ + $command = $application->find('create'); + + /** @var Manager $managerStub mock the manager class */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->configMultiDb, $this->input, $this->output]) + ->getMock(); + + $command->setConfig($this->configMultiDb); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); + sleep(1.01); // need at least a second due to file naming scheme + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The migration class name "Foo\Bar\MyDuplicateMigration" already exists @@ -126,6 +187,60 @@ public function testExecuteWithDuplicateMigrationNamesWithNamespace() $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The migration class name "Foo\Bar\MyDuplicateMigration" already exists + */ + public function testExecuteWithDuplicateMigrationNamesWithNamespaceWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Create()); + + /** @var Create $command */ + $command = $application->find('create'); + + /** @var Manager $managerStub mock the manager class */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->configMultiDb, $this->input, $this->output]) + ->getMock(); + + $config = new Config( + [ + 'paths' => [ + 'migrations' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'paths' => [ + 'migrations' => [ + 'Foo\\Bar' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', + ], + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ], + ] + ); + + $command->setConfig($config); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); + sleep(1.01); // need at least a second due to file naming scheme + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateMigration']); + } + /** * @expectedException \Exception * @expectedExceptionMessage Cannot use --template and --class at the same time From f5b2980e60265607d96b804d522edfe1e9d721b6 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 11:42:43 +0000 Subject: [PATCH 22/45] Fix seed path with namespaces --- src/Phinx/Config/Config.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index c251ba2b1..d48e2310e 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -285,7 +285,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) if (is_string($this->values['paths']['migrations'])) { $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; } - + if ($environment !== null && $dbReference !== null) { $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['migrations'])) { @@ -364,7 +364,14 @@ public function getSeedPaths($environment = null, $dbReference = null) if (!is_array($properties)) { continue; } - $paths []= $this->values['environments'][$env][$dbReference]['paths']['seeds']; + + if (!is_array($this->values['environments'][$env][$dbReference]['paths']['seeds'])) { + $paths []= $this->values['environments'][$env][$dbReference]['paths']['seeds']; + } else { + foreach ($this->values['environments'][$env][$dbReference]['paths']['seeds'] as $namespace => $migrationPath) { + $paths[$namespace]= $migrationPath; + } + } } } } From 120b3b46101e248ecbc2f5f15052b903c2333b5b Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 12:05:10 +0000 Subject: [PATCH 23/45] MultiDb tests added to test migration creation --- tests/Phinx/Console/Command/CreateTest.php | 38 +++++++++------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/tests/Phinx/Console/Command/CreateTest.php b/tests/Phinx/Console/Command/CreateTest.php index 0c417a882..cdef195ff 100644 --- a/tests/Phinx/Console/Command/CreateTest.php +++ b/tests/Phinx/Console/Command/CreateTest.php @@ -204,33 +204,25 @@ public function testExecuteWithDuplicateMigrationNamesWithNamespaceWithMultiDb() ->setConstructorArgs([$this->configMultiDb, $this->input, $this->output]) ->getMock(); - $config = new Config( - [ - 'paths' => [ - 'migrations' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', - ], - 'environments' => [ - 'default_migration_table' => 'phinxlog', - 'default_database' => 'development', - 'development' => [ - 'db1' => [ - 'paths' => [ - 'migrations' => [ - 'Foo\\Bar' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', - ], - 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', - ], - 'adapter' => 'mysql', - 'host' => 'fakehost', - 'name' => 'development', - 'user' => '', - 'pass' => '', - 'port' => 3006, + $config = clone $this->configMultiDb; + $config['environments']= [ + 'development' => [ + 'db1' => [ + 'paths' => [ + 'migrations' => [ + 'Foo\\Bar' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', ], + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'migrations', ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, ], ] - ); + ]; $command->setConfig($config); $command->setManager($managerStub); From 88f214c40efe942251ae7832e9f8436692cafbd6 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 12:11:04 +0000 Subject: [PATCH 24/45] Implement tests to check multiDb config for migrations --- tests/Phinx/Console/Command/MigrateTest.php | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/Phinx/Console/Command/MigrateTest.php b/tests/Phinx/Console/Command/MigrateTest.php index f1622c764..b27941775 100644 --- a/tests/Phinx/Console/Command/MigrateTest.php +++ b/tests/Phinx/Console/Command/MigrateTest.php @@ -173,6 +173,31 @@ public function testExecuteWithEnvironmentOption() $this->assertSame(0, $exitCode); } + public function testExecuteWithEnvironmentOptionWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Migrate()); + + /** @var Migrate $command */ + $command = $application->find('migrate'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->any()) + ->method('migrate'); + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $exitCode = $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + $this->assertSame(0, $exitCode); + } + public function testDatabaseNameSpecified() { $application = new PhinxApplication('testing'); @@ -198,4 +223,30 @@ public function testDatabaseNameSpecified() $this->assertRegExp('/using database development/', $commandTester->getDisplay()); $this->assertSame(0, $exitCode); } + + public function testDatabaseNameSpecifiedWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Migrate()); + + /** @var Migrate $command */ + $command = $application->find('migrate'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('migrate'); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $exitCode = $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + + $this->assertRegExp('/using database development/', $commandTester->getDisplay()); + $this->assertSame(0, $exitCode); + } } From 6c1bd1c9bc3fa005d6c85a9c0db5434c1dada2c6 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 12:26:12 +0000 Subject: [PATCH 25/45] Implement tests for multidb configs when rolling back --- tests/Phinx/Console/Command/RollbackTest.php | 209 +++++++++++++++++++ 1 file changed, 209 insertions(+) diff --git a/tests/Phinx/Console/Command/RollbackTest.php b/tests/Phinx/Console/Command/RollbackTest.php index d65536bfc..460e8cd02 100644 --- a/tests/Phinx/Console/Command/RollbackTest.php +++ b/tests/Phinx/Console/Command/RollbackTest.php @@ -60,6 +60,41 @@ protected function setUp() $this->output = new StreamOutput(fopen('php://memory', 'a', false)); } + protected function getMultiDbConfig() + { + + $config = new Config([ + 'paths' => [ + 'migrations' => __FILE__, + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + 'db2' => [ + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development_2', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ] + ]); + + return $config; + + } + public function testExecute() { $application = new PhinxApplication('testing'); @@ -91,6 +126,37 @@ public function testExecute() $this->assertRegExp('/ordering by creation time/', $display); } + public function testExecuteWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Rollback()); + + /** @var Rollback $command */ + $command = $application->find('rollback'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with(self::DEFAULT_TEST_ENVIRONMENT, null, false, true); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + + $display = $commandTester->getDisplay(); + + $this->assertRegExp('/no environment specified/', $display); + + // note that the default order is by creation time + $this->assertRegExp('/ordering by creation time/', $display); + } + public function testExecuteWithEnvironmentOption() { $application = new PhinxApplication('testing'); @@ -116,6 +182,31 @@ public function testExecuteWithEnvironmentOption() $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } + public function testExecuteWithEnvironmentOptionWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Rollback()); + + /** @var Rollback $command */ + $command = $application->find('rollback'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with('development', null, false, true); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + } + public function testDatabaseNameSpecified() { $application = new PhinxApplication('testing'); @@ -141,6 +232,33 @@ public function testDatabaseNameSpecified() $this->assertRegExp('/using database development/', $commandTester->getDisplay()); } + public function testDatabaseNameSpecifiedWithMultiDb() + { + + $application = new PhinxApplication('testing'); + $application->add(new Rollback()); + + /** @var Rollback $command */ + $command = $application->find('rollback'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with(self::DEFAULT_TEST_ENVIRONMENT, null, false); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + $this->assertRegExp('/using database development/', $commandTester->getDisplay()); + + } + public function testStartTimeVersionOrder() { $application = new \Phinx\Console\PhinxApplication('testing'); @@ -168,6 +286,34 @@ public function testStartTimeVersionOrder() $this->assertRegExp('/ordering by execution time/', $commandTester->getDisplay()); } + public function testStartTimeVersionOrderWithMultiDb() + { + $application = new \Phinx\Console\PhinxApplication('testing'); + $application->add(new Rollback()); + + // setup dependencies + $config = $this->getMultiDbConfig(); + $config['version_order'] = \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME; + + $command = $application->find('rollback'); + + // mock the manager class + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$config, $this->input, $this->output]) + ->getMock(); + + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with(self::DEFAULT_TEST_ENVIRONMENT, null, false, true); + + $command->setConfig($config); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + $this->assertRegExp('/ordering by execution time/', $commandTester->getDisplay()); + } + public function testWithDate() { $application = new \Phinx\Console\PhinxApplication('testing'); @@ -203,6 +349,41 @@ public function testWithDate() $commandTester->execute(['command' => $command->getName(), '-d' => $date], ['decorated' => false]); } + public function testWithDateWithMultiDb() + { + $application = new \Phinx\Console\PhinxApplication('testing'); + + $date = '20160101'; + $target = '20160101000000'; + $rollbackStub = $this->getMockBuilder('\Phinx\Console\Command\Rollback') + ->setMethods(['getTargetFromDate']) + ->getMock(); + + $rollbackStub->expects($this->exactly(2)) + ->method('getTargetFromDate') + ->with($date) + ->will($this->returnValue($target)); + + $application->add($rollbackStub); + + // setup dependencies + $command = $application->find('rollback'); + + // mock the manager class + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with(self::DEFAULT_TEST_ENVIRONMENT, $target, false, false); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), '-d' => $date], ['decorated' => false]); + } + /** * @dataProvider getTargetFromDataProvider */ @@ -282,4 +463,32 @@ public function testStarTimeVersionOrderWithDate() $commandTester->execute(['command' => $command->getName(), '-d' => $targetDate], ['decorated' => false]); $this->assertRegExp('/ordering by execution time/', $commandTester->getDisplay()); } + + public function testStarTimeVersionOrderWithDateWithMultiDb() + { + $application = new \Phinx\Console\PhinxApplication('testing'); + $application->add(new Rollback()); + + // setup dependencies + $config = $this->getMultiDbConfig(); + $config['version_order'] = \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME; + + $command = $application->find('rollback'); + + // mock the manager class + $targetDate = '20150101'; + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$config, $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('rollback') + ->with(self::DEFAULT_TEST_ENVIRONMENT, '20150101000000', false, false); + + $command->setConfig($config); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), '-d' => $targetDate], ['decorated' => false]); + $this->assertRegExp('/ordering by execution time/', $commandTester->getDisplay()); + } } From f84c385b10927119008af877f095dce827e95eeb Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 13:57:53 +0000 Subject: [PATCH 26/45] MultiDb tests implemented --- .../Phinx/Console/Command/SeedCreateTest.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tests/Phinx/Console/Command/SeedCreateTest.php b/tests/Phinx/Console/Command/SeedCreateTest.php index 7373c4a55..addb9624b 100644 --- a/tests/Phinx/Console/Command/SeedCreateTest.php +++ b/tests/Phinx/Console/Command/SeedCreateTest.php @@ -56,6 +56,49 @@ protected function setUp() $this->output = new StreamOutput(fopen('php://memory', 'a', false)); } + protected function getMultiDbConfig() + { + + @mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'seeds', 0777, true); + $config = new Config([ + 'paths' => [ + 'migrations' => sys_get_temp_dir(), + 'seeds' => sys_get_temp_dir(), + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'paths' => [ + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'seeds', + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + 'db2' => [ + 'paths' => [ + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db2' . DIRECTORY_SEPARATOR . 'seeds', + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development_2', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ] + ]); + + return $config; + + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The file "MyDuplicateSeeder.php" already exists @@ -82,6 +125,32 @@ public function testExecute() $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateSeeder'], ['decorated' => false]); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The file "MyDuplicateSeeder.php" already exists + */ + public function testExecuteWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedCreate()); + + /** @var SeedCreate $command */ + $command = $application->find('seed:create'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateSeeder'], ['decorated' => false]); + $commandTester->execute(['command' => $command->getName(), 'name' => 'MyDuplicateSeeder'], ['decorated' => false]); + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The seed class name "badseedname" is invalid. Please use CamelCase format @@ -106,4 +175,29 @@ public function testExecuteWithInvalidClassName() $commandTester = new CommandTester($command); $commandTester->execute(['command' => $command->getName(), 'name' => 'badseedname'], ['decorated' => false]); } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The seed class name "badseedname" is invalid. Please use CamelCase format + */ + public function testExecuteWithInvalidClassNameWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedCreate()); + + /** @var SeedCreate $command */ + $command = $application->find('seed:create'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), 'name' => 'badseedname'], ['decorated' => false]); + } } From 9ab0cdcc954135f9e734ca15d37c8261e9dac6bc Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 14:05:09 +0000 Subject: [PATCH 27/45] MultiDb tests for running seeds --- tests/Phinx/Console/Command/SeedRunTest.php | 150 ++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/tests/Phinx/Console/Command/SeedRunTest.php b/tests/Phinx/Console/Command/SeedRunTest.php index 0aa0526b8..93377954f 100644 --- a/tests/Phinx/Console/Command/SeedRunTest.php +++ b/tests/Phinx/Console/Command/SeedRunTest.php @@ -56,6 +56,48 @@ protected function setUp() $this->output = new StreamOutput(fopen('php://memory', 'a', false)); } + protected function getMultiDbConfig() + { + + $config = new Config([ + 'paths' => [ + 'migrations' => __FILE__, + 'seeds' => __FILE__, + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'paths' => [ + 'seeds' => __FILE__, + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + 'db2' => [ + 'paths' => [ + 'seeds' => __FILE__, + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development_2', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ] + ]); + + return $config; + + } + public function testExecute() { $application = new PhinxApplication('testing'); @@ -81,6 +123,31 @@ public function testExecute() $this->assertRegExp('/no environment specified/', $commandTester->getDisplay()); } + public function testExecuteWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedRun()); + + /** @var SeedRun $command */ + $command = $application->find('seed:run'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('seed')->with($this->identicalTo('development'), $this->identicalTo(null)); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + + $this->assertRegExp('/no environment specified/', $commandTester->getDisplay()); + } + public function testExecuteWithEnvironmentOption() { $application = new PhinxApplication('testing'); @@ -105,6 +172,30 @@ public function testExecuteWithEnvironmentOption() $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } + public function testExecuteWithEnvironmentOptionWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedRun()); + + /** @var SeedRun $command */ + $command = $application->find('seed:run'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->any()) + ->method('migrate'); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + } + public function testDatabaseNameSpecified() { $application = new PhinxApplication('testing'); @@ -129,6 +220,30 @@ public function testDatabaseNameSpecified() $this->assertRegExp('/using database development/', $commandTester->getDisplay()); } + public function testDatabaseNameSpecifiedWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedRun()); + + /** @var SeedRun $command */ + $command = $application->find('seed:run'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('seed'); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + $this->assertRegExp('/using database development/', $commandTester->getDisplay()); + } + public function testExecuteMultipleSeeders() { $application = new PhinxApplication('testing'); @@ -163,4 +278,39 @@ public function testExecuteMultipleSeeders() $this->assertRegExp('/no environment specified/', $commandTester->getDisplay()); } + + public function testExecuteMultipleSeedersWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new SeedRun()); + + /** @var SeedRun $command */ + $command = $application->find('seed:run'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(6)) + ->method('seed')->withConsecutive( + [$this->identicalTo('development'), $this->identicalTo('One')], + [$this->identicalTo('development'), $this->identicalTo('Two')], + [$this->identicalTo('development'), $this->identicalTo('Three')] + ); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $commandTester->execute( + [ + 'command' => $command->getName(), + '--seed' => ['One', 'Two', 'Three'], + ], + ['decorated' => false] + ); + + $this->assertRegExp('/no environment specified/', $commandTester->getDisplay()); + } } From 0b3f73cd3b8f034c1629a32cfb078652a55635fc Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 14:17:42 +0000 Subject: [PATCH 28/45] Add multidb tests to status test --- tests/Phinx/Console/Command/StatusTest.php | 164 +++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/tests/Phinx/Console/Command/StatusTest.php b/tests/Phinx/Console/Command/StatusTest.php index 712fc497f..3b99752bb 100644 --- a/tests/Phinx/Console/Command/StatusTest.php +++ b/tests/Phinx/Console/Command/StatusTest.php @@ -60,6 +60,48 @@ protected function setUp() $this->output = new StreamOutput(fopen('php://memory', 'a', false)); } + protected function getMultiDbConfig() + { + + $config = new Config([ + 'paths' => [ + 'migrations' => __FILE__, + 'seeds' => __FILE__, + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'development', + 'development' => [ + 'db1' => [ + 'paths' => [ + 'seeds' => __FILE__, + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + 'db2' => [ + 'paths' => [ + 'seeds' => __FILE__, + ], + 'adapter' => 'mysql', + 'host' => 'fakehost', + 'name' => 'development_2', + 'user' => '', + 'pass' => '', + 'port' => 3006, + ], + ], + ] + ]); + + return $config; + + } + public function testExecute() { $application = new PhinxApplication('testing'); @@ -93,6 +135,39 @@ public function testExecute() $this->assertRegExp('/ordering by creation time/', $display); } + public function testExecuteWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Status()); + + /** @var Status $command */ + $command = $application->find('status'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('printStatus') + ->with(self::DEFAULT_TEST_ENVIRONMENT, null) + ->will($this->returnValue(0)); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $return = $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + + $this->assertEquals(0, $return); + + $display = $commandTester->getDisplay(); + $this->assertRegExp('/no environment specified/', $display); + + // note that the default order is by creation time + $this->assertRegExp('/ordering by creation time/', $display); + } + public function testExecuteWithEnvironmentOption() { $application = new PhinxApplication('testing'); @@ -120,6 +195,33 @@ public function testExecuteWithEnvironmentOption() $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); } + public function testExecuteWithEnvironmentOptionWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Status()); + + /** @var Status $command */ + $command = $application->find('status'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('printStatus') + ->with('development', null) + ->will($this->returnValue(0)); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $return = $commandTester->execute(['command' => $command->getName(), '--environment' => 'development'], ['decorated' => false]); + $this->assertEquals(0, $return); + $this->assertRegExp('/using environment development/', $commandTester->getDisplay()); + } + public function testFormatSpecified() { $application = new PhinxApplication('testing'); @@ -147,6 +249,33 @@ public function testFormatSpecified() $this->assertRegExp('/using format json/', $commandTester->getDisplay()); } + public function testFormatSpecifiedWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Status()); + + /** @var Status $command */ + $command = $application->find('status'); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$this->getMultiDbConfig(), $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('printStatus') + ->with(self::DEFAULT_TEST_ENVIRONMENT, 'json') + ->will($this->returnValue(0)); + + $command->setConfig($this->getMultiDbConfig()); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $return = $commandTester->execute(['command' => $command->getName(), '--format' => 'json'], ['decorated' => false]); + $this->assertEquals(0, $return); + $this->assertRegExp('/using format json/', $commandTester->getDisplay()); + } + public function testExecuteVersionOrderByExecutionTime() { $application = new PhinxApplication('testing'); @@ -179,4 +308,39 @@ public function testExecuteVersionOrderByExecutionTime() $this->assertRegExp('/no environment specified/', $display); $this->assertRegExp('/ordering by execution time/', $display); } + + public function testExecuteVersionOrderByExecutionTimeWithMultiDb() + { + $application = new PhinxApplication('testing'); + $application->add(new Status()); + + /** @var Status $command */ + $command = $application->find('status'); + + $config = $this->getMultiDbConfig(); + + // mock the manager class + /** @var Manager|PHPUnit_Framework_MockObject_MockObject $managerStub */ + $managerStub = $this->getMockBuilder('\Phinx\Migration\Manager') + ->setConstructorArgs([$config, $this->input, $this->output]) + ->getMock(); + $managerStub->expects($this->exactly(2)) + ->method('printStatus') + ->with(self::DEFAULT_TEST_ENVIRONMENT, null) + ->will($this->returnValue(0)); + + $config['version_order'] = \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME; + + $command->setConfig($config); + $command->setManager($managerStub); + + $commandTester = new CommandTester($command); + $return = $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); + + $this->assertEquals(0, $return); + + $display = $commandTester->getDisplay(); + $this->assertRegExp('/no environment specified/', $display); + $this->assertRegExp('/ordering by execution time/', $display); + } } From a49fc5e6ebd3a5f7fc934598ab298c055c233d3d Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 16:14:12 +0000 Subject: [PATCH 29/45] Make sure status sets the db reference --- src/Phinx/Console/Command/Status.php | 1 + src/Phinx/Migration/Manager.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Console/Command/Status.php b/src/Phinx/Console/Command/Status.php index c8b22226c..ebd51d863 100644 --- a/src/Phinx/Console/Command/Status.php +++ b/src/Phinx/Console/Command/Status.php @@ -92,6 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($databases as $adapterOptions) { + $this->getManager()->setMigrations(null); if (isset($adapterOptions['dbRef'])) { $this->getManager()->setDbRef($adapterOptions['dbRef']); } diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 7fd792216..2e7074e9b 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -112,6 +112,7 @@ public function __construct(ConfigInterface $config, InputInterface $input, Outp */ public function printStatus($environment, $format = null) { + $this->setEnvironmentName($environment); $output = $this->getOutput(); $migrations = []; $hasDownMigration = false; @@ -672,7 +673,6 @@ public function getSingleEnvironment($name) */ public function getMultiEnvironment($name) { - if (isset($this->environments[$name][$this->getDbRef()])) { return $this->environments[$name][$this->getDbRef()]; } From 929eca1b770947e9104cd8c7214a3347b4bad2b2 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 16:25:18 +0000 Subject: [PATCH 30/45] Add namespace checking to get migration paths --- src/Phinx/Config/Config.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index d48e2310e..b12de9539 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -287,9 +287,19 @@ public function getMigrationPaths($environment = null, $dbReference = null) } if ($environment !== null && $dbReference !== null) { + $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['migrations'])) { - return [$environment[$dbReference]['paths']['migrations']]; + if (!is_array($environment[$dbReference]['paths']['migrations'])) { + return [$environment[$dbReference]['paths']['migrations']]; + } else { + foreach ($environment[$dbReference]['paths']['migrations'] as $namespace => $migrationPath) { + $paths[$namespace]= $migrationPath; + } + if (count($paths) > 0) { + $this->values['paths']['migrations']= $paths; + } + } } } elseif (is_null($environment) && is_null($dbReference)) { if (is_array($this->values['environments'])) { From 547258e1e29300da5ee2a920243f0a7cd4a4da13 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Thu, 23 Nov 2017 16:35:05 +0000 Subject: [PATCH 31/45] Add namespace options to seed paths --- src/Phinx/Config/Config.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index b12de9539..c2faca28e 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -363,7 +363,17 @@ public function getSeedPaths($environment = null, $dbReference = null) if ($environment !== null && $dbReference !== null) { $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['seeds'])) { - return [$environment[$dbReference]['paths']['seeds']]; + + if (!is_array($environment[$dbReference]['paths']['seeds'])) { + return [$environment[$dbReference]['paths']['seeds']]; + } else { + foreach ($environment[$dbReference]['paths']['seeds'] as $namespace => $seedPath) { + $paths[$namespace]= $seedPath; + } + if (count($paths) > 0) { + $this->values['paths']['seeds']= $paths; + } + } } } elseif (is_null($environment) && is_null($dbReference)) { if (is_array($this->values['environments'])) { From 97c2794ee91f08b2fe6d885f8fb527c3544ebd56 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 09:08:02 +0000 Subject: [PATCH 32/45] Fix missing call to empty seeds as they are iterated --- src/Phinx/Console/Command/SeedRun.php | 2 ++ src/Phinx/Migration/Manager.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index cbdedca32..fca57dc57 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -87,6 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($databases as $adapterOptions) { + $this->getManager()->setSeeds(null); + if (isset($adapterOptions['dbRef'])) { $this->getManager()->setDbRef($adapterOptions['dbRef']); } diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 2e7074e9b..15da5ba3a 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -859,7 +859,7 @@ protected function getMigrationFiles() * @param array $seeds Seeders * @return \Phinx\Migration\Manager */ - public function setSeeds(array $seeds) + public function setSeeds(array $seeds = null) { $this->seeds = $seeds; From b6e83ba9ab0f852556bca6b3310af76b54dae83f Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 09:24:28 +0000 Subject: [PATCH 33/45] Allow the option to specify the db reference when running specific seeds --- src/Phinx/Config/Config.php | 4 +-- src/Phinx/Console/Command/SeedRun.php | 41 ++++++++++++++++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index c2faca28e..610e947a2 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -194,10 +194,10 @@ public function getStorageConfigs(array $envOptions) if (count($envOptions) > 0) { foreach ($envOptions as $dbRef => $adapterOptions) { if (!is_array($adapterOptions)) { - $configs []= $envOptions; + $configs [$dbRef]= $envOptions; break; } else { - $configs []= array_merge($adapterOptions, ['dbRef' => $dbRef]); + $configs [$dbRef]= array_merge($adapterOptions, ['dbRef' => $dbRef]); } } } diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index fca57dc57..e18088fd5 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -46,6 +46,7 @@ protected function configure() $this->setName('seed:run') ->setDescription('Run database seeders') ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?') + ->addOption('--database', '-d', InputOption::VALUE_REQUIRED, 'What is the db reference required?') ->setHelp( <<seed:run command runs all available or individual seeders @@ -53,6 +54,7 @@ protected function configure() phinx seed:run -e development phinx seed:run -e development -s UserSeeder phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder +phinx seed:run -e development -s UserSeeder -d databaseReference phinx seed:run -e development -v EOT @@ -72,6 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $seedSet = $input->getOption('seed'); $environment = $input->getOption('environment'); + $dbRef = $input->getOption('database'); if ($environment === null) { $environment = $this->getConfig()->getDefaultEnvironment(); @@ -82,19 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $envOptions = $this->getConfig()->getEnvironment($environment); $databases = $this->getConfig()->getStorageConfigs($envOptions); - $start = microtime(true); - foreach ($databases as $adapterOptions) { - - $this->getManager()->setSeeds(null); - - if (isset($adapterOptions['dbRef'])) { - $this->getManager()->setDbRef($adapterOptions['dbRef']); - } - - $this->outputEnvironmentInfo($adapterOptions, $output); - + if (!is_null($dbRef)) { + $this->getManager()->setDbRef($dbRef); + $this->outputEnvironmentInfo($databases[$dbRef], $output); if (empty($seedSet)) { // run all the seed(ers) $this->getManager()->seed($environment); @@ -105,6 +100,30 @@ protected function execute(InputInterface $input, OutputInterface $output) } } + } else { + + foreach ($databases as $adapterOptions) { + + $this->getManager()->setSeeds(null); + + if (isset($adapterOptions['dbRef'])) { + $this->getManager()->setDbRef($adapterOptions['dbRef']); + } + + $this->outputEnvironmentInfo($adapterOptions, $output); + + if (empty($seedSet)) { + // run all the seed(ers) + $this->getManager()->seed($environment); + } else { + // run seed(ers) specified in a comma-separated list of classes + foreach ($seedSet as $seed) { + $this->getManager()->seed($environment, trim($seed)); + } + } + + } + } $end = microtime(true); From d7757906475e06d402cc0eccf01e8a1cfc6ae078 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 10:00:43 +0000 Subject: [PATCH 34/45] Fix PHPStan analysis errors --- src/Phinx/Config/ConfigInterface.php | 2 +- src/Phinx/Console/Command/AbstractCommand.php | 2 +- src/Phinx/Migration/Manager.php | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Phinx/Config/ConfigInterface.php b/src/Phinx/Config/ConfigInterface.php index 37ec53df8..6ef5ab57a 100644 --- a/src/Phinx/Config/ConfigInterface.php +++ b/src/Phinx/Config/ConfigInterface.php @@ -110,7 +110,7 @@ public function getMigrationPaths($environment = null, $dbReference = null); * * @return string[] */ - public function getSeedPaths(); + public function getSeedPaths($environment = null, $dbReference = null); /** * Get the template file name. diff --git a/src/Phinx/Console/Command/AbstractCommand.php b/src/Phinx/Console/Command/AbstractCommand.php index 9068088aa..5e006e201 100644 --- a/src/Phinx/Console/Command/AbstractCommand.php +++ b/src/Phinx/Console/Command/AbstractCommand.php @@ -348,7 +348,7 @@ protected function verifySeedDirectory($path) * * @param array $info * @param OutputInterface $output - * @return null + * @return null|int */ public function outputEnvironmentInfo(array $info, OutputInterface $output) { diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index d9830c20f..697a738c1 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -59,6 +59,11 @@ class Manager */ protected $environments; + /** + * @var string + */ + protected $dbRef; + /** * @var string */ From de5f2de826a6292e9e199379b2677829a5c770d9 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 10:19:48 +0000 Subject: [PATCH 35/45] Fix undefined variable errors --- src/Phinx/Config/Config.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 610e947a2..bd00248fd 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -302,7 +302,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) } } } elseif (is_null($environment) && is_null($dbReference)) { - if (is_array($this->values['environments'])) { + if (isset($this->values['environments']) && is_array($this->values['environments'])) { $environments = array_keys($this->values['environments']); foreach ($environments as $env) { @@ -312,7 +312,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) continue; } - if (!is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { + if (isset($this->values['environments'][$env][$dbReference]['paths']) && !is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } else { foreach ($this->values['environments'][$env][$dbReference]['paths']['migrations'] as $namespace => $migrationPath) { @@ -376,7 +376,7 @@ public function getSeedPaths($environment = null, $dbReference = null) } } } elseif (is_null($environment) && is_null($dbReference)) { - if (is_array($this->values['environments'])) { + if (isset($this->values['environments']) && is_array($this->values['environments'])) { $environments = array_keys($this->values['environments']); foreach ($environments as $env) { if (is_array($this->values['environments'][$env])) { From 25e9054bc3aac5a59f0a7422a6f242a6f8ff3b5a Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 10:24:29 +0000 Subject: [PATCH 36/45] Fix undefined index in migration paths --- src/Phinx/Config/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index bd00248fd..f12a9e661 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -312,7 +312,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) continue; } - if (isset($this->values['environments'][$env][$dbReference]['paths']) && !is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { + if (!isset($this->values['environments'][$env][$dbReference]['paths']) || !is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } else { foreach ($this->values['environments'][$env][$dbReference]['paths']['migrations'] as $namespace => $migrationPath) { From 7984b3b224a4182a18ca8926e04a2f427eee35e2 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 10:32:20 +0000 Subject: [PATCH 37/45] Fix missing parameter in test --- tests/Phinx/Console/Command/MigrateTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Phinx/Console/Command/MigrateTest.php b/tests/Phinx/Console/Command/MigrateTest.php index b27941775..3c8c81ba7 100644 --- a/tests/Phinx/Console/Command/MigrateTest.php +++ b/tests/Phinx/Console/Command/MigrateTest.php @@ -77,6 +77,9 @@ public function getMultiDbConfig() 'adapter' => 'mysql', 'host' => 'fakehost', 'name' => 'development', + 'paths' => [ + 'migrations' => __FILE__, + ], 'user' => '', 'pass' => '', 'port' => 3006, @@ -85,6 +88,9 @@ public function getMultiDbConfig() 'adapter' => 'mysql', 'host' => 'fakehost', 'name' => 'development_2', + 'paths' => [ + 'migrations' => __FILE__, + ], 'user' => '', 'pass' => '', 'port' => 3006, From c54c0acc58b245a51683fddaef7d0695da5b2791 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 11:05:04 +0000 Subject: [PATCH 38/45] Fix missing params in test configs --- src/Phinx/Config/Config.php | 2 +- tests/Phinx/Console/Command/RollbackTest.php | 6 ++++++ tests/Phinx/Console/Command/SeedCreateTest.php | 2 ++ tests/Phinx/Console/Command/SeedRunTest.php | 2 ++ tests/Phinx/Console/Command/StatusTest.php | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index f12a9e661..b0c48a8bc 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -312,7 +312,7 @@ public function getMigrationPaths($environment = null, $dbReference = null) continue; } - if (!isset($this->values['environments'][$env][$dbReference]['paths']) || !is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { + if (!is_array($this->values['environments'][$env][$dbReference]['paths']['migrations'])) { $paths []= $this->values['environments'][$env][$dbReference]['paths']['migrations']; } else { foreach ($this->values['environments'][$env][$dbReference]['paths']['migrations'] as $namespace => $migrationPath) { diff --git a/tests/Phinx/Console/Command/RollbackTest.php b/tests/Phinx/Console/Command/RollbackTest.php index 460e8cd02..afd997dcb 100644 --- a/tests/Phinx/Console/Command/RollbackTest.php +++ b/tests/Phinx/Console/Command/RollbackTest.php @@ -73,6 +73,9 @@ protected function getMultiDbConfig() 'development' => [ 'db1' => [ 'adapter' => 'mysql', + 'paths' => [ + 'migrations' => __FILE__, + ], 'host' => 'fakehost', 'name' => 'development', 'user' => '', @@ -81,6 +84,9 @@ protected function getMultiDbConfig() ], 'db2' => [ 'adapter' => 'mysql', + 'paths' => [ + 'migrations' => __FILE__, + ], 'host' => 'fakehost', 'name' => 'development_2', 'user' => '', diff --git a/tests/Phinx/Console/Command/SeedCreateTest.php b/tests/Phinx/Console/Command/SeedCreateTest.php index addb9624b..0cdc7a636 100644 --- a/tests/Phinx/Console/Command/SeedCreateTest.php +++ b/tests/Phinx/Console/Command/SeedCreateTest.php @@ -71,6 +71,7 @@ protected function getMultiDbConfig() 'development' => [ 'db1' => [ 'paths' => [ + 'migrations' => sys_get_temp_dir(), 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'seeds', ], 'adapter' => 'mysql', @@ -82,6 +83,7 @@ protected function getMultiDbConfig() ], 'db2' => [ 'paths' => [ + 'migrations' => sys_get_temp_dir(), 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db2' . DIRECTORY_SEPARATOR . 'seeds', ], 'adapter' => 'mysql', diff --git a/tests/Phinx/Console/Command/SeedRunTest.php b/tests/Phinx/Console/Command/SeedRunTest.php index 93377954f..9e2cbd5a6 100644 --- a/tests/Phinx/Console/Command/SeedRunTest.php +++ b/tests/Phinx/Console/Command/SeedRunTest.php @@ -70,6 +70,7 @@ protected function getMultiDbConfig() 'development' => [ 'db1' => [ 'paths' => [ + 'migrations' => __FILE__, 'seeds' => __FILE__, ], 'adapter' => 'mysql', @@ -81,6 +82,7 @@ protected function getMultiDbConfig() ], 'db2' => [ 'paths' => [ + 'migrations' => __FILE__, 'seeds' => __FILE__, ], 'adapter' => 'mysql', diff --git a/tests/Phinx/Console/Command/StatusTest.php b/tests/Phinx/Console/Command/StatusTest.php index 3b99752bb..908ec6cca 100644 --- a/tests/Phinx/Console/Command/StatusTest.php +++ b/tests/Phinx/Console/Command/StatusTest.php @@ -74,6 +74,7 @@ protected function getMultiDbConfig() 'development' => [ 'db1' => [ 'paths' => [ + 'migrations' => __FILE__, 'seeds' => __FILE__, ], 'adapter' => 'mysql', @@ -85,6 +86,7 @@ protected function getMultiDbConfig() ], 'db2' => [ 'paths' => [ + 'migrations' => __FILE__, 'seeds' => __FILE__, ], 'adapter' => 'mysql', From b809d4908bd9a9748e94fa068a678cd9189ee29a Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 11:25:54 +0000 Subject: [PATCH 39/45] Code style fixes --- src/Phinx/Config/Config.php | 3 --- src/Phinx/Console/Command/AbstractCommand.php | 1 - src/Phinx/Console/Command/Migrate.php | 3 --- src/Phinx/Console/Command/Rollback.php | 2 -- src/Phinx/Console/Command/SeedRun.php | 6 ------ src/Phinx/Console/Command/Status.php | 1 - src/Phinx/Migration/Manager.php | 4 +--- 7 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index b0c48a8bc..543663060 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -203,7 +203,6 @@ public function getStorageConfigs(array $envOptions) } return $configs; - } /** @@ -287,7 +286,6 @@ public function getMigrationPaths($environment = null, $dbReference = null) } if ($environment !== null && $dbReference !== null) { - $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['migrations'])) { if (!is_array($environment[$dbReference]['paths']['migrations'])) { @@ -363,7 +361,6 @@ public function getSeedPaths($environment = null, $dbReference = null) if ($environment !== null && $dbReference !== null) { $environment = $this->getEnvironment($environment); if (isset($environment[$dbReference]['paths']['seeds'])) { - if (!is_array($environment[$dbReference]['paths']['seeds'])) { return [$environment[$dbReference]['paths']['seeds']]; } else { diff --git a/src/Phinx/Console/Command/AbstractCommand.php b/src/Phinx/Console/Command/AbstractCommand.php index 5e006e201..57f32457b 100644 --- a/src/Phinx/Console/Command/AbstractCommand.php +++ b/src/Phinx/Console/Command/AbstractCommand.php @@ -379,7 +379,6 @@ public function outputEnvironmentInfo(array $info, OutputInterface $output) if (isset($info['table_suffix'])) { $output->writeln('using table suffix ' . $info['table_suffix']); } - } /** diff --git a/src/Phinx/Console/Command/Migrate.php b/src/Phinx/Console/Command/Migrate.php index c2832bab6..72e227b0d 100644 --- a/src/Phinx/Console/Command/Migrate.php +++ b/src/Phinx/Console/Command/Migrate.php @@ -90,7 +90,6 @@ protected function execute(InputInterface $input, OutputInterface $output) // run the migrations $start = microtime(true); foreach ($databases as $adapterOptions) { - $this->getManager()->setMigrations(null); if (isset($adapterOptions['dbRef'])) { @@ -104,7 +103,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $this->getManager()->migrate($environment, $version); } - } $end = microtime(true); @@ -113,5 +111,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - } diff --git a/src/Phinx/Console/Command/Rollback.php b/src/Phinx/Console/Command/Rollback.php index d68018464..14edde616 100644 --- a/src/Phinx/Console/Command/Rollback.php +++ b/src/Phinx/Console/Command/Rollback.php @@ -101,7 +101,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $start = microtime(true); foreach ($databases as $adapterOptions) { - // rollback the specified environment if ($date === null) { $targetMustMatchVersion = true; @@ -121,7 +120,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('ordering by ' . $versionOrder . " time"); $this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion); - } $end = microtime(true); diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index e18088fd5..e62b265d8 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -99,11 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->getManager()->seed($environment, trim($seed)); } } - } else { - foreach ($databases as $adapterOptions) { - $this->getManager()->setSeeds(null); if (isset($adapterOptions['dbRef'])) { @@ -121,15 +118,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->getManager()->seed($environment, trim($seed)); } } - } - } $end = microtime(true); $output->writeln(''); $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); - } } diff --git a/src/Phinx/Console/Command/Status.php b/src/Phinx/Console/Command/Status.php index ebd51d863..2c01bc8c1 100644 --- a/src/Phinx/Console/Command/Status.php +++ b/src/Phinx/Console/Command/Status.php @@ -91,7 +91,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $start = microtime(true); foreach ($databases as $adapterOptions) { - $this->getManager()->setMigrations(null); if (isset($adapterOptions['dbRef'])) { $this->getManager()->setDbRef($adapterOptions['dbRef']); diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 697a738c1..9ae8c1a53 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -587,7 +587,7 @@ public function setDbRef($ref) * Get the adapter property * * @return string - */ + */ public function getDbRef() { return $this->dbRef; @@ -666,7 +666,6 @@ public function getSingleEnvironment($name) $environment->setOutput($this->getOutput()); return $environment; - } /** @@ -702,7 +701,6 @@ public function getMultiEnvironment($name) $environment->setOutput($this->getOutput()); return $environment; - } /** From ee7f84d5277e8bb1e33f901a57a10f6e2f37e68b Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 15:23:13 +0000 Subject: [PATCH 40/45] Additional missing tests --- tests/Phinx/Config/AbstractConfigTest.php | 36 +++++++++++++++++++++++ tests/Phinx/Config/ConfigTest.php | 8 +++++ tests/Phinx/Migration/ManagerTest.php | 7 +++++ 3 files changed, 51 insertions(+) diff --git a/tests/Phinx/Config/AbstractConfigTest.php b/tests/Phinx/Config/AbstractConfigTest.php index 940e8d179..8c4a6808c 100644 --- a/tests/Phinx/Config/AbstractConfigTest.php +++ b/tests/Phinx/Config/AbstractConfigTest.php @@ -57,6 +57,42 @@ public function getConfigArray() ]; } + public function getConfigArrayWithMultiDb() + { + return [ + 'default' => [ + 'paths' => [ + 'migrations' => '%%PHINX_CONFIG_PATH%%/testmigrations2', + 'seeds' => '%%PHINX_CONFIG_PATH%%/db/seeds', + ] + ], + 'paths' => [ + 'migrations' => $this->getMigrationPaths(), + 'seeds' => $this->getSeedPaths() + ], + 'templates' => [ + 'file' => '%%PHINX_CONFIG_PATH%%/tpl/testtemplate.txt', + 'class' => '%%PHINX_CONFIG_PATH%%/tpl/testtemplate.php' + ], + 'environments' => [ + 'default_migration_table' => 'phinxlog', + 'default_database' => 'testing', + 'testing' => [ + 'db1' => [ + 'adapter' => 'sqllite', + 'wrapper' => 'testwrapper', + 'paths' => [ + 'migrations' => '%%PHINX_CONFIG_PATH%%/testdb/test.db' + ], + ] + ], + 'production' => [ + 'adapter' => 'mysql' + ] + ] + ]; + } + /** * Generate dummy migration paths * diff --git a/tests/Phinx/Config/ConfigTest.php b/tests/Phinx/Config/ConfigTest.php index 911638539..a216c4614 100644 --- a/tests/Phinx/Config/ConfigTest.php +++ b/tests/Phinx/Config/ConfigTest.php @@ -283,4 +283,12 @@ public function isVersionOrderCreationTimeDataProvider() ], ]; } + + public function testGetStorageSpecificConfig() + { + $config = new Config($this->getConfigArrayWithMultiDb()); + $envOptions = $config->getEnvironment('testing'); + $multiEnvOptions = $config->getStorageConfigs($envOptions); + $this->assertArrayHasKey('db1', $multiEnvOptions); + } } diff --git a/tests/Phinx/Migration/ManagerTest.php b/tests/Phinx/Migration/ManagerTest.php index d9ff44610..297549798 100644 --- a/tests/Phinx/Migration/ManagerTest.php +++ b/tests/Phinx/Migration/ManagerTest.php @@ -121,6 +121,13 @@ public function testInstantiation() $this->assertTrue($this->manager->getOutput() instanceof StreamOutput); } + public function testSetDbRef() + { + $reference = 'TestReference'; + $this->manager->setDbRef($reference); + $this->assertEquals($reference, $this->manager->getDbRef()); + } + public function testPrintStatusMethod() { // stub environment From 0946002a055ed1bc5a0ba06baf4d6450ee6527b9 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 15:34:40 +0000 Subject: [PATCH 41/45] Test removing default_database param --- tests/Phinx/Config/ConfigTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Phinx/Config/ConfigTest.php b/tests/Phinx/Config/ConfigTest.php index a216c4614..967db19b6 100644 --- a/tests/Phinx/Config/ConfigTest.php +++ b/tests/Phinx/Config/ConfigTest.php @@ -288,6 +288,7 @@ public function testGetStorageSpecificConfig() { $config = new Config($this->getConfigArrayWithMultiDb()); $envOptions = $config->getEnvironment('testing'); + $envOptions['default_database']= __FILE__; $multiEnvOptions = $config->getStorageConfigs($envOptions); $this->assertArrayHasKey('db1', $multiEnvOptions); } From 2ee7b3bca5a00a6d3a851783bbda4aebbd49c708 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Fri, 24 Nov 2017 16:17:34 +0000 Subject: [PATCH 42/45] Missing unit tests --- tests/Phinx/Config/AbstractConfigTest.php | 77 ++++++++++++++++++- .../Phinx/Config/ConfigMigrationPathsTest.php | 10 +++ tests/Phinx/Config/ConfigSeedPathsTest.php | 10 +++ .../Phinx/Console/Command/SeedCreateTest.php | 2 +- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/tests/Phinx/Config/AbstractConfigTest.php b/tests/Phinx/Config/AbstractConfigTest.php index 8c4a6808c..122b4088d 100644 --- a/tests/Phinx/Config/AbstractConfigTest.php +++ b/tests/Phinx/Config/AbstractConfigTest.php @@ -15,11 +15,31 @@ abstract class AbstractConfigTest extends \PHPUnit_Framework_TestCase */ protected $migrationPath = null; + /** + * @var string + */ + protected $migrationPathWithMultiDb = null; + + /** + * @var string + */ + protected $migrationPathWithMultiDbAsString = null; + /** * @var string */ protected $seedPath = null; + /** + * @var string + */ + protected $seedPathWithMultiDb = null; + + /** + * @var string + */ + protected $seedPathWithMultiDbAsString = null; + /** * Returns a sample configuration array for use with the unit tests. * @@ -82,12 +102,19 @@ public function getConfigArrayWithMultiDb() 'adapter' => 'sqllite', 'wrapper' => 'testwrapper', 'paths' => [ - 'migrations' => '%%PHINX_CONFIG_PATH%%/testdb/test.db' + 'migrations' => $this->getMigrationPathsWithMultiDb(), + 'seeds' => $this->getSeedPathsWithMultiDb() ], ] ], 'production' => [ - 'adapter' => 'mysql' + 'db1' => [ + 'adapter' => 'mysql', + 'paths' => [ + 'migrations' => $this->getMigrationPathsWithMultiDbAsString(), + 'seeds' => $this->getSeedPathsWithMultiDbAsString() + ], + ] ] ] ]; @@ -107,6 +134,24 @@ protected function getMigrationPaths() return [$this->migrationPath]; } + protected function getMigrationPathsWithMultiDb() + { + if (null === $this->migrationPathWithMultiDb) { + $this->migrationPathWithMultiDb = uniqid('phinx', true); + } + + return [$this->migrationPathWithMultiDb]; + } + + protected function getMigrationPathsWithMultiDbAsString() + { + if (null === $this->migrationPathWithMultiDbAsString) { + $this->migrationPathWithMultiDbAsString = uniqid('phinx', true); + } + + return $this->migrationPathWithMultiDbAsString; + } + /** * Generate dummy seed paths * @@ -120,4 +165,32 @@ protected function getSeedPaths() return [$this->seedPath]; } + + /** + * Generate dummy seed paths + * + * @return string[] + */ + protected function getSeedPathsWithMultiDb() + { + if (null === $this->seedPathWithMultiDb) { + $this->seedPathWithMultiDb = uniqid('phinx', true); + } + + return [$this->seedPathWithMultiDb]; + } + + /** + * Generate dummy seed paths + * + * @return string[] + */ + protected function getSeedPathsWithMultiDbAsString() + { + if (null === $this->seedPathWithMultiDbAsString) { + $this->seedPathWithMultiDbAsString = uniqid('phinx', true); + } + + return $this->seedPathWithMultiDbAsString; + } } diff --git a/tests/Phinx/Config/ConfigMigrationPathsTest.php b/tests/Phinx/Config/ConfigMigrationPathsTest.php index c29bf0a23..c17a40f53 100644 --- a/tests/Phinx/Config/ConfigMigrationPathsTest.php +++ b/tests/Phinx/Config/ConfigMigrationPathsTest.php @@ -30,6 +30,16 @@ public function testGetMigrationPaths() $this->assertEquals($this->getMigrationPaths(), $config->getMigrationPaths()); } + /** + * Normal behavior + */ + public function testGetMigrationPathsWithMultiDb() + { + $config = new Config($this->getConfigArrayWithMultiDb()); + $this->assertEquals($this->getMigrationPathsWithMultiDb(), $config->getMigrationPaths('testing', 'db1')); + $this->assertEquals([$this->getMigrationPathsWithMultiDbAsString()], $config->getMigrationPaths('production', 'db1')); + } + public function testGetMigrationPathConvertsStringToArray() { $values = [ diff --git a/tests/Phinx/Config/ConfigSeedPathsTest.php b/tests/Phinx/Config/ConfigSeedPathsTest.php index 74cd45d7d..051853eff 100644 --- a/tests/Phinx/Config/ConfigSeedPathsTest.php +++ b/tests/Phinx/Config/ConfigSeedPathsTest.php @@ -30,6 +30,16 @@ public function testGetSeedPaths() $this->assertEquals($this->getSeedPaths(), $config->getSeedPaths()); } + /** + * Normal behavior + */ + public function testGetSeedPathsWithMultiDb() + { + $config = new Config($this->getConfigArrayWithMultiDb()); + $this->assertEquals($this->getSeedPathsWithMultiDb(), $config->getSeedPaths('testing', 'db1')); + $this->assertEquals([$this->getSeedPathsWithMultiDbAsString()], $config->getSeedPaths('production', 'db1')); + } + public function testGetSeedPathConvertsStringToArray() { diff --git a/tests/Phinx/Console/Command/SeedCreateTest.php b/tests/Phinx/Console/Command/SeedCreateTest.php index 0cdc7a636..564af4b6b 100644 --- a/tests/Phinx/Console/Command/SeedCreateTest.php +++ b/tests/Phinx/Console/Command/SeedCreateTest.php @@ -63,7 +63,7 @@ protected function getMultiDbConfig() $config = new Config([ 'paths' => [ 'migrations' => sys_get_temp_dir(), - 'seeds' => sys_get_temp_dir(), + 'seeds' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db1' . DIRECTORY_SEPARATOR . 'seeds', ], 'environments' => [ 'default_migration_table' => 'phinxlog', From 84aa46085c87fedcfeadba503d7aead4608cf26d Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Mon, 18 Dec 2017 16:15:42 +0000 Subject: [PATCH 43/45] Fix exception being thrown when a seed doesn't exist in a preceeding directory --- src/Phinx/Migration/Manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index 9ae8c1a53..52d8e898d 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -566,7 +566,7 @@ public function seed($environment, $seed = null) $this->executeSeed($environment, $seeds[$seed]); unset($this->seeds[$seed]); } else { - throw new \InvalidArgumentException(sprintf('The seed class "%s" does not exist', $seed)); + $this->getOutput()->writeln(sprintf('The seed class "%s" does not exist', $seed)); } } } From da3a03c1d52af78230fc048c0b1ff0c88b7a772b Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Wed, 20 Dec 2017 10:30:11 +0000 Subject: [PATCH 44/45] Fix PHPCBF error --- src/Phinx/Console/Command/SeedRun.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phinx/Console/Command/SeedRun.php b/src/Phinx/Console/Command/SeedRun.php index d6efa3a17..e62b265d8 100644 --- a/src/Phinx/Console/Command/SeedRun.php +++ b/src/Phinx/Console/Command/SeedRun.php @@ -58,7 +58,7 @@ protected function configure() phinx seed:run -e development -v EOT - ); + ); } /** From 480cc8263b577b7c93ada2118b59d93230cf2634 Mon Sep 17 00:00:00 2001 From: Mike van Rooyen Date: Wed, 20 Dec 2017 10:50:49 +0000 Subject: [PATCH 45/45] Fix broken tests --- tests/Phinx/Migration/ManagerTest.php | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/tests/Phinx/Migration/ManagerTest.php b/tests/Phinx/Migration/ManagerTest.php index b858dc4e7..8700ef340 100644 --- a/tests/Phinx/Migration/ManagerTest.php +++ b/tests/Phinx/Migration/ManagerTest.php @@ -5365,10 +5365,6 @@ public function testExecuteASingleSeedWorksAsExpectedWithMixedNamespace() $this->assertContains('Baz\UserSeeder', $output); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage The seed class "NonExistentSeeder" does not exist - */ public function testExecuteANonExistentSeedWorksAsExpected() { // stub environment @@ -5379,13 +5375,9 @@ public function testExecuteANonExistentSeedWorksAsExpected() $this->manager->seed('mockenv', 'NonExistentSeeder'); rewind($this->manager->getOutput()->getStream()); $output = stream_get_contents($this->manager->getOutput()->getStream()); - $this->assertContains('UserSeeder', $output); + $this->assertContains('NonExistentSeeder', $output); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage The seed class "Foo\Bar\NonExistentSeeder" does not exist - */ public function testExecuteANonExistentSeedWorksAsExpectedWithNamespace() { // stub environment @@ -5397,13 +5389,9 @@ public function testExecuteANonExistentSeedWorksAsExpectedWithNamespace() $this->manager->seed('mockenv', 'Foo\Bar\NonExistentSeeder'); rewind($this->manager->getOutput()->getStream()); $output = stream_get_contents($this->manager->getOutput()->getStream()); - $this->assertContains('Foo\Bar\UserSeeder', $output); + $this->assertContains('Foo\Bar\NonExistentSeeder', $output); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage The seed class "Baz\NonExistentSeeder" does not exist - */ public function testExecuteANonExistentSeedWorksAsExpectedWithMixedNamespace() { // stub environment @@ -5412,12 +5400,11 @@ public function testExecuteANonExistentSeedWorksAsExpectedWithMixedNamespace() ->getMock(); $this->manager->setConfig($this->getConfigWithMixedNamespace()); $this->manager->setEnvironments(['mockenv' => $envStub]); - $this->manager->seed('mockenv', 'Baz\NonExistentSeeder'); + $this->manager->seed('mockenv', 'Foo\Baz\NonExistentSeeder'); rewind($this->manager->getOutput()->getStream()); $output = stream_get_contents($this->manager->getOutput()->getStream()); - $this->assertContains('UserSeeder', $output); - $this->assertContains('Baz\UserSeeder', $output); - $this->assertContains('Foo\Bar\UserSeeder', $output); + $this->assertContains('Baz\NonExistentSeeder', $output); + $this->assertContains('Foo\Baz\NonExistentSeeder', $output); } public function testGettingInputObject()