Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
b9cd6fff
Commit
b9cd6fff
authored
May 01, 2015
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix dropping database with active connection on PostgreSQL
parent
2bd2232a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
153 additions
and
0 deletions
+153
-0
PostgreSQL92Platform.php
lib/Doctrine/DBAL/Platforms/PostgreSQL92Platform.php
+8
-0
PostgreSqlPlatform.php
lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
+28
-0
PostgreSqlSchemaManager.php
lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php
+28
-0
SchemaManagerFunctionalTestCase.php
...BAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
+30
-0
SqliteSchemaManagerTest.php
.../Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php
+26
-0
AbstractPostgreSqlPlatformTestCase.php
...sts/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php
+22
-0
PostgreSQL92PlatformTest.php
...octrine/Tests/DBAL/Platforms/PostgreSQL92PlatformTest.php
+11
-0
No files found.
lib/Doctrine/DBAL/Platforms/PostgreSQL92Platform.php
View file @
b9cd6fff
...
...
@@ -72,4 +72,12 @@ class PostgreSQL92Platform extends PostgreSQL91Platform
parent
::
initializeDoctrineTypeMappings
();
$this
->
doctrineTypeMapping
[
'json'
]
=
'json_array'
;
}
/**
* {@inheritdoc}
*/
public
function
getCloseActiveDatabaseConnectionsSQL
(
$database
)
{
return
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '
$database
'"
;
}
}
lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
View file @
b9cd6fff
...
...
@@ -420,6 +420,34 @@ class PostgreSqlPlatform extends AbstractPlatform
return
'CREATE DATABASE '
.
$name
;
}
/**
* Returns the SQL statement for disallowing new connections on the given database.
*
* This is useful to force DROP DATABASE operations which could fail because of active connections.
*
* @param string $database The name of the database to disallow new connections for.
*
* @return string
*/
public
function
getDisallowDatabaseConnectionsSQL
(
$database
)
{
return
"UPDATE pg_database SET datallowconn = 'false' WHERE datname = '
$database
'"
;
}
/**
* Returns the SQL statement for closing currently active connections on the given database.
*
* This is useful to force DROP DATABASE operations which could fail because of active connections.
*
* @param string $database The name of the database to close currently active connections for.
*
* @return string
*/
public
function
getCloseActiveDatabaseConnectionsSQL
(
$database
)
{
return
"SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = '
$database
'"
;
}
/**
* {@inheritDoc}
*/
...
...
lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php
View file @
b9cd6fff
...
...
@@ -19,6 +19,7 @@
namespace
Doctrine\DBAL\Schema
;
use
Doctrine\DBAL\Exception\DriverException
;
use
Doctrine\DBAL\Types\Type
;
/**
...
...
@@ -100,6 +101,33 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
});
}
/**
* {@inheritdoc}
*/
public
function
dropDatabase
(
$database
)
{
try
{
parent
::
dropDatabase
(
$database
);
}
catch
(
DriverException
$exception
)
{
// If we have a SQLSTATE 55006, the drop database operation failed
// because of active connections on the database.
// To force dropping the database, we first have to close all active connections
// on that database and issue the drop database operation again.
if
(
$exception
->
getSQLState
()
!==
'55006'
)
{
throw
$exception
;
}
$this
->
_execSql
(
array
(
$this
->
_platform
->
getDisallowDatabaseConnectionsSQL
(
$database
),
$this
->
_platform
->
getCloseActiveDatabaseConnectionsSQL
(
$database
),
)
);
parent
::
dropDatabase
(
$database
);
}
}
/**
* {@inheritdoc}
*/
...
...
tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
View file @
b9cd6fff
...
...
@@ -42,6 +42,36 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this
->
_sm
=
$this
->
_conn
->
getSchemaManager
();
}
/**
* @group DBAL-1220
*/
public
function
testDropsDatabaseWithActiveConnections
()
{
if
(
!
$this
->
_sm
->
getDatabasePlatform
()
->
supportsCreateDropDatabase
())
{
$this
->
markTestSkipped
(
'Cannot drop Database client side with this Driver.'
);
}
$this
->
_sm
->
dropAndCreateDatabase
(
'test_drop_database'
);
$this
->
assertContains
(
'test_drop_database'
,
$this
->
_sm
->
listDatabases
());
$params
=
$this
->
_conn
->
getParams
();
$params
[
'dbname'
]
=
'test_drop_database'
;
$user
=
isset
(
$params
[
'user'
])
?
$params
[
'user'
]
:
null
;
$password
=
isset
(
$params
[
'password'
])
?
$params
[
'password'
]
:
null
;
$connection
=
$this
->
_conn
->
getDriver
()
->
connect
(
$params
,
$user
,
$password
);
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Driver\Connection'
,
$connection
);
$this
->
_sm
->
dropDatabase
(
'test_drop_database'
);
$this
->
assertNotContains
(
'test_drop_database'
,
$this
->
_sm
->
listDatabases
());
unset
(
$connection
);
}
/**
* @group DBAL-195
*/
...
...
tests/Doctrine/Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php
View file @
b9cd6fff
...
...
@@ -28,6 +28,32 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this
->
assertEquals
(
false
,
file_exists
(
$path
));
}
/**
* @group DBAL-1220
*/
public
function
testDropsDatabaseWithActiveConnections
()
{
$this
->
_sm
->
dropAndCreateDatabase
(
'test_drop_database'
);
$this
->
assertFileExists
(
'test_drop_database'
);
$params
=
$this
->
_conn
->
getParams
();
$params
[
'dbname'
]
=
'test_drop_database'
;
$user
=
isset
(
$params
[
'user'
])
?
$params
[
'user'
]
:
null
;
$password
=
isset
(
$params
[
'password'
])
?
$params
[
'password'
]
:
null
;
$connection
=
$this
->
_conn
->
getDriver
()
->
connect
(
$params
,
$user
,
$password
);
$this
->
assertInstanceOf
(
'Doctrine\DBAL\Driver\Connection'
,
$connection
);
$this
->
_sm
->
dropDatabase
(
'test_drop_database'
);
$this
->
assertFileNotExists
(
'test_drop_database'
);
unset
(
$connection
);
}
public
function
testRenameTable
()
{
$this
->
createTestTable
(
'oldname'
);
...
...
tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php
View file @
b9cd6fff
...
...
@@ -783,4 +783,26 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'ALTER INDEX idx_foo RENAME TO idx_foo_renamed'
,
);
}
/**
* @group DBAL-1220
*/
public
function
testReturnsDisallowDatabaseConnectionsSQL
()
{
$this
->
assertSame
(
"UPDATE pg_database SET datallowconn = 'false' WHERE datname = 'foo'"
,
$this
->
_platform
->
getDisallowDatabaseConnectionsSQL
(
'foo'
)
);
}
/**
* @group DBAL-1220
*/
public
function
testReturnsCloseActiveDatabaseConnectionsSQL
()
{
$this
->
assertSame
(
"SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = 'foo'"
,
$this
->
_platform
->
getCloseActiveDatabaseConnectionsSQL
(
'foo'
)
);
}
}
tests/Doctrine/Tests/DBAL/Platforms/PostgreSQL92PlatformTest.php
View file @
b9cd6fff
...
...
@@ -56,4 +56,15 @@ class PostgreSQL92PlatformTest extends AbstractPostgreSqlPlatformTestCase
$this
->
assertTrue
(
$this
->
_platform
->
hasDoctrineTypeMappingFor
(
'json'
));
$this
->
assertEquals
(
'json_array'
,
$this
->
_platform
->
getDoctrineTypeMapping
(
'json'
));
}
/**
* @group DBAL-1220
*/
public
function
testReturnsCloseActiveDatabaseConnectionsSQL
()
{
$this
->
assertSame
(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'foo'"
,
$this
->
_platform
->
getCloseActiveDatabaseConnectionsSQL
(
'foo'
)
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment