Skip to content

Commit

Permalink
feat(Spanner): enable drop db protection
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwarajanand committed Mar 28, 2023
1 parent cea265c commit 4c80591
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Spanner/src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public function listDatabases(array $args);
*/
public function createDatabase(array $args);

/**
* @param array $args
*/
public function updateDatabase(array $args);

/**
* @param array $args
*/
Expand Down
16 changes: 16 additions & 0 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,22 @@ public function createDatabase(array $args)
return $this->operationToArray($res, $this->serializer, $this->lroResponseMappers);
}

/**
* @param array $args
*/
public function updateDatabase(array $args)
{
$database = $this->pluck('database', $args);
$databaseInfo = $this->serializer->decodeMessage(new Database(), $database);
$databaseName = $databaseInfo->getName();
$updateMask = $this->serializer->decodeMessage(new FieldMask(), $this->pluck('updateMask', $args));
return $this->send([$this->getDatabaseAdminClient(), 'updateDatabase'], [
$databaseInfo,
$updateMask,
$this->addResourcePrefixHeader($args, $databaseName)
]);
}

/**
* @param array $args
*/
Expand Down
35 changes: 35 additions & 0 deletions Spanner/src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,41 @@ public function create(array $options = [])
return $this->resumeOperation($operation['name'], $operation);
}

/**
* Update an existing Cloud Spanner database.
*
* Example:
* ```
* $operation = $database->updateDatabase(['enableDropProtection' => true]);
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#updatedatabaserequest UpdateDatabaseRequest
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] {
* Configuration Options
*
* @type bool $enableDropProtection If true, delete operations for Database
* and Instance will be blocked. **Defaults to** `false`.
* }
* @return LongRunningOperation<Database>
*/
public function updateDatabase(array $options = [])
{
return $this->info = $this->connection->updateDatabase([
'database' => [
'name' => $this->name,
'enableDropProtection' => isset($options['enableDropProtection'])
? $options['enableDropProtection']
: false,
],
'updateMask' => [
'paths' => ['enable_drop_protection']
]
] + $options);
}

/**
* Restores to this database from a backup.
*
Expand Down
45 changes: 45 additions & 0 deletions Spanner/tests/System/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Google\Cloud\Spanner\Tests\System;

use Google\Cloud\Core\Exception\FailedPreconditionException;
use Google\Cloud\Core\LongRunning\LongRunningOperation;
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
Expand Down Expand Up @@ -129,6 +130,50 @@ public function testDatabase()
$this->assertEquals($db->ddl()[0], $stmt);
}

public function testDatabaseDropProtection()
{
$instance = self::$instance;

$dbName = uniqid(self::TESTING_PREFIX);
$op = $instance->createDatabase($dbName);

$this->assertInstanceOf(LongRunningOperation::class, $op);
$db = $op->pollUntilComplete();
$this->assertInstanceOf(Database::class, $db);

$info = $db->reload();
$this->assertFalse($info['enableDropProtection']);

$op = $db->updateDatabase(['enableDropProtection' => true]);
$op->pollUntilComplete();
$info = $db->reload();
$this->assertTrue($info['enableDropProtection']);

// delete database should throw
$isDropThrows = false;
try {
$db->drop();
} catch (FailedPreconditionException $ex) {
$isDropThrows = true;
$this->assertStringContainsStringIgnoringCase(
'enable_drop_protection',
$ex->getMessage()
);
}
$this->assertTrue($isDropThrows);
$this->assertTrue($db->exists());

// disable drop databases config
$op = $db->updateDatabase(['enableDropProtection' => false]);
$op->pollUntilComplete();
$info = $db->reload();
$this->assertFalse($info['enableDropProtection']);

// drop should succeed
$db->drop();
$this->assertFalse($db->exists());
}

public function testCreateCustomerManagedInstanceConfiguration()
{
$this->skipEmulatorTests();
Expand Down
21 changes: 21 additions & 0 deletions Spanner/tests/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ public function testCreate()
$this->assertInstanceOf(LongRunningOperation::class, $op);
}

/**
* @group spanner-admin
*/
public function testUpdateDatabase()
{
$this->connection->updateDatabase(Argument::allOf(
Argument::withEntry('database', [
'name' => DatabaseAdminClient::databaseName(self::PROJECT, self::INSTANCE, self::DATABASE),
'enableDropProtection' => true,
]),
Argument::withEntry('updateMask', ['paths' => ['enable_drop_protection']])
))->shouldBeCalled()->willReturn([
'enableDropProtection' => true
]);

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

$res = $this->database->updateDatabase(['enableDropProtection' => true]);
$this->assertTrue($res['enableDropProtection']);
}

/**
* @group spanner-admin
*/
Expand Down

0 comments on commit 4c80591

Please sign in to comment.