Skip to content

Instantly share code, notes, and snippets.

@ENM1989
Created December 25, 2016 23:16
Show Gist options
  • Save ENM1989/aedb1c7b56a76dcc49b3359b80270786 to your computer and use it in GitHub Desktop.
Save ENM1989/aedb1c7b56a76dcc49b3359b80270786 to your computer and use it in GitHub Desktop.
<?php
/**
* Phinx recipe for Deployer
*
* @author Alexey Boyko <ket4yiit@gmail.com>
* @copyright 2016 Alexey Boyko
* @license MIT https://github.com/deployphp/recipes/blob/master/LICENSE
*
* @link https://github.com/deployphp/recipes
*
* @see http://deployer.org
* @see https://phinx.org
*/
namespace Deployer;
/**
* Get Phinx command
*
* @return Path to Phinx
*/
function phinx_path() {
$isExistsCmd = 'if [ -f %s ]; then echo true; fi';
try {
$phinxPath = run('which phinx')->toString();
} catch (\RuntimeException $e) {
$phinxPath = null;
}
if ($phinxPath !== null) {
return 'phinx';
} else if (run(
sprintf(
$isExistsCmd,
'{{release_path}}/vendor/bin/phinx'
)
)->toBool()
) {
return "{{release_path}}/vendor/bin/phinx";
} else if (run(
sprintf(
$isExistsCmd,
'~/.composer/vendor/bin/phinx'
)
)->toBool()
) {
return '~/.composer/vendor/bin/phinx';
} else {
throw new \RuntimeException(
'Cannot find phinx. Please specify path to phinx manually'
);
}
}
/**
* Make Phinx command from env options
*
* @param string $cmdName Name of command
* @param array $conf Command options(config)
*
* @return string Phinx command to execute
*/
function phinx_get_cmd($cmdName, $conf) {
$phinx = phinx_path();
$phinxCmd = "$phinx $cmdName";
$options = '';
foreach ($conf as $name => $value) {
$options .= " --$name $value";
}
$phinxCmd .= $options;
return $phinxCmd;
}
/**
* Returns options array that allowed for command
*
* @param array $allowedOptions List of allowed options
*
* @return array Array of options
*/
function phinx_get_allowed_config($allowedOptions) {
$opts = [];
try {
foreach (get('phinx') as $key => $val) {
if (in_array($key, $allowedOptions)) {
$opts[$key] = $val;
}
}
} catch (\RuntimeException $e) {
}
return $opts;
}
desc('Migrating database by phinx');
task(
'phinx:migrate', function () {
$allowedOptions = [
'configuration',
'date',
'environment',
'target',
'parser'
];
$conf = phinx_get_allowed_config($allowedOptions);
cd('{{release_path}}');
$phinxCmd = phinx_get_cmd('migrate', $conf);
run($phinxCmd);
cd('{{deploy_path}}');
}
);
task(
'phinx:rollback', function () {
$allowedOptions = [
'configuration',
'date',
'environment',
'target',
'parser'
];
$conf = phinx_get_allowed_config($allowedOptions);
cd('{{release_path}}');
$phinxCmd = phinx_get_cmd('rollback', $conf);
run($phinxCmd);
cd('{{deploy_path}}');
}
);
task(
'phinx:seed', function () {
$allowedOptions = [
'configuration',
'environment',
'parser',
'seed'
];
$conf = phinx_get_allowed_config($allowedOptions);
cd('{{release_path}}');
$phinxCmd = phinx_get_cmd('seed:run', $conf);
run($phinxCmd);
cd('{{deploy_path}}');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment