3030# [START spanner_create_database_with_property_graph]
3131def create_database_with_property_graph (instance_id , database_id ):
3232 """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
3534
3635 spanner_client = spanner .Client ()
3736 database_admin_api = spanner_client .database_admin_api
@@ -104,8 +103,7 @@ def create_database_with_property_graph(instance_id, database_id):
104103def update_allow_commit_timestamps (instance_id , database_id ):
105104 """Alters table column(s) to support commit timestamps."""
106105
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
109107
110108 spanner_client = spanner .Client ()
111109 database_admin_api = spanner_client .database_admin_api
@@ -117,7 +115,8 @@ def update_allow_commit_timestamps(instance_id, database_id):
117115 statements = [
118116 """ALTER TABLE AccountTransferAccount
119117 ALTER COLUMN create_time
120- SET OPTIONS (allow_commit_timestamp = true)""" ],
118+ SET OPTIONS (allow_commit_timestamp = true)"""
119+ ],
121120 )
122121
123122 operation = database_admin_api .update_database_ddl (request )
@@ -147,42 +146,42 @@ def insert_data(instance_id, database_id):
147146 table = "Account" ,
148147 columns = ("id" , "create_time" , "is_blocked" , "nick_name" ),
149148 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" ),
153152 ],
154153 )
155154
156155 batch .insert (
157156 table = "Person" ,
158157 columns = ("id" , "name" , "birthday" , "country" , "city" ),
159158 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" ),
163162 ],
164163 )
165164
166165 batch .insert (
167166 table = "AccountTransferAccount" ,
168167 columns = ("id" , "to_id" , "amount" , "create_time" , "order_number" ),
169168 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" ),
175174 ],
176175 )
177176
178177 batch .insert (
179178 table = "PersonOwnAccount" ,
180179 columns = ("id" , "account_id" , "create_time" ),
181180 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+ ],
186185 )
187186
188187 print ("Inserted data." )
@@ -207,7 +206,7 @@ def insert_accounts(transaction):
207206 "INSERT INTO Account (id, create_time, is_blocked) "
208207 " VALUES"
209208 " (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)"
211210 )
212211
213212 print ("{} record(s) inserted into Account." .format (row_ct ))
@@ -216,8 +215,8 @@ def insert_transfers(transaction):
216215 row_ct = transaction .execute_update (
217216 "INSERT INTO AccountTransferAccount (id, to_id, create_time, amount) "
218217 " 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) "
221220 )
222221
223222 print ("{} record(s) inserted into AccountTransferAccount." .format (row_ct ))
@@ -244,14 +243,14 @@ def update_accounts(transaction):
244243 "UPDATE Account SET is_blocked = false WHERE id = 2"
245244 )
246245
247- print ("{} record(s) updated." .format (row_ct ))
246+ print ("{} Account record(s) updated." .format (row_ct ))
248247
249248 def update_transfers (transaction ):
250249 row_ct = transaction .execute_update (
251250 "UPDATE AccountTransferAccount SET amount = 300 WHERE id = 1 AND to_id = 2"
252251 )
253252
254- print ("{} record(s) updated." .format (row_ct ))
253+ print ("{} AccountTransferAccount record(s) updated." .format (row_ct ))
255254
256255 database .run_in_transaction (update_accounts )
257256 database .run_in_transaction (update_transfers )
@@ -279,7 +278,7 @@ def update_accounts(transaction):
279278 " RETURN b.id}"
280279 )
281280
282- print ("{} record(s) updated." .format (row_ct ))
281+ print ("{} Account record(s) updated." .format (row_ct ))
283282
284283 database .run_in_transaction (update_accounts )
285284
@@ -351,14 +350,12 @@ def delete_transfers(transaction):
351350 "DELETE FROM AccountTransferAccount WHERE id = 1 AND to_id = 2"
352351 )
353352
354- print ("{} record(s) deleted." .format (row_ct ))
353+ print ("{} AccountTransferAccount record(s) deleted." .format (row_ct ))
355354
356355 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" )
360357
361- print ("{} record(s) deleted." .format (row_ct ))
358+ print ("{} Account record(s) deleted." .format (row_ct ))
362359
363360 database .run_in_transaction (delete_transfers )
364361 database .run_in_transaction (delete_accounts )
@@ -415,14 +412,18 @@ def delete_data(instance_id, database_id):
415412 subparsers = parser .add_subparsers (dest = "command" )
416413 subparsers .add_parser (
417414 "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+ )
421420 subparsers .add_parser ("insert_data" , help = insert_data .__doc__ )
422421 subparsers .add_parser ("insert_data_with_dml" , help = insert_data_with_dml .__doc__ )
423422 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+ )
426427 subparsers .add_parser ("query_data" , help = query_data .__doc__ )
427428 subparsers .add_parser (
428429 "query_data_with_parameter" , help = query_data_with_parameter .__doc__
0 commit comments