Skip to content

Commit

Permalink
fix: update DELETE samples to match docs (#1072)
Browse files Browse the repository at this point in the history
[Docs](https://cloud.google.com/spanner/docs/modify-mutation-api#deleting_rows_in_a_table)
list a set of dot points on the multiple ways to perform deletes. The
updated samples now contain examples of these.
  • Loading branch information
skuruppu authored Jun 5, 2020
1 parent ab6dc62 commit 3336e04
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
57 changes: 49 additions & 8 deletions samples/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,60 @@ async function deleteData(instanceId, databaseId, projectId) {
const database = instance.database(databaseId);

// Instantiate Spanner table object
const singersTable = database.table('Singers');
const albumsTable = database.table('Albums');

// Deletes rows from the Singers table and the Albums table,
// because Albums table is defined with ON DELETE CASCADE.
// Deletes individual rows from the Albums table.
try {
const keys = [1, 2, 3, 4, 5];
await singersTable.deleteRows(keys);
console.log('Deleted data.');
const keys = [
[2, 1],
[2, 3],
];
await albumsTable.deleteRows(keys);
console.log('Deleted individual rows in Albums.');
} catch (err) {
console.error('ERROR:', err);
} finally {
await database.close();
}

// Delete a range of rows where the column key is >=3 and <5
database.runTransaction(async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
const [rowCount] = await transaction.runUpdate({
sql: 'DELETE FROM Singers WHERE SingerId >= 3 AND SingerId < 5',
});
console.log(`${rowCount} records deleted from Singers.`);
await transaction.commit();
} catch (err) {
console.error('ERROR:', err);
}
});

// Deletes remaining rows from the Singers table and the Albums table,
// because Albums table is defined with ON DELETE CASCADE.
database.runTransaction(async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
// The WHERE clause is required for DELETE statements to prevent
// accidentally deleting all rows in a table.
// https://cloud.google.com/spanner/docs/dml-syntax#where_clause
const [rowCount] = await transaction.runUpdate({
sql: 'DELETE FROM Singers WHERE true',
});
console.log(`${rowCount} records deleted from Singers.`);
await transaction.commit();
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
});
// [END spanner_delete_data]
}

Expand Down
4 changes: 3 additions & 1 deletion samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ describe('Spanner', () => {
let output = execSync(
`${crudCmd} delete ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`
);
assert.match(output, /Deleted data\./);
assert.include(output, 'Deleted individual rows in Albums.');
assert.include(output, '2 records deleted from Singers.');
assert.include(output, '3 records deleted from Singers.');
output = execSync(
`${crudCmd} insert ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`
);
Expand Down

0 comments on commit 3336e04

Please sign in to comment.