@@ -46,24 +46,30 @@ def show_progress(block_num, block_size, total_size):
46
46
file_part = None
47
47
six .print_ ("" )
48
48
49
+ def getConnectionParameters ():
50
+ """Get the parameters for the connection to the database."""
49
51
50
- def buildConnectionString (dbname , mbHost , mbPort , mbUsername , mbPassword ):
51
- dbConnectionParam = "dbname={}" .format (dbname )
52
+ parameters = {}
52
53
53
- if mbPort is not None :
54
- dbConnectionParam += " port={}" . format ( mbPort )
54
+ if args . dbname :
55
+ parameters [ 'dbname' ] = args . dbname
55
56
56
- if mbHost is not None :
57
- dbConnectionParam += " host={}" . format ( mbHost )
57
+ if args . host :
58
+ parameters [ 'host' ] = args . host
58
59
59
- # TODO Is the escaping done here correct?
60
- if mbUsername is not None :
61
- dbConnectionParam += " user={}" .format (mbUsername )
60
+ if args .port :
61
+ parameters ['port' ] = args .port
62
62
63
- # TODO Is the escaping done here correct?
64
- if mbPassword is not None :
65
- dbConnectionParam += " password={}" .format (mbPassword )
66
- return dbConnectionParam
63
+ if args .username :
64
+ parameters ['user' ] = args .username
65
+
66
+ if args .password :
67
+ parameters ['password' ] = args .password
68
+
69
+ if args .schema_name :
70
+ parameters ['options' ] = "-c search_path=" + args .schema_name
71
+
72
+ return parameters
67
73
68
74
69
75
def _makeDefValues (keys ):
@@ -174,7 +180,7 @@ def _getTableKeys(table):
174
180
return keys
175
181
176
182
177
- def handleTable (table , insertJson , createFk , mbDbFile , dbConnectionParam ):
183
+ def handleTable (table , insertJson , createFk , mbDbFile ):
178
184
"""Handle the table including the post/pre processing."""
179
185
keys = _getTableKeys (table )
180
186
dbFile = mbDbFile if mbDbFile is not None else table + ".xml"
@@ -193,7 +199,7 @@ def handleTable(table, insertJson, createFk, mbDbFile, dbConnectionParam):
193
199
sys .exit (- 1 )
194
200
195
201
try :
196
- with pg .connect (dbConnectionParam ) as conn :
202
+ with pg .connect (** getConnectionParameters () ) as conn :
197
203
with conn .cursor () as cur :
198
204
try :
199
205
with open (dbFile , "rb" ) as xml :
@@ -273,29 +279,8 @@ def handleTable(table, insertJson, createFk, mbDbFile, dbConnectionParam):
273
279
six .print_ ("Warning from the database." , file = sys .stderr )
274
280
six .print_ ("pg.Warning: {0}" .format (str (w )), file = sys .stderr )
275
281
276
-
277
- def moveTableToSchema (table , schemaName , dbConnectionParam ):
278
- try :
279
- with pg .connect (dbConnectionParam ) as conn :
280
- with conn .cursor () as cur :
281
- # create the schema
282
- cur .execute ("CREATE SCHEMA IF NOT EXISTS " + schemaName + ";" )
283
- conn .commit ()
284
- # move the table to the right schema
285
- cur .execute ("ALTER TABLE " + table + " SET SCHEMA " + schemaName + ";" )
286
- conn .commit ()
287
- except pg .Error as e :
288
- six .print_ ("Error in dealing with the database." , file = sys .stderr )
289
- six .print_ ("pg.Error ({0}): {1}" .format (e .pgcode , e .pgerror ), file = sys .stderr )
290
- six .print_ (str (e ), file = sys .stderr )
291
- except pg .Warning as w :
292
- six .print_ ("Warning from the database." , file = sys .stderr )
293
- six .print_ ("pg.Warning: {0}" .format (str (w )), file = sys .stderr )
294
-
295
-
296
282
#############################################################
297
283
298
-
299
284
parser = argparse .ArgumentParser ()
300
285
parser .add_argument (
301
286
"-t" ,
@@ -384,10 +369,6 @@ def moveTableToSchema(table, schemaName, dbConnectionParam):
384
369
except NameError :
385
370
pass
386
371
387
- dbConnectionParam = buildConnectionString (
388
- args .dbname , args .host , args .port , args .username , args .password
389
- )
390
-
391
372
# load given file in table
392
373
if args .file and args .table :
393
374
table = args .table
@@ -398,14 +379,13 @@ def moveTableToSchema(table, schemaName, dbConnectionParam):
398
379
specialRules [("Posts" , "Body" )] = "NULL"
399
380
400
381
choice = input ("This will drop the {} table. Are you sure [y/n]?" .format (table ))
382
+
401
383
if len (choice ) > 0 and choice [0 ].lower () == "y" :
402
384
handleTable (
403
- table , args .insert_json , args .foreign_keys , args .file , dbConnectionParam
404
- )
385
+ table , args .insert_json , args .foreign_keys , args .file )
405
386
else :
406
387
six .print_ ("Cancelled." )
407
- if args .schema_name != "public" :
408
- moveTableToSchema (table , args .schema_name , dbConnectionParam )
388
+
409
389
exit (0 )
410
390
411
391
# load a project
@@ -453,7 +433,7 @@ def moveTableToSchema(table, schemaName, dbConnectionParam):
453
433
454
434
for table in tables :
455
435
six .print_ ("Load {0}.xml file" .format (table ))
456
- handleTable (table , args .insert_json , args .foreign_keys , None , dbConnectionParam )
436
+ handleTable (table , args .insert_json , args .foreign_keys , None )
457
437
# remove file
458
438
os .remove (table + ".xml" )
459
439
@@ -465,9 +445,6 @@ def moveTableToSchema(table, schemaName, dbConnectionParam):
465
445
else :
466
446
six .print_ ("Archive '{0}' deleted" .format (filepath ))
467
447
468
- if args .schema_name != "public" :
469
- for table in tables :
470
- moveTableToSchema (table , args .schema_name , dbConnectionParam )
471
448
exit (0 )
472
449
473
450
else :
0 commit comments