Skip to content

Commit

Permalink
db import script uses DB config instead of ENV vars directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Stark committed Feb 1, 2019
1 parent 58b5e73 commit 143deb3
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions bin/craft-copy-import-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
}

// Bootstrap Craft
/** @var \craft\console\Application $app */
$app = require $root . '/vendor/craftcms/cms/bootstrap/console.php';

if (count($argv) < 2 || stristr($argv[1], '.sql') == false) {
Expand All @@ -39,30 +40,30 @@
exit(1);
}

if (\Craft::$app->getIsInstalled()) {
if ($app->getIsInstalled()) {
echo "Craft is already installed!" . PHP_EOL;
if (!in_array('--force', $argv)) {
echo "Abort. No --force flag given." . PHP_EOL;
exit(1);
}
}

if (!getenv('DB_USER')) {
echo "No DB ENV vars found." . PHP_EOL;
if (!$app->getConfig()->getDb()->database) {
echo "No DB Config found." . PHP_EOL;
exit(1);
}

$cmd = 'mysql -u {DB_USER} -p{DB_PASSWORD} -h {DB_SERVER} {DB_DATABASE} < {file} && echo 1';
$db = $app->getConfig()->getDb();
$cmd = 'mysql -u {DB_USER} -p{DB_PASSWORD} -h {DB_SERVER} {DB_DATABASE} < {file} && echo 1';
$tokens = [
'{file}' => $file,
'{DB_USER}' => getenv('DB_USER'),
'{DB_PASSWORD}' => getenv('DB_PASSWORD'),
'{DB_SERVER}' => getenv('DB_SERVER'),
'{DB_DATABASE}' => getenv('DB_DATABASE'),
'{file}' => $file,
'{DB_USER}' => $db->user,
'{DB_PASSWORD}' => $db->password,
'{DB_SERVER}' => $db->server,
'{DB_DATABASE}' => $db->database,
];

$cmd = str_replace(array_keys($tokens), array_values($tokens), $cmd);

$cmd = str_replace(array_keys($tokens), array_values($tokens), $cmd);
$process = new \Symfony\Component\Process\Process($cmd);
$process->run();

Expand Down

0 comments on commit 143deb3

Please sign in to comment.