Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3443488
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3443488
Commits
40e2b521
Commit
40e2b521
authored
16 years ago
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
#302300
by Crell: Support adding and ignoring database targets, for better replication support.
parent
d02d8998
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
includes/database/database.inc
+55
-0
55 additions, 0 deletions
includes/database/database.inc
modules/simpletest/tests/database_test.test
+50
-0
50 additions, 0 deletions
modules/simpletest/tests/database_test.test
with
105 additions
and
0 deletions
includes/database/database.inc
+
55
−
0
View file @
40e2b521
...
...
@@ -665,6 +665,13 @@ abstract class Database {
*/
static
protected
$databaseInfo
=
NULL
;
/**
* A list of key/target credentials to simply ignore.
*
* @var array
*/
static
protected
$ignoreTargets
=
array
();
/**
* The key of the currently active database connection.
*
...
...
@@ -695,6 +702,10 @@ final public static function getActiveConnection($target = 'default') {
* The corresponding connection object.
*/
final
public
static
function
getConnection
(
$key
=
'default'
,
$target
=
'default'
)
{
if
(
!
empty
(
self
::
$ignoreTargets
[
$key
][
$target
]))
{
$target
=
'default'
;
}
if
(
!
isset
(
self
::
$connections
[
$key
][
$target
]))
{
self
::
openConnection
(
$key
,
$target
);
}
...
...
@@ -772,6 +783,32 @@ final protected static function parseConnectionInfo() {
self
::
$databaseInfo
=
$databaseInfo
;
}
/**
* Add database connection info for a given key/target.
*
* This method allows the addition of new connection credentials at runtime.
* Under normal circumstances the preferred way to specify database credentials
* is via settings.php. However, this method allows them to be added at
* arbitrary times, such as during unit tests, when connecting to admin-defined
* third party databases, etc.
*
* If the given key/target pair already exists, this method will be ignored.
*
* @param $key
* The database key.
* @param $target
* The database target name.
* @param $info
* The database connection information, as it would be defined in settings.php.
* Note that the structure of this array will depend on the database driver
* it is connecting to.
*/
public
static
function
addConnectionInfo
(
$key
,
$target
,
$info
)
{
if
(
empty
(
self
::
$databaseInfo
[
$key
][
$target
]))
{
self
::
$databaseInfo
[
$key
][
$target
]
=
$info
;
}
}
/**
* Gets information on the specified database connection.
*
...
...
@@ -842,6 +879,24 @@ final protected static function openConnection($key, $target) {
throw
$e
;
}
}
/**
* Instruct the system to temporarily ignore a given key/target.
*
* At times we need to temporarily disable slave queries. To do so,
* call this method with the database key and the target to disable.
* That database key will then always fall back to 'default' for that
* key, even if it's defined.
*
* @param $key
* The database connection key.
* @param $target
* The target of the specified key to ignore.
*/
public
static
function
ignoreTarget
(
$key
,
$target
)
{
self
::
$ignoreTargets
[
$key
][
$target
]
=
TRUE
;
}
}
/**
...
...
This diff is collapsed.
Click to expand it.
modules/simpletest/tests/database_test.test
+
50
−
0
View file @
40e2b521
...
...
@@ -104,6 +104,56 @@ class DatabaseTestCase extends DrupalWebTestCase {
':priority'
=>
3
,
));
}
}
/**
* Test connection management.
*/
class
DatabaseConnectionTestCase
extends
DatabaseTestCase
{
function
getInfo
()
{
return
array
(
'name'
=>
t
(
'Connection tests'
),
'description'
=>
t
(
'Tests of the core database system.'
),
'group'
=>
t
(
'Database'
),
);
}
/**
* Test that connections return appropriate connection objects.
*/
function
testConnectionRouting
()
{
// Clone the master credentials to a slave connection.
// Note this will result in two independent connection objects that happen
// to point to the same place.
$connection_info
=
Database
::
getConnectionInfo
();
Database
::
addConnectionInfo
(
'default'
,
'slave'
,
$connection_info
[
'default'
][
'default'
]);
$db1
=
Database
::
getConnection
(
'default'
,
'default'
);
$db2
=
Database
::
getConnection
(
'default'
,
'slave'
);
$this
->
assertFalse
(
$db1
===
$db2
,
t
(
'Each target refers to a different connection.'
));
}
/**
* Test that connections return appropriate connection objects.
*/
function
testConnectionRoutingOverride
()
{
// Clone the master credentials to a slave connection.
// Note this will result in two independent connection objects that happen
// to point to the same place.
$connection_info
=
Database
::
getConnectionInfo
(
'default'
);
Database
::
addConnectionInfo
(
'default'
,
'slave'
,
$connection_info
[
'default'
]);
Database
::
ignoreTarget
(
'default'
,
'slave'
);
$db1
=
Database
::
getConnection
(
'default'
,
'default'
);
$db2
=
Database
::
getConnection
(
'default'
,
'slave'
);
$this
->
assertTrue
(
$db1
===
$db2
,
t
(
'Both targets refer to the same connection.'
));
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment