Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all sessions when dropping database #1244

Merged
merged 1 commit into from Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions Spanner/src/Database.php
Expand Up @@ -394,6 +394,11 @@ public function updateDdlBatch(array $statements, array $options = [])
/**
* Drop the database.
*
* Please note that after a database is dropped, all sessions attached to it
* will be invalid and unusable. Calls to this method will clear any session
* pool attached to this database class instance and delete any sessions
* attached to the database class instance.
*
* **NOTE**: Requires `https://www.googleapis.com/auth/spanner.admin` scope.
*
* Example:
Expand All @@ -413,6 +418,15 @@ public function drop(array $options = [])
$this->connection->dropDatabase($options + [
'name' => $this->name
]);

if ($this->sessionPool) {
$this->sessionPool->clear();
}

if ($this->session) {

This comment was marked as spam.

$this->session->delete($options);
$this->session = null;
}
}

/**
Expand Down Expand Up @@ -1574,6 +1588,7 @@ public function __debugInfo()
'name' => $this->name,
'instance' => $this->instance,
'sessionPool' => $this->sessionPool,
'session' => $this->session,
];
}

Expand Down
1 change: 1 addition & 0 deletions Spanner/tests/Snippet/DatabaseTest.php
Expand Up @@ -71,6 +71,7 @@ public function setUp()
->willReturn($session->reveal());
$sessionPool->setDatabase(Argument::any())
->willReturn(null);
$sessionPool->clear()->willReturn(null);

$this->connection = $this->prophesize(ConnectionInterface::class);
$this->database = TestHelpers::stub(Database::class, [
Expand Down
42 changes: 42 additions & 0 deletions Spanner/tests/Unit/DatabaseTest.php
Expand Up @@ -38,6 +38,7 @@
use Google\Cloud\Spanner\Snapshot;
use Google\Cloud\Spanner\Timestamp;
use Google\Cloud\Spanner\Transaction;
use Google\Cloud\Spanner\V1\SpannerClient;
use Google\Cloud\Spanner\ValueMapper;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand Down Expand Up @@ -240,11 +241,52 @@ public function testDrop()
'name' => DatabaseAdminClient::databaseName(self::PROJECT, self::INSTANCE, self::DATABASE)
])->shouldBeCalled();

$this->sessionPool->clear()->shouldBeCalled()->willReturn(null);

$this->database->___setProperty('connection', $this->connection->reveal());

$this->database->drop();
}

/**
* @group spanneradmin
*/
public function testDropDeleteSession()
{
$this->connection->createSession(Argument::any())
->shouldBeCalled()
->willReturn([
'name' => SpannerClient::sessionName(self::PROJECT, self::INSTANCE, self::DATABASE, self::SESSION)
]);

$this->connection->beginTransaction(Argument::any())
->shouldBeCalled()
->willReturn([
'id' => self::TRANSACTION
]);

$this->connection->deleteSession(Argument::any())
->shouldBeCalled();

$this->connection->dropDatabase([
'name' => DatabaseAdminClient::databaseName(self::PROJECT, self::INSTANCE, self::DATABASE)
])->shouldBeCalled();

$database = TestHelpers::stub(Database::class, [
$this->connection->reveal(),
$this->instance->reveal(),
$this->lro->reveal(),
$this->lroCallables,
self::PROJECT,
self::DATABASE
]);

// This will set a session on the Database class.
$database->transaction();

$database->drop();
}

/**
* @group spanneradmin
*/
Expand Down