From 0d8f29db07350711a8321f1a822d33ffccffd2fb Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Wed, 21 Jun 2023 23:17:13 +0000 Subject: [PATCH] feat(Spanner): add sample for update database (#1793) --- spanner/composer.json | 2 +- spanner/src/update_database.php | 61 +++++++++++++++++++++++++++++++++ spanner/test/spannerTest.php | 22 ++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 spanner/src/update_database.php diff --git a/spanner/composer.json b/spanner/composer.json index 109f502236..3680820374 100755 --- a/spanner/composer.json +++ b/spanner/composer.json @@ -1,5 +1,5 @@ { "require": { - "google/cloud-spanner": "^1.54.0" + "google/cloud-spanner": "^1.62.1" } } diff --git a/spanner/src/update_database.php b/spanner/src/update_database.php new file mode 100644 index 0000000000..4c90059055 --- /dev/null +++ b/spanner/src/update_database.php @@ -0,0 +1,61 @@ +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); diff --git a/spanner/test/spannerTest.php b/spanner/test/spannerTest.php index cfd5f0cb92..d1eb54a135 100644 --- a/spanner/test/spannerTest.php +++ b/spanner/test/spannerTest.php @@ -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 */