Skip to content

Commit 3bb2c2c

Browse files
authored
feat(Spanner): add spanner delete_data sample coverage (GoogleCloudPlatform#1108)
1 parent 790adcb commit 3bb2c2c

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

spanner/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ To run the Spanner Samples:
104104
create-storing-index Adds an storing index to the example database.
105105
create-table-timestamp Creates a table with a commit timestamp column.
106106
create-table-with-datatypes Creates a table with supported datatypes.
107+
delete-data Deletes sample data from the given database.
107108
delete-data-with-dml Remove sample data from the given database with a DML statement.
108109
deleted-data-with-partitioned-dml Deletes sample data in the database by partition with a DML statement.
109110
help Displays help for a command

spanner/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"src/read_only_transaction.php",
2323
"src/read_stale_data.php",
2424
"src/batch_query_data.php",
25+
"src/delete_data.php",
2526
"src/create_table_with_timestamp_column.php",
2627
"src/insert_data_with_timestamp_column.php",
2728
"src/add_timestamp_column.php",

spanner/spanner.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@
118118
);
119119
})
120120
);
121+
122+
// Delete data command
123+
$application->add((new Command('delete-data'))
124+
->setDefinition($inputDefinition)
125+
->setDescription('Deletes sample data from the given database.')
126+
->setCode(function ($input, $output) {
127+
delete_data(
128+
$input->getArgument('instance_id'),
129+
$input->getArgument('database_id')
130+
);
131+
})
132+
);
133+
121134
// Add column command
122135
$application->add((new Command('add-column'))
123136
->setDefinition($inputDefinition)

spanner/src/delete_data.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_delete_data]
27+
use Google\Cloud\Spanner\SpannerClient;
28+
use Google\Cloud\Spanner\KeyRange;
29+
use Google\Cloud\Core\Exception\GoogleException;
30+
31+
/**
32+
* Deletes sample data from the given database.
33+
*
34+
* @param string $instanceId The Spanner instance ID.
35+
* @param string $databaseId The Spanner database ID.
36+
* @throws GoogleException
37+
*/
38+
function delete_data($instanceId, $databaseId)
39+
{
40+
$spanner = new SpannerClient();
41+
$instance = $spanner->instance($instanceId);
42+
$database = $instance->database($databaseId);
43+
44+
// Delete individual rows
45+
$albumsToDelete = $spanner->keySet([
46+
'keys' => [[2, 1], [2, 3]]
47+
]);
48+
$database->delete('Albums', $albumsToDelete);
49+
50+
// Delete a range of rows where the column key is >=3 and <5
51+
// NOTE: A KeyRange must include a start and end.
52+
// NOTE: startType and endType both default to KeyRange::TYPE_OPEN.
53+
$singersRange = $spanner->keyRange([
54+
'startType' => KeyRange::TYPE_CLOSED,
55+
'start' => [3],
56+
'endType' => KeyRange::TYPE_OPEN,
57+
'end' => [5]
58+
]);
59+
$singersToDelete = $spanner->keySet([
60+
'ranges' => [$singersRange]
61+
]);
62+
$database->delete('Singers', $singersToDelete);
63+
64+
// Delete remaining Singers rows, which will also delete the remaining
65+
// Albums rows because Albums was defined with ON DELETE CASCADE
66+
$remainingSingers = $spanner->keySet([
67+
'all' => true
68+
]);
69+
$database->delete('Singers', $remainingSingers);
70+
71+
print('Deleted data.' . PHP_EOL);
72+
}
73+
// [END spanner_delete_data]

spanner/test/spannerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,30 @@ public function testReadData()
141141
/**
142142
* @depends testInsertData
143143
*/
144+
public function testDeleteData()
145+
{
146+
$output = $this->runCommand('delete-data');
147+
$this->assertContains('Deleted data.' . PHP_EOL, $output);
148+
149+
$spanner = new SpannerClient();
150+
$instance = $spanner->instance(spannerTest::$instanceId);
151+
$database = $instance->database(spannerTest::$databaseId);
152+
153+
$results = $database->execute(
154+
'SELECT SingerId FROM Albums UNION ALL SELECT SingerId FROM Singers'
155+
);
156+
157+
foreach ($results as $row) {
158+
$this->fail("Not all data was deleted.");
159+
}
160+
161+
$output = $this->runCommand('insert-data');
162+
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
163+
}
164+
165+
/**
166+
* @depends testDeleteData
167+
*/
144168
public function testAddColumn()
145169
{
146170
$output = $this->runCommand('add-column');

0 commit comments

Comments
 (0)