Skip to content

Commit

Permalink
DatabaseInterface now checks if the database file that it is
Browse files Browse the repository at this point in the history
creating already exists and raises an error if it does. This
change is to prevent the unfortunate overwrite of a database.
  • Loading branch information
onufer committed May 14, 2020
1 parent c0edb11 commit 268b6f8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 6 additions & 5 deletions armi/bookkeeping/db/database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ def initDB(self):
interface stack (so that all the parameters have been updated) while the Main
Interface interacts first.
"""
if self.cs["reloadDBName"].lower() == (self.cs.caseTitle + ".h5").lower():
databaseName = self.cs.caseTitle + ".h5"
if os.path.exists(os.path.abspath(databaseName)):
raise ValueError(
"It appears that reloadDBName is the same as the case "
"title. This could lead to data loss! Rename the reload DB or the "
"case."
"There is already a database file {}.\nIt would be overwritten. "
"Please rename your case (settings file) to a name that does not yet "
"have a database.".format(os.path.abspath(databaseName))
)
self._db = Database3(self.cs.caseTitle + ".h5", "w")
self._db = Database3(databaseName, "w")
self._db.open()

# Grab geomString here because the DB-level has no access to the reactor or
Expand Down
4 changes: 3 additions & 1 deletion armi/bookkeeping/tests/test_databaseInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,14 @@ def test_convertDatabase(self):
class TestBadName(unittest.TestCase):
def test_badDBName(self):
cs = settings.Settings(os.path.join(TEST_ROOT, "armiRun.yaml"))
cs["reloadDBName"] = "aRmIRuN.h5" # weird casing to confirm robust checking
dbi = DatabaseInterface(None, cs)
with open("aRmIRuN.h5", "w"): # check case insensitive
pass # just make file, don't do anything with it
with self.assertRaises(ValueError):
# an error should be raised when the database loaded from
# has the same name as the run to avoid overwriting.
dbi.initDB()
os.remove("aRmIRuN.h5")


class TestStandardFollowOn(unittest.TestCase):
Expand Down

0 comments on commit 268b6f8

Please sign in to comment.