@@ -200,7 +200,6 @@ def make_environment(load_ontologies, download_reference, add_demo_user):
200
200
with open (SETTINGS_FP , newline = None ) as f :
201
201
qdb .sql_connection .TRN .add (f .read ())
202
202
qdb .sql_connection .TRN .execute ()
203
-
204
203
# Insert the settings values to the database
205
204
sql = """INSERT INTO settings
206
205
(test, base_data_dir, base_work_dir)
@@ -211,7 +210,6 @@ def make_environment(load_ontologies, download_reference, add_demo_user):
211
210
qiita_config .working_dir ])
212
211
qdb .sql_connection .TRN .execute ()
213
212
create_layout (test = test , verbose = verbose )
214
-
215
213
patch (verbose = verbose , test = test )
216
214
217
215
if load_ontologies :
@@ -274,7 +272,16 @@ def drop_environment(ask_for_confirmation):
274
272
# Connect to the postgres server
275
273
with qdb .sql_connection .TRN :
276
274
qdb .sql_connection .TRN .add ("SELECT test FROM settings" )
277
- is_test_environment = qdb .sql_connection .TRN .execute_fetchflatten ()[0 ]
275
+ try :
276
+ is_test_environment = \
277
+ qdb .sql_connection .TRN .execute_fetchflatten ()[0 ]
278
+ except ValueError as e :
279
+ # if settings doesn't exist then is fine to treat this as a test
280
+ # environment and clean up
281
+ if 'UNDEFINED_TABLE. MSG: relation "settings"' in str (e ):
282
+ is_test_environment = True
283
+ else :
284
+ raise
278
285
qdb .sql_connection .TRN .close ()
279
286
280
287
if is_test_environment :
@@ -369,10 +376,6 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
369
376
Pulls the current patch from the settings table and applies all subsequent
370
377
patches found in the patches directory.
371
378
"""
372
- # we are going to open and close 2 main transactions; this is a required
373
- # change since patch 68.sql where we transition to jsonb for all info
374
- # files. The 2 main transitions are: (1) get the current settings,
375
- # (2) each patch in their independent transaction
376
379
with qdb .sql_connection .TRN :
377
380
qdb .sql_connection .TRN .add ("SELECT current_patch FROM settings" )
378
381
current_patch = qdb .sql_connection .TRN .execute_fetchlast ()
@@ -389,36 +392,31 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
389
392
else :
390
393
next_patch_index = sql_patch_files .index (current_sql_patch_fp ) + 1
391
394
392
- patch_update_sql = "UPDATE settings SET current_patch = %s"
395
+ if test :
396
+ with qdb .sql_connection .TRN :
397
+ _populate_test_db ()
393
398
399
+ patch_update_sql = "UPDATE settings SET current_patch = %s"
394
400
for sql_patch_fp in sql_patch_files [next_patch_index :]:
395
401
sql_patch_filename = basename (sql_patch_fp )
396
402
397
- py_patch_fp = corresponding_py_patch (
398
- splitext (basename (sql_patch_fp ))[0 ] + '.py' )
399
- py_patch_filename = basename (py_patch_fp )
400
-
401
- # patch 43.sql is when we started testing patches, then in patch
402
- # 68.sql is when we transitioned to jsonb for the info files; let's do
403
- # this in its own transition
404
- if sql_patch_filename == '68.sql' and test :
405
- with qdb .sql_connection .TRN :
406
- _populate_test_db ()
403
+ patch_prefix = splitext (basename (sql_patch_fp ))[0 ]
404
+ py_patch_fp = corresponding_py_patch (f'{ patch_prefix } .py' )
407
405
408
406
with qdb .sql_connection .TRN :
409
407
with open (sql_patch_fp , newline = None ) as patch_file :
410
408
if verbose :
411
409
print ('\t Applying patch %s...' % sql_patch_filename )
410
+
412
411
qdb .sql_connection .TRN .add (patch_file .read ())
413
412
qdb .sql_connection .TRN .add (
414
413
patch_update_sql , [sql_patch_filename ])
415
-
416
414
qdb .sql_connection .TRN .execute ()
417
415
418
416
if exists (py_patch_fp ):
419
417
if verbose :
420
418
print ('\t \t Applying python patch %s...'
421
- % py_patch_filename )
419
+ % basename ( py_patch_fp ) )
422
420
with open (py_patch_fp ) as py_patch :
423
421
exec (py_patch .read (), globals ())
424
422
@@ -427,7 +425,5 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
427
425
# for the test Study (1) so a lot of the tests actually expect this.
428
426
# Now, trying to regenerate directly in the populate_test_db might
429
427
# require too many dev hours so the easiest is just do it here
430
- # UPDATE 01/25/2021: moving to 81.sql as we added timestamps to
431
- # prep info files
432
- if test and sql_patch_filename == '81.sql' :
433
- qdb .study .Study (1 ).sample_template .generate_files ()
428
+ if test :
429
+ qdb .study .Study (1 ).sample_template .generate_files ()
0 commit comments