Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ async def get_computation(
last_run: CompRunsAtDB | None = None
pipeline_state = RunningState.NOT_STARTED
with contextlib.suppress(ComputationalRunNotFoundError):
last_run = await comp_runs_repo.get(user_id=user_id, project_id=project_id)
last_run = await comp_runs_repo.get_latest_run_by_project(project_id=project_id)
pipeline_state = last_run.result

_logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ async def get(
raise ComputationalRunNotFoundError
return CompRunsAtDB.model_validate(row)

async def get_latest_run_by_project(
self,
project_id: ProjectID,
) -> CompRunsAtDB:
async with pass_or_acquire_connection(self.db_engine) as conn:
result = await conn.execute(
sa.select(comp_runs)
.where(comp_runs.c.project_uuid == f"{project_id}")
.order_by(desc(comp_runs.c.run_id))
.limit(1)
)
row = result.one_or_none()
if not row:
raise ComputationalRunNotFoundError
return CompRunsAtDB.model_validate(row)

async def list_(
self,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,75 @@ async def test_list_group_by_collection_run_id_state_priority_precedence(
assert len(items) == 1
collection_item = items[0]
assert collection_item.state == RunningState.FAILED


async def test_get_with_user_id_none_across_multiple_users(
sqlalchemy_async_engine: AsyncEngine,
run_metadata: RunMetadataDict,
faker: Faker,
publish_project: Callable[[], Awaitable[PublishedProject]],
create_registered_user: Callable[..., dict[str, Any]],
):
"""Test that get() with user_id=None retrieves the latest run regardless of user"""
published_project = await publish_project()

# Create a second user
second_user = create_registered_user()
# second_user_id = UserID(faker.pyint(min_value=1000))

# Create comp runs for the original user
comp_run_user1_iter1 = await CompRunsRepository(sqlalchemy_async_engine).create(
user_id=published_project.user["id"],
project_id=published_project.project.uuid,
iteration=None,
metadata=run_metadata,
use_on_demand_clusters=faker.pybool(),
dag_adjacency_list=published_project.pipeline.dag_adjacency_list,
)

# Create comp runs for the second user (this should increment iteration)
comp_run_user2_iter2 = await CompRunsRepository(sqlalchemy_async_engine).create(
user_id=second_user["id"],
project_id=published_project.project.uuid,
iteration=None,
metadata=run_metadata,
use_on_demand_clusters=faker.pybool(),
dag_adjacency_list=published_project.pipeline.dag_adjacency_list,
)

# Create another run for the first user (should be iteration 3)
comp_run_user1_iter3 = await CompRunsRepository(sqlalchemy_async_engine).create(
user_id=published_project.user["id"],
project_id=published_project.project.uuid,
iteration=None,
metadata=run_metadata,
use_on_demand_clusters=faker.pybool(),
dag_adjacency_list=published_project.pipeline.dag_adjacency_list,
)

# Verify iterations are correct
assert comp_run_user1_iter1.iteration == 1
assert comp_run_user2_iter2.iteration == 1
assert comp_run_user1_iter3.iteration == 2

# Test get with user_id=None should return the latest run (highest iteration)
latest_run = await CompRunsRepository(
sqlalchemy_async_engine
).get_latest_run_by_project(
project_id=published_project.project.uuid,
)
assert latest_run == comp_run_user1_iter3
assert latest_run.iteration == 2

# Test get with specific user_id still works
user1_latest = await CompRunsRepository(sqlalchemy_async_engine).get(
user_id=published_project.user["id"],
project_id=published_project.project.uuid,
)
assert user1_latest == comp_run_user1_iter3

user2_latest = await CompRunsRepository(sqlalchemy_async_engine).get(
user_id=second_user["id"],
project_id=published_project.project.uuid,
)
assert user2_latest == comp_run_user2_iter2
Loading