Skip to content

Commit d07f21a

Browse files
committed
Automatically create database if it doesn't yet exist
1 parent 2b73ae8 commit d07f21a

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/Command/SchemaLoadCommand.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
use Doctrine\DBAL\DriverManager;
1414
use Doctrine\DBAL\Schema\Schema;
1515
use Doctrine\DBAL\Schema\Comparator;
16+
use Doctrine\DBAL\Exception\ConnectionException;
17+
use PDO;
18+
use RuntimeException;
1619

1720
/**
1821
* @author Joost Faassen <j.faassen@linkorb.com>
1922
* @author Igor Mukhin <igor.mukhin@gmail.com>
2023
*/
21-
class SchemaLoadCommand extends Command
22-
{
24+
class SchemaLoadCommand extends Command {
2325
/**
2426
* {@inheritdoc}
2527
*/
2628
protected function configure()
2729
{
2830
$this
2931
->setName('schema:load')
30-
->setDescription('Load Alice fixture data into database')
32+
->setDescription('Load database schema into database')
3133
->addArgument(
3234
'filename',
3335
InputArgument::REQUIRED,
@@ -36,13 +38,13 @@ protected function configure()
3638
->addArgument(
3739
'url',
3840
InputArgument::REQUIRED,
39-
'Database connection details. You can use PDO url or just database name.'
41+
'Database connection details. You can use PDO url or just a database name.'
4042
)
4143
->addOption(
4244
'apply',
4345
null,
4446
InputOption::VALUE_NONE,
45-
'Apply allow you to synchronise schema'
47+
'Apply allow you to apply changes'
4648
)
4749
;
4850
}
@@ -55,6 +57,38 @@ public function execute(InputInterface $input, OutputInterface $output)
5557
$url = $input->getArgument('url');
5658
$filename = $input->getArgument('filename');
5759
$apply = $input->getOption('apply');
60+
61+
62+
$scheme = parse_url($url, PHP_URL_SCHEME);
63+
$user = parse_url($url, PHP_URL_USER);
64+
$pass = parse_url($url, PHP_URL_PASS);
65+
$host = parse_url($url, PHP_URL_HOST);
66+
$port = parse_url($url, PHP_URL_PORT);
67+
$dbname = substr(parse_url($url, PHP_URL_PATH), 1);
68+
if (!$port) {
69+
$port = 3306;
70+
}
71+
$dsn = sprintf(
72+
'%s:host=%s;port=%d',
73+
$scheme,
74+
$host,
75+
$port
76+
);
77+
//echo $dsn;exit();
78+
try {
79+
$pdo = new PDO($dsn, $user, $pass);
80+
} catch (\Exception $e) {
81+
throw new RuntimeException("Can't connect to server with provided address and credentials");
82+
}
83+
84+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
85+
$stmt = $pdo->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" . $dbname . "'");
86+
if (!$stmt->fetchColumn()) {
87+
$output->writeln("Creating database");
88+
$stmt = $pdo->query("CREATE DATABASE " . $dbname . "");
89+
} else {
90+
$output->writeln("Database exists...");
91+
}
5892

5993
$loader = LoaderFactory::getInstance()->getLoader($filename);
6094
$toSchema = $loader->loadSchema($filename);
@@ -65,6 +99,8 @@ public function execute(InputInterface $input, OutputInterface $output)
6599
$connectionParams = array(
66100
'url' => $dbmanager->getUrlByDatabaseName($url)
67101
);
102+
103+
68104
$connection = DriverManager::getConnection($connectionParams, $config);
69105

70106
$output->writeln(sprintf(
@@ -74,6 +110,7 @@ public function execute(InputInterface $input, OutputInterface $output)
74110
));
75111

76112
$schemaManager = $connection->getSchemaManager();
113+
77114
$fromSchema = $schemaManager->createSchema();
78115

79116
$comparator = new Comparator();

0 commit comments

Comments
 (0)