Commit 224b8000 authored by Samuel GAIST's avatar Samuel GAIST Committed by Samuel GAIST

[scripts][migrate_db_to_v2] Fix protocole template creation

The wrong name was used to generate the template.

Also improve handling of protocol template double creation.

If the content is different warn the user and ask
whether it should continue with the database creation.
parent 8ee5c54e
Pipeline #47464 passed with stage
in 38 minutes and 31 seconds
......@@ -47,11 +47,15 @@ Options:
-V, --version Show version
-v, --verbose Increases the output verbosity level
-p, --prefix=<path> Path where the prefix is contained [default: .]
-f, --force Force overwrite
"""
import copy
import os
import sys
from collections import OrderedDict
import simplejson as json
from docopt import docopt
from ..database import Database
......@@ -62,6 +66,24 @@ from ..utils import setup_logging
from ..version import __version__
def yes_or_no(question):
"""Helper method to get a yes or no answer
Inspired from:
https://gist.github.com/garrettdreyfus/8153571
"""
while "the answer is invalid":
try:
reply = str(input(question + " (y/n): ")).lower().strip() # nosec
except EOFError:
return False
if reply[:1] == "y":
return True
if reply[:1] == "n":
return False
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
......@@ -98,7 +120,7 @@ def main(argv=None):
new_db_name = f"{db_name}/{int(db_version) + 1}"
db_storage = DBStorage(prefix, new_db_name)
if db_storage.exists():
if db_storage.exists() and not args["--force"]:
logger.error(f"Database already exists: {new_db_name}")
return 1
......@@ -107,6 +129,7 @@ def main(argv=None):
database_json["protocols"] = []
for protocol in database.protocols:
sets = database.sets(protocol)
set_list = []
views = {}
......@@ -121,22 +144,35 @@ def main(argv=None):
set_.pop(key)
set_list.append(set_)
template = {"schema_version": 1, "sets": set_list}
template = OrderedDict(schema_version=1, sets=set_list)
pt_name = f"{protocol}/1"
pt_name = f"{database.protocols[protocol]['template']}/1"
pt_storage = PTStorage(prefix, pt_name)
protocol_template = ProtocolTemplate(prefix, template)
if not protocol_template.valid:
logger.error(
"Invalid protocol created:", "\n".join(protocol_template.errors)
)
return 1
if pt_storage.exists():
logger.info(f"Protocol template already exists: {pt_name}")
else:
protocol_template = ProtocolTemplate(prefix, template)
if not protocol_template.valid:
logger.info(f"Protocol template for {db_name} already exists: {pt_name}")
original_pt = ProtocolTemplate(prefix, pt_name)
# This will simplify the comparison as the data are loaded using
# OrderedDict
original_data = json.loads(json.dumps(original_pt.data))
new_data = json.loads(json.dumps(protocol_template.data))
if new_data != original_data:
logger.error(
"Invalid protocol created:", "\n".join(protocol_template.errors)
f"{pt_name} current content for {db_name} does not match original"
)
return 1
else:
protocol_template.write(pt_storage)
if not yes_or_no("Do you want to continue ?"):
return 1
else:
protocol_template.write(pt_storage)
protocol_entry = {"name": protocol, "template": pt_name, "views": views}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment