Skip to content

Commit

Permalink
feat(Spanner): add sample for update database (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwarajanand authored Jun 21, 2023
1 parent 384a0f0 commit 0d8f29d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spanner/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-spanner": "^1.54.0"
"google/cloud-spanner": "^1.62.1"
}
}
61 changes: 61 additions & 0 deletions spanner/src/update_database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/spanner/README.md
*/

namespace Google\Cloud\Samples\Spanner;

// [START spanner_update_database]
use Google\Cloud\Spanner\SpannerClient;

/**
* Updates the drop protection setting for a database.
* Example:
* ```
* update_database($instanceId, $databaseId);
* ```
*
* @param string $instanceId The Spanner instance ID.
* @param string $databaseId The Spanner database ID.
*/
function update_database(string $instanceId, string $databaseId): void
{
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);
printf(
'Updating database %s',
$database->name(),
);
$op = $database->updateDatabase(['enableDropProtection' => true]);
$op->pollUntilComplete();
$database->reload();
printf(
'Updated the drop protection for %s to %s' . PHP_EOL,
$database->name(),
$database->info()['enableDropProtection']
);
}
// [END spanner_update_database]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
22 changes: 22 additions & 0 deletions spanner/test/spannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ public function testCreateDatabaseWithEncryptionKey()
$this->assertStringContainsString('Created database en-test-', $output);
}

/**
* @depends testCreateDatabase
*/
public function testUpdateDatabase()
{
$output = $this->runFunctionSnippet('update_database', [
'instanceId' => self::$instanceId,
'databaseId' => self::$databaseId
]);
$this->assertStringContainsString(self::$databaseId, $output);
$this->assertStringContainsString(true, $output);

// reset the enableDropProtection for test tear down
$spanner = new SpannerClient();
$instance = $spanner->instance(self::$instanceId);
$database = $instance->database(self::$databaseId);
$op = $database->updateDatabase(['enableDropProtection' => false]);
$op->pollUntilComplete();
$database->reload();
$this->assertFalse($database->info()['enableDropProtection']);
}

/**
* @depends testCreateDatabase
*/
Expand Down

0 comments on commit 0d8f29d

Please sign in to comment.