Skip to content

Commit aa3e95c

Browse files
author
vrana
committedJan 17, 2013
Cache connection in bin/storage
Summary: Connection takes .3s from dev server to master. Test Plan: $ bin/storage --trace upgrade --namespace x $ bin/storage --trace destroy --namespace x Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D4480
1 parent c7c25e1 commit aa3e95c

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed
 

‎scripts/sql/manage_storage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585

8686
try {
8787
queryfx(
88-
$api->getConn('meta_data', $select_database = false),
88+
$api->getConn(null),
8989
'SELECT 1');
9090
} catch (AphrontQueryException $ex) {
9191
echo phutil_console_format(

‎src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class PhabricatorStorageManagementAPI {
66
private $user;
77
private $password;
88
private $namespace;
9+
private $conns = array();
910

1011
public function setNamespace($namespace) {
1112
$this->namespace = $namespace;
@@ -62,19 +63,24 @@ public function getDatabaseList(array $patches) {
6263
return $list;
6364
}
6465

65-
public function getConn($fragment, $select_database = true) {
66-
return PhabricatorEnv::newObjectFromConfig(
66+
public function getConn($fragment) {
67+
$database = $this->getDatabaseName($fragment);
68+
$return = &$this->conns[$this->host][$this->user][$database];
69+
if (!$return) {
70+
$return = PhabricatorEnv::newObjectFromConfig(
6771
'mysql.implementation',
6872
array(
6973
array(
7074
'user' => $this->user,
7175
'pass' => $this->password,
7276
'host' => $this->host,
73-
'database' => $select_database
74-
? $this->getDatabaseName($fragment)
77+
'database' => $fragment
78+
? $database
7579
: null,
7680
),
7781
));
82+
}
83+
return $return;
7884
}
7985

8086
public function getAppliedPatches() {
@@ -90,7 +96,7 @@ public function getAppliedPatches() {
9096

9197
public function createDatabase($fragment) {
9298
queryfx(
93-
$this->getConn($fragment, $select_database = false),
99+
$this->getConn(null),
94100
'CREATE DATABASE IF NOT EXISTS %T COLLATE utf8_general_ci',
95101
$this->getDatabaseName($fragment));
96102
}
@@ -160,7 +166,7 @@ public function applyPatchSQL($sql) {
160166
$queries = preg_split('/;\s+/', $sql);
161167
$queries = array_filter($queries);
162168

163-
$conn = $this->getConn('meta_data', $select_database = false);
169+
$conn = $this->getConn(null);
164170

165171
foreach ($queries as $query) {
166172
$query = str_replace('{$NAMESPACE}', $this->namespace, $query);
@@ -172,7 +178,7 @@ public function applyPatchSQL($sql) {
172178
}
173179

174180
public function applyPatchPHP($script) {
175-
$schema_conn = $this->getConn('meta_data', $select_database = false);
181+
$schema_conn = $this->getConn(null);
176182
require_once $script;
177183
}
178184

‎src/infrastructure/storage/management/workflow/PhabricatorStorageManagementDestroyWorkflow.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function execute(PhutilArgumentParser $args) {
4242
$patches = $this->getPatches();
4343

4444
if ($args->getArg('unittest-fixtures')) {
45-
$conn = $api->getConn(null, false);
45+
$conn = $api->getConn(null);
4646
$databases = queryfx_all(
4747
$conn,
4848
'SELECT DISTINCT(TABLE_SCHEMA) AS db '.
@@ -64,7 +64,7 @@ public function execute(PhutilArgumentParser $args) {
6464
} else {
6565
echo "Dropping database '{$database}'...\n";
6666
queryfx(
67-
$api->getConn('meta_data', $select_database = false),
67+
$api->getConn(null),
6868
'DROP DATABASE IF EXISTS %T',
6969
$database);
7070
}

0 commit comments

Comments
 (0)
Failed to load comments.