Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
acb773e
Update method to get migration paths from default_ location in config
Nov 14, 2017
d708d01
Iterate through all of the adapter options and pass the environment t…
Nov 14, 2017
ef46680
Rework how migration paths are retrieved pass adaptor executor
Nov 14, 2017
897cd4c
Get selected environment from Input interface
Nov 16, 2017
0dceab4
Rework to allow db references and use previous adapter references
Nov 16, 2017
55e2dca
Move environment info output to own method
Nov 16, 2017
c3bc5d4
Enable multi database management for rollback and seed functions
Nov 17, 2017
3f10f4d
Refactor how environment data is retreived for single and multiple da…
Nov 17, 2017
17008b9
Fix issue preventing single db seed creation
Nov 17, 2017
c855d5e
Rollback and status function to work with single databases in env
Nov 17, 2017
847a67e
Refactor the envoptions to merge single and multiple environments in …
Nov 17, 2017
b854441
Simplfy how the db reference is extracted
Nov 17, 2017
3a00e7f
Store environment value as property rather than relying on access fro…
Nov 21, 2017
8608ccd
Revery labelling of default paths to original format for backwards co…
Nov 21, 2017
4afef22
Fix test error for output
Nov 21, 2017
c1a8a3b
Refactor getting the seed and migration paths
Nov 21, 2017
6d2dccb
Update test to get full config for environment
Nov 21, 2017
e80fc7d
Make sure the environments param is an array before getting keys
Nov 21, 2017
9771ca2
Fix erroring tests
Nov 22, 2017
570b600
Test migration with multienv config
Nov 22, 2017
57b624a
Fix namespace issue when creating migrations
Nov 23, 2017
f5b2980
Fix seed path with namespaces
Nov 23, 2017
120b3b4
MultiDb tests added to test migration creation
Nov 23, 2017
88f214c
Implement tests to check multiDb config for migrations
Nov 23, 2017
6c1bd1c
Implement tests for multidb configs when rolling back
Nov 23, 2017
f84c385
MultiDb tests implemented
Nov 23, 2017
9ab0cdc
MultiDb tests for running seeds
Nov 23, 2017
0b3f73c
Add multidb tests to status test
Nov 23, 2017
a49fc5e
Make sure status sets the db reference
Nov 23, 2017
929eca1
Add namespace checking to get migration paths
Nov 23, 2017
547258e
Add namespace options to seed paths
Nov 23, 2017
a3bb5fd
Merge remote-tracking branch 'upstream/master'
Nov 24, 2017
97c2794
Fix missing call to empty seeds as they are iterated
Nov 24, 2017
b6e83ba
Allow the option to specify the db reference when running specific seeds
Nov 24, 2017
c343f81
Merge branch 'multiple-adapters'
Nov 24, 2017
d775790
Fix PHPStan analysis errors
Nov 24, 2017
de5f2de
Fix undefined variable errors
Nov 24, 2017
25e9054
Fix undefined index in migration paths
Nov 24, 2017
7984b3b
Fix missing parameter in test
Nov 24, 2017
c54c0ac
Fix missing params in test configs
Nov 24, 2017
b809d49
Code style fixes
Nov 24, 2017
ee7f84d
Additional missing tests
Nov 24, 2017
0946002
Test removing default_database param
Nov 24, 2017
2ee7b3b
Missing unit tests
Nov 24, 2017
84aa460
Fix exception being thrown when a seed doesn't exist in a preceeding …
Dec 18, 2017
764be65
Merge branch 'master' into master
sincilite Dec 19, 2017
da3a03c
Fix PHPCBF error
Dec 20, 2017
480cc82
Fix broken tests
Dec 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 119 additions & 3 deletions src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ 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 [$dbRef]= $envOptions;
break;
} else {
$configs [$dbRef]= array_merge($adapterOptions, ['dbRef' => $dbRef]);
}
}
}

return $configs;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -243,16 +273,59 @@ public function getConfigFilePath()
/**
* {@inheritdoc}
*/
public function getMigrationPaths()
public function getMigrationPaths($environment = null, $dbReference = null)
{
$paths = [];

if (!isset($this->values['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['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'])) {
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 (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])) {
foreach ($this->values['environments'][$env] as $dbReference => $properties) {
if (!is_array($properties)) {
continue;
}

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;
}
}
}
}
}
if (count($paths) > 0) {
$this->values['paths']['migrations']= $paths;
}
}
}

return $this->values['paths']['migrations'];
}

Expand All @@ -272,8 +345,11 @@ public function getMigrationBaseClassName($dropNamespace = true)
/**
* {@inheritdoc}
*/
public function getSeedPaths()
public function getSeedPaths($environment = null, $dbReference = null)
{

$paths = [];

if (!isset($this->values['paths']['seeds'])) {
throw new \UnexpectedValueException('Seeds path missing from config file');
}
Expand All @@ -282,6 +358,46 @@ public function getSeedPaths()
$this->values['paths']['seeds'] = [$this->values['paths']['seeds']];
}

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 {
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 (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])) {
foreach ($this->values['environments'][$env] as $dbReference => $properties) {
if (!is_array($properties)) {
continue;
}

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;
}
}
}
}
}
}
if (count($paths) > 0) {
$this->values['paths']['seeds']= $paths;
}
}

return $this->values['paths']['seeds'];
}

Expand Down
14 changes: 12 additions & 2 deletions src/Phinx/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down Expand Up @@ -91,16 +99,18 @@ 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.
*
* @return string[]
*/
public function getSeedPaths();
public function getSeedPaths($environment = null, $dbReference = null);

/**
* Get the template file name.
Expand Down
38 changes: 38 additions & 0 deletions src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,44 @@ protected function verifySeedDirectory($path)
}
}

/**
* Output the database information
*
* @param array $info
* @param OutputInterface $output
* @return null|int
*/
public function outputEnvironmentInfo(array $info, OutputInterface $output)
{

if (isset($info['adapter'])) {
$output->writeln('<info>using adapter</info> ' . $info['adapter']);
}

if (isset($info['wrapper'])) {
$output->writeln('<info>using wrapper</info> ' . $info['wrapper']);
}

if (isset($info['id'])) {
$output->writeln('<info>using name</info> ' . $info['id']);
}

if (isset($info['name'])) {
$output->writeln('<info>using database</info> ' . $info['name']);
} else {
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');

return 1;
}

if (isset($info['table_prefix'])) {
$output->writeln('<info>using table prefix</info> ' . $info['table_prefix']);
}
if (isset($info['table_suffix'])) {
$output->writeln('<info>using table suffix</info> ' . $info['table_suffix']);
}
}

/**
* Returns the migration template filename.
*
Expand Down
40 changes: 15 additions & 25 deletions src/Phinx/Console/Command/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

$this->bootstrap($input, $output);

$version = $input->getOption('target');
Expand All @@ -84,38 +85,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$envOptions = $this->getConfig()->getEnvironment($environment);
if (isset($envOptions['adapter'])) {
$output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
}
$databases = $this->getConfig()->getStorageConfigs($envOptions);

if (isset($envOptions['wrapper'])) {
$output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
}
// run the migrations
$start = microtime(true);
foreach ($databases as $adapterOptions) {
$this->getManager()->setMigrations(null);

if (isset($envOptions['name'])) {
$output->writeln('<info>using database</info> ' . $envOptions['name']);
} else {
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
if (isset($adapterOptions['dbRef'])) {
$this->getManager()->setDbRef($adapterOptions['dbRef']);
}

return 1;
}
$this->outputEnvironmentInfo($adapterOptions, $output);

if (isset($envOptions['table_prefix'])) {
$output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
}
if (isset($envOptions['table_suffix'])) {
$output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
if ($date !== null) {
$this->getManager()->migrateToDateTime($environment, new \DateTime($date));
} else {
$this->getManager()->migrate($environment, $version);
}
}

// 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('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');

Expand Down
48 changes: 24 additions & 24 deletions src/Phinx/Console/Command/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$envOptions = $config->getEnvironment($environment);
if (isset($envOptions['adapter'])) {
$output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
}

if (isset($envOptions['wrapper'])) {
$output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
}

if (isset($envOptions['name'])) {
$output->writeln('<info>using database</info> ' . $envOptions['name']);
}

$versionOrder = $this->getConfig()->getVersionOrder();
$output->writeln('<info>ordering by </info>' . $versionOrder . " time");

// rollback the specified environment
if ($date === null) {
$targetMustMatchVersion = true;
$target = $version;
} else {
$targetMustMatchVersion = false;
$target = $this->getTargetFromDate($date);
$databases = $config->getStorageConfigs($envOptions);

$start = microtime(true);

foreach ($databases as $adapterOptions) {
// rollback the specified environment
if ($date === null) {
$targetMustMatchVersion = true;
$target = $version;
} else {
$targetMustMatchVersion = false;
$target = $this->getTargetFromDate($date);
}

if (isset($adapterOptions['dbRef'])) {
$this->getManager()->setDbRef($adapterOptions['dbRef']);
}

$this->outputEnvironmentInfo($adapterOptions, $output);

$versionOrder = $this->getConfig()->getVersionOrder();
$output->writeln('<info>ordering by </info>' . $versionOrder . " time");

$this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion);
}

$start = microtime(true);
$this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion);
$end = microtime(true);

$output->writeln('');
Expand Down
Loading