Skip to content

Commit

Permalink
Implement set-default command for setting default data-source
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 28, 2015
1 parent fc6e24f commit 051f332
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
21 changes: 13 additions & 8 deletions db/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ bootstrap:
- tests/bootstrap.php
schema:
auto_id: 1
# Customize your schema class loader
# loader: custom_schema_loader.php
# Customize your schema lookup paths
paths:
- tests
# cache:
# class: LazyRecord\Cache\Memcache
# servers:
# - { host: localhost, port: 11211 }
data_source:
default: sqlite
nodes:
sqlite:
dsn: 'sqlite:testing.sqlite3'
# dsn: 'sqlite::memory:'
driver: sqlite
user: null
pass: null
query_options: { }
connection_options: { }
pgsql:
dsn: 'pgsql:host=localhost;dbname=testing'
user: postgres
driver: pgsql
pass: null
query_options: { }
connection_options: { }
mysql:
dsn: 'mysql:host=localhost;dbname=testing'
user: testing
driver: mysql
pass: null
query_options: { }
connection_options: { 1002: 'SET NAMES utf8' }
50 changes: 24 additions & 26 deletions src/LazyRecord/Command/DataSourceCommand/SetDefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use SQLBuilder\Driver\PDODriverFactory;
use SQLBuilder\ArgumentArray;
use SQLBuilder\Universal\Query\CreateDatabaseQuery;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
use Exception;
use PDO;

class SetDefaultCommand extends BaseCommand
Expand All @@ -26,41 +29,36 @@ public function execute($defaultDataSource)
// force loading data source
$configLoader = $this->getConfigLoader(true);

$idList = $configLoader->getDataSourceIdList();
$dataSources = $configLoader->getDataSources();

if (!in_array($defaultDataSource, array_keys($dataSources))) {
$this->logger->error("Undefined data source ID: $defaultDataSource");
return false;
}

$config = $configLoader->getConfigStash();
$config['data_source']['default'] = $defaultDataSource;

$dsId = $this->getCurrentDataSourceId();
$ds = $configLoader->getDataSource($dsId);

$dsnParser = new DSNParser;
$dsn = $dsnParser->parse($ds['dsn']);

$dbName = $dsn->getAttribute('dbname');

$dsn->removeAttribute('dbname');

$this->logger->debug("Connection DSN: " . $dsn);

$pdo = new PDO($dsn, @$ds['user'], @$ds['pass'], @$ds['connection_options']);
$this->logger->debug("Checking symbol link file: " . $configLoader->symbolFilename);
if (!file_exists($configLoader->symbolFilename)) {
$this->logger->error($configLoader->symbolFilename . " is missing. please use lazy build-conf {filename} to update your config link.");
return false;
}

$q = new CreateDatabaseQuery($dbName);
if (isset($ds['charset'])) {
$q->characterSet($ds['charset']);
} else {
$q->characterSet('utf8');
$targetFile = readlink($configLoader->symbolFilename);
if ($targetFile === false || !file_exists($targetFile)) {
$this->logger->error('Missing target config file. incorrect symbol link.');
return false;
}

$queryDriver = PDODriverFactory::create($pdo);
$sql = $q->toSql($queryDriver, new ArgumentArray);
$this->logger->info($sql);
$this->logger->debug("Writing config back to $targetFile");

if ($pdo->query($sql) === false) {
list($statusCode, $errorCode, $message) = $pdo->errorInfo();
$this->logger->error("$statusCode:$errorCode $message");
$yaml = Yaml::dump($config, $inlineLevel = 4, $indentSpaces = 2, $exceptionOnInvalidType = true);
if (false === file_put_contents($targetFile, "---\n" . $yaml)) {
$this->logger->error("YAML config update failed: $targetFile");
return false;
}
$this->logger->info('Database created successfully.');
$this->logger->info("Config file is updated successfully.");
}

}
Expand Down

0 comments on commit 051f332

Please sign in to comment.