Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample to delete data. #1872

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ def insert_data(instance_id, database_id):
# [END spanner_insert_data]


# [START spanner_delete_data]
def delete_data(instance_id, database_id):
"""Deletes sample data from the given database.

The database, table, and data must already exist and can be created using
`create_database` and `insert_data`.
"""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

singers_to_delete = spanner.KeySet(
keys=[[1], [2], [3], [4], [5]])
albums_to_delete = spanner.KeySet(
keys=[[1, 1], [1, 2], [2, 1], [2, 2], [2, 3]])

with database.batch() as batch:
batch.delete('Albums', albums_to_delete)
batch.delete('Singers', singers_to_delete)

print('Deleted data.')
# [END spanner_delete_data]


# [START spanner_query_data]
def query_data(instance_id, database_id):
"""Queries sample data from the database using SQL."""
Expand Down Expand Up @@ -1027,6 +1051,7 @@ def delete_data_with_partitioned_dml(instance_id, database_id):

subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('create_database', help=create_database.__doc__)
subparsers.add_parser('delete_data', help=delete_data.__doc__)
subparsers.add_parser('insert_data', help=insert_data.__doc__)
subparsers.add_parser('query_data', help=query_data.__doc__)
subparsers.add_parser('read_data', help=read_data.__doc__)
Expand Down Expand Up @@ -1100,6 +1125,8 @@ def delete_data_with_partitioned_dml(instance_id, database_id):
create_database(args.instance_id, args.database_id)
elif args.command == 'insert_data':
insert_data(args.instance_id, args.database_id)
elif args.command == 'delete_data':
delete_data(args.instance_id, args.database_id)
elif args.command == 'query_data':
query_data(args.instance_id, args.database_id)
elif args.command == 'read_data':
Expand Down
7 changes: 7 additions & 0 deletions spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def test_insert_data(capsys):
assert 'Inserted data' in out


def test_delete_data(capsys):
snippets.delete_data(INSTANCE_ID, DATABASE_ID)
snippets.insert_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Deleted data' in out


def test_query_data(capsys):
snippets.query_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
Expand Down