Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-14139: Implement Registry.ensureRun on Gen3 Butler #32

Merged
merged 1 commit into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions python/lsst/daf/butler/registries/sqlRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,15 @@ def addExecution(self, execution):
----------
execution : `Execution`
Instance to add to the `SqlRegistry`.
The given `Execution` must not already be present in the `SqlRegistry`
(or any other), therefore its `id` attribute must be `None`.
The given `Execution` must not already be present in the `SqlRegistry`.
"""
assert execution.id is None # Must not be preexisting
executionTable = self._schema.metadata.tables['Execution']
with self._engine.begin() as connection:
result = connection.execute(executionTable.insert().values(start_time=execution.startTime,
result = connection.execute(executionTable.insert().values(execution_id=execution.id,
start_time=execution.startTime,
end_time=execution.endTime,
host=execution.host))
# Reassign id, may have been `None`
execution._id = result.inserted_primary_key[0]

def getExecution(self, id):
Expand Down Expand Up @@ -482,6 +482,30 @@ def makeRun(self, collection):
self.addRun(run)
return run

def ensureRun(self, run):
"""Conditionally add a new `Run` to the `SqlRegistry`.

If the ``run.id`` is ``None`` or a `Run` with this `id` doesn't exist
in the `Registry` yet, add it. Otherwise, ensure the provided run is
identical to the one already in the registry.

Parameters
----------
run : `Run`
Instance to add to the `SqlRegistry`.

Raises
------
ValueError
If `run` already exists, but is not identical.
"""
if run.id is not None:
existingRun = self.getRun(id=run.id)
if run != existingRun:
raise ValueError("{} != existing: {}".format(run, existingRun))
return
self.addRun(run)

def addRun(self, run):
"""Add a new `Run` to the `SqlRegistry`.

Expand Down
13 changes: 13 additions & 0 deletions tests/test_sqlRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ def testRun(self):
# Inserting with a preexisting collection should fail
with self.assertRaises(ValueError):
registry.makeRun("one")
# Insert a new Run and check that ensureRun silently ignores it
collection = "dummy"
run = registry.makeRun(collection)
registry.ensureRun(run)
# Calling ensureRun with a different Run with the same id should fail
run2 = Run(collection="hello")
run2._id = run.id
with self.assertRaises(ValueError):
registry.ensureRun(run2)
run2._id = None
# Now it should work
registry.ensureRun(run2)
self.assertEqual(run2, registry.getRun(id=run2.id))

def testExecution(self):
registry = Registry.fromConfig(self.configFile)
Expand Down