30
30
# [START spanner_create_database_with_property_graph]
31
31
def create_database_with_property_graph (instance_id , database_id ):
32
32
"""Creates a database, tables and a property graph for sample data."""
33
- from google .cloud .spanner_admin_database_v1 .types import \
34
- spanner_database_admin
33
+ from google .cloud .spanner_admin_database_v1 .types import spanner_database_admin
35
34
36
35
spanner_client = spanner .Client ()
37
36
database_admin_api = spanner_client .database_admin_api
@@ -104,8 +103,7 @@ def create_database_with_property_graph(instance_id, database_id):
104
103
def update_allow_commit_timestamps (instance_id , database_id ):
105
104
"""Alters table column(s) to support commit timestamps."""
106
105
107
- from google .cloud .spanner_admin_database_v1 .types import \
108
- spanner_database_admin
106
+ from google .cloud .spanner_admin_database_v1 .types import spanner_database_admin
109
107
110
108
spanner_client = spanner .Client ()
111
109
database_admin_api = spanner_client .database_admin_api
@@ -117,7 +115,8 @@ def update_allow_commit_timestamps(instance_id, database_id):
117
115
statements = [
118
116
"""ALTER TABLE AccountTransferAccount
119
117
ALTER COLUMN create_time
120
- SET OPTIONS (allow_commit_timestamp = true)""" ],
118
+ SET OPTIONS (allow_commit_timestamp = true)"""
119
+ ],
121
120
)
122
121
123
122
operation = database_admin_api .update_database_ddl (request )
@@ -147,42 +146,42 @@ def insert_data(instance_id, database_id):
147
146
table = "Account" ,
148
147
columns = ("id" , "create_time" , "is_blocked" , "nick_name" ),
149
148
values = [
150
- (7 , ' 2020-01-10T06:22:20.12Z' , False , "Vacation Fund" ),
151
- (16 , ' 2020-01-27T17:55:09.12Z' , True , "Vacation Fund" ),
152
- (20 , ' 2020-02-18T05:44:20.12Z' , False , "Rainy Day Fund" )
149
+ (7 , " 2020-01-10T06:22:20.12Z" , False , "Vacation Fund" ),
150
+ (16 , " 2020-01-27T17:55:09.12Z" , True , "Vacation Fund" ),
151
+ (20 , " 2020-02-18T05:44:20.12Z" , False , "Rainy Day Fund" ),
153
152
],
154
153
)
155
154
156
155
batch .insert (
157
156
table = "Person" ,
158
157
columns = ("id" , "name" , "birthday" , "country" , "city" ),
159
158
values = [
160
- (1 , "Alex" , ' 1991-12-21T00:00:00.12Z' , "Australia" ," Adelaide" ),
161
- (2 , "Dana" , ' 1980-10-31T00:00:00.12Z' , "Czech_Republic" , "Moravia" ),
162
- (3 , "Lee" , ' 1986-12-07T00:00:00.12Z' , "India" , "Kollam" )
159
+ (1 , "Alex" , " 1991-12-21T00:00:00.12Z" , "Australia" , " Adelaide" ),
160
+ (2 , "Dana" , " 1980-10-31T00:00:00.12Z" , "Czech_Republic" , "Moravia" ),
161
+ (3 , "Lee" , " 1986-12-07T00:00:00.12Z" , "India" , "Kollam" ),
163
162
],
164
163
)
165
164
166
165
batch .insert (
167
166
table = "AccountTransferAccount" ,
168
167
columns = ("id" , "to_id" , "amount" , "create_time" , "order_number" ),
169
168
values = [
170
- (7 , 16 , 300.0 , ' 2020-08-29T15:28:58.12Z' , "304330008004315" ),
171
- (7 , 16 , 100.0 , ' 2020-10-04T16:55:05.12Z' , "304120005529714" ),
172
- (16 , 20 , 300.0 , ' 2020-09-25T02:36:14.12Z' , "103650009791820" ),
173
- (20 , 7 , 500.0 , ' 2020-10-04T16:55:05.12Z' , "304120005529714" ),
174
- (20 , 16 , 200.0 , ' 2020-10-17T03:59:40.12Z' , "302290001255747" )
169
+ (7 , 16 , 300.0 , " 2020-08-29T15:28:58.12Z" , "304330008004315" ),
170
+ (7 , 16 , 100.0 , " 2020-10-04T16:55:05.12Z" , "304120005529714" ),
171
+ (16 , 20 , 300.0 , " 2020-09-25T02:36:14.12Z" , "103650009791820" ),
172
+ (20 , 7 , 500.0 , " 2020-10-04T16:55:05.12Z" , "304120005529714" ),
173
+ (20 , 16 , 200.0 , " 2020-10-17T03:59:40.12Z" , "302290001255747" ),
175
174
],
176
175
)
177
176
178
177
batch .insert (
179
178
table = "PersonOwnAccount" ,
180
179
columns = ("id" , "account_id" , "create_time" ),
181
180
values = [
182
- (1 , 7 , ' 2020-01-10T06:22:20.12Z' ),
183
- (2 , 20 , ' 2020-01-27T17:55:09.12Z' ),
184
- (3 , 16 , ' 2020-02-18T05:44:20.12Z' )
185
- ]
181
+ (1 , 7 , " 2020-01-10T06:22:20.12Z" ),
182
+ (2 , 20 , " 2020-01-27T17:55:09.12Z" ),
183
+ (3 , 16 , " 2020-02-18T05:44:20.12Z" ),
184
+ ],
186
185
)
187
186
188
187
print ("Inserted data." )
@@ -207,7 +206,7 @@ def insert_accounts(transaction):
207
206
"INSERT INTO Account (id, create_time, is_blocked) "
208
207
" VALUES"
209
208
" (1, CAST('2000-08-10 08:18:48.463959-07:52' AS TIMESTAMP), false),"
210
- " (2, CAST('2000-08-12 08:18:48 .463959-07:52 ' AS TIMESTAMP), true)"
209
+ " (2, CAST('2000-08-12 07:13:16 .463959-03:41 ' AS TIMESTAMP), true)"
211
210
)
212
211
213
212
print ("{} record(s) inserted into Account." .format (row_ct ))
@@ -216,8 +215,8 @@ def insert_transfers(transaction):
216
215
row_ct = transaction .execute_update (
217
216
"INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) "
218
217
" VALUES"
219
- " (1, 2, PENDING_COMMIT_TIMESTAMP( ), 100),"
220
- " (1, 1, PENDING_COMMIT_TIMESTAMP( ), 200) "
218
+ " (1, 2, CAST('2000-09-11 03:11:18.463959-06:36' AS TIMESTAMP ), 100),"
219
+ " (1, 1, CAST('2000-09-12 04:09:34.463959-05:12' AS TIMESTAMP ), 200) "
221
220
)
222
221
223
222
print ("{} record(s) inserted into AccountTransferAccount." .format (row_ct ))
@@ -244,14 +243,14 @@ def update_accounts(transaction):
244
243
"UPDATE Account SET is_blocked = false WHERE id = 2"
245
244
)
246
245
247
- print ("{} record(s) updated." .format (row_ct ))
246
+ print ("{} Account record(s) updated." .format (row_ct ))
248
247
249
248
def update_transfers (transaction ):
250
249
row_ct = transaction .execute_update (
251
250
"UPDATE AccountTransferAccount SET amount = 300 WHERE id = 1 AND to_id = 2"
252
251
)
253
252
254
- print ("{} record(s) updated." .format (row_ct ))
253
+ print ("{} AccountTransferAccount record(s) updated." .format (row_ct ))
255
254
256
255
database .run_in_transaction (update_accounts )
257
256
database .run_in_transaction (update_transfers )
@@ -279,7 +278,7 @@ def update_accounts(transaction):
279
278
" RETURN b.id}"
280
279
)
281
280
282
- print ("{} record(s) updated." .format (row_ct ))
281
+ print ("{} Account record(s) updated." .format (row_ct ))
283
282
284
283
database .run_in_transaction (update_accounts )
285
284
@@ -351,14 +350,12 @@ def delete_transfers(transaction):
351
350
"DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2"
352
351
)
353
352
354
- print ("{} record(s) deleted." .format (row_ct ))
353
+ print ("{} AccountTransferAccount record(s) deleted." .format (row_ct ))
355
354
356
355
def delete_accounts (transaction ):
357
- row_ct = transaction .execute_update (
358
- "DELETE FROM Account WHERE id = 2"
359
- )
356
+ row_ct = transaction .execute_update ("DELETE FROM Account WHERE id = 2" )
360
357
361
- print ("{} record(s) deleted." .format (row_ct ))
358
+ print ("{} Account record(s) deleted." .format (row_ct ))
362
359
363
360
database .run_in_transaction (delete_transfers )
364
361
database .run_in_transaction (delete_accounts )
@@ -415,14 +412,18 @@ def delete_data(instance_id, database_id):
415
412
subparsers = parser .add_subparsers (dest = "command" )
416
413
subparsers .add_parser (
417
414
"create_database_with_property_graph" ,
418
- help = create_database_with_property_graph .__doc__ )
419
- subparsers .add_parser ("update_allow_commit_timestamps" ,
420
- help = update_allow_commit_timestamps .__doc__ )
415
+ help = create_database_with_property_graph .__doc__ ,
416
+ )
417
+ subparsers .add_parser (
418
+ "update_allow_commit_timestamps" , help = update_allow_commit_timestamps .__doc__
419
+ )
421
420
subparsers .add_parser ("insert_data" , help = insert_data .__doc__ )
422
421
subparsers .add_parser ("insert_data_with_dml" , help = insert_data_with_dml .__doc__ )
423
422
subparsers .add_parser ("update_data_with_dml" , help = update_data_with_dml .__doc__ )
424
- subparsers .add_parser ("update_data_with_graph_query_in_dml" ,
425
- help = update_data_with_graph_query_in_dml .__doc__ )
423
+ subparsers .add_parser (
424
+ "update_data_with_graph_query_in_dml" ,
425
+ help = update_data_with_graph_query_in_dml .__doc__ ,
426
+ )
426
427
subparsers .add_parser ("query_data" , help = query_data .__doc__ )
427
428
subparsers .add_parser (
428
429
"query_data_with_parameter" , help = query_data_with_parameter .__doc__
0 commit comments