-
Notifications
You must be signed in to change notification settings - Fork 31
feat: autocommit sample #552
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
32be3dc
feat: autocommit sample
AlisskaPie 22e3633
fix # [END ..] line
AlisskaPie 0d67d2c
formatting fixes
AlisskaPie dd7bae4
Excess whitespaces
AlisskaPie 7390311
Merge branch 'master' into autocommit_samples
mf2199 c4676bf
Merge branch 'master' into autocommit_samples
c24t fb1b546
review fixes
AlisskaPie 7570ec9
Merge branch 'autocommit_samples' of https://github.com/q-logic/pytho…
AlisskaPie 4dedd72
add region tag product prefix
AlisskaPie 2c0a1e5
Merge branch 'master' into autocommit_samples
dinagraves 5960772
Using unique ID's for database creation
dinagraves 06ff1a7
Shortening DB name to 30 char
dinagraves 812c42b
small format fixes
AlisskaPie 04470d7
Merge branch 'master' into autocommit_samples
c24t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import argparse | ||
|
||
from google.cloud import spanner_dbapi | ||
from google.cloud.spanner_dbapi import connect | ||
|
||
|
||
def enable_autocommit_mode(instance_id, database_id): | ||
"""Enables autocommit mode.""" | ||
# [START spanner_enable_autocommit_mode] | ||
connection = connect(instance_id, database_id) | ||
connection.autocommit = True | ||
print("Autocommit mode is enabled.") | ||
|
||
cursor = connection.cursor() | ||
|
||
cursor.execute( | ||
"""CREATE TABLE Singers ( | ||
SingerId INT64 NOT NULL, | ||
FirstName STRING(1024), | ||
LastName STRING(1024), | ||
SingerInfo BYTES(MAX) | ||
) PRIMARY KEY (SingerId)""" | ||
) | ||
|
||
cursor.execute( | ||
"""INSERT INTO Singers (SingerId, FirstName, LastName) VALUES | ||
(12, 'Melissa', 'Garcia'), | ||
(13, 'Russell', 'Morales'), | ||
(14, 'Jacqueline', 'Long'), | ||
(15, 'Dylan', 'Shaw')""" | ||
) | ||
|
||
cursor.execute("""SELECT * FROM Singers WHERE SingerId = 13""") | ||
|
||
print( | ||
"SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*cursor.fetchone()) | ||
) | ||
|
||
connection.close() | ||
# [END spanner_enable_autocommit_mode] | ||
|
||
|
||
if __name__ == "__main__": | ||
c24t marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
parser.add_argument("instance_id", help="Your Cloud Spanner instance ID.") | ||
parser.add_argument( | ||
"--database-id", | ||
help="Your Cloud Spanner database ID.", | ||
default="example_db", | ||
) | ||
subparsers = parser.add_subparsers(dest="command") | ||
subparsers.add_parser( | ||
"enable_autocommit_mode", help=enable_autocommit_mode.__doc__ | ||
) | ||
args = parser.parse_args() | ||
if args.command == "enable_autocommit_mode": | ||
enable_autocommit_mode(args.instance_id, args.database_id) | ||
else: | ||
print(f"Command {args.command} did not match expected commands.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import uuid | ||
|
||
from google.api_core.exceptions import DeadlineExceeded | ||
from google.cloud import spanner | ||
import pytest | ||
|
||
from . import autocommit | ||
|
||
|
||
def unique_instance_id(): | ||
"""Creates a unique id for the database.""" | ||
return f"test-instance-{uuid.uuid4().hex[:10]}" | ||
|
||
|
||
def unique_database_id(): | ||
"""Creates a unique id for the database.""" | ||
return f"test-db-{uuid.uuid4().hex[:10]}" | ||
|
||
|
||
INSTANCE_ID = unique_instance_id() | ||
DATABASE_ID = unique_database_id() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def spanner_instance(): | ||
spanner_client = spanner.Client() | ||
config_name = ( | ||
f"{spanner_client.project_name}/instanceConfigs/regional-us-central1" | ||
) | ||
|
||
instance = spanner_client.instance(INSTANCE_ID, config_name) | ||
op = instance.create() | ||
op.result(120) # block until completion | ||
yield instance | ||
instance.delete() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def database(spanner_instance): | ||
"""Creates a temporary database that is removed after testing.""" | ||
db = spanner_instance.database(DATABASE_ID) | ||
db.create() | ||
yield db | ||
db.drop() | ||
|
||
|
||
def test_enable_autocommit_mode(capsys, database): | ||
autocommit.enable_autocommit_mode(INSTANCE_ID, DATABASE_ID) | ||
out, _ = capsys.readouterr() | ||
assert "Autocommit mode is enabled." in out | ||
assert "SingerId: 13, AlbumId: Russell, AlbumTitle: Morales" in out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.