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

🏗️ Persist the session #372

Merged
merged 5 commits into from
Nov 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/guide/07-link-samples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5e532d56",
"id": "7117701d-eda9-4a1a-a011-f2fb1b9c49ff",
"metadata": {},
"outputs": [],
"source": [
"readout.dobjects.append(dobject)"
"dobject.readouts.append(readout)"
]
},
{
Expand Down Expand Up @@ -177,9 +177,9 @@
"metadata": {},
"outputs": [],
"source": [
"biosample.species_id = (\n",
" species.id\n",
") # need to have a better UX around this that clarifies why we have to do this"
"# the species record already exists, hence we only link the id instead of adding the full record\n",
"# need to have a more intuitive UX around this that clarifies why we have to do this\n",
"biosample.species_id = species.id"
]
},
{
Expand Down Expand Up @@ -259,11 +259,11 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f4c14773-eeac-4db8-95e5-364b5e7cd532",
"id": "d0a3783a-370d-4648-8bd1-6776f014b0f5",
"metadata": {},
"outputs": [],
"source": [
"biosample.dobjects.append(dobject)"
"dobject.biosamples.append(biosample)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions lamindb/_check_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from nbproject import __version__ as nbproject_v
from packaging import version

if version.parse(lndb_setup_v) != version.parse("0.17.0"):
raise RuntimeError("lamindb needs lndb_setup==0.17.0")
if version.parse(lndb_setup_v) != version.parse("0.18.0"):
raise RuntimeError("lamindb needs lndb_setup==0.18.0")

if version.parse(lnschema_core_v) != version.parse("0.20.0"):
raise RuntimeError("lamindb needs lnschema_core==0.20.0")
Expand Down
42 changes: 20 additions & 22 deletions lamindb/_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,27 @@ def delete(record: sqm.SQLModel):
Args:
record: One or multiple records as instances of `SQLModel`.
"""
with sqm.Session(settings.instance.db_engine()) as session:
if isinstance(record, DObject):
# delete usage events related to the dobject that's to be deleted
events = session.exec(
sqm.select(Usage).where(Usage.dobject_id == record.id)
)
for event in events:
session.delete(event)
session.commit()
# delete run_ins related to the dobject that's to be deleted
run_ins = session.exec(
sqm.select(RunIn).where(RunIn.dobject_id == record.id)
)
for run_in in run_ins:
session.delete(run_in)
session.commit()
session.delete(record)
session = settings.instance.session()
if isinstance(record, DObject):
# delete usage events related to the dobject that's to be deleted
events = session.exec(sqm.select(Usage).where(Usage.dobject_id == record.id))
for event in events:
session.delete(event)
session.commit()
settings.instance._update_cloud_sqlite_file()
logger.success(
f"Deleted {colors.yellow(f'row {record}')} in"
f" {colors.blue(f'table {type(record).__name__}')}."
)
# delete run_ins related to the dobject that's to be deleted
run_ins = session.exec(sqm.select(RunIn).where(RunIn.dobject_id == record.id))
for run_in in run_ins:
session.delete(run_in)
session.commit()
session.delete(record)
session.commit()
settings.instance._update_cloud_sqlite_file()
logger.success(
f"Deleted {colors.yellow(f'row {record}')} in"
f" {colors.blue(f'table {type(record).__name__}')}."
)
if settings.instance._session is None:
session.close()
if isinstance(record, DObject):
# TODO: do not track deletes until we come up
# with a good design that respects integrity
Expand Down
25 changes: 13 additions & 12 deletions lamindb/_load.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import lnschema_core as core
import sqlmodel as sqm
from lamin_logger import logger
from lndb_setup import settings

Expand All @@ -9,18 +8,20 @@


def populate_runin(dobject: core.DObject, run: core.Run):
with sqm.Session(settings.instance.db_engine()) as session:
result = session.get(core.RunIn, (run.id, dobject.id))
if result is None:
session.add(
core.RunIn(
run_id=run.id,
dobject_id=dobject.id,
)
session = settings.instance.session()
result = session.get(core.RunIn, (run.id, dobject.id))
if result is None:
session.add(
core.RunIn(
run_id=run.id,
dobject_id=dobject.id,
)
session.commit()
logger.info(f"Added dobject ({dobject.id}) as input for run ({run.id}).")
settings.instance._update_cloud_sqlite_file()
)
session.commit()
logger.info(f"Added dobject ({dobject.id}) as input for run ({run.id}).")
settings.instance._update_cloud_sqlite_file()
if settings.instance._session is None:
session.close()


def load(dobject: core.DObject, stream: bool = False):
Expand Down
14 changes: 8 additions & 6 deletions lamindb/dev/db/_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ def add( # type: ignore # no support of different naming of args across overlo
for record in records:
if isinstance(record, DObject) and hasattr(record, "_local_filepath"):
upload_data_object(record, **kwargs)
with sqm.Session(settings.instance.db_engine(), expire_on_commit=False) as session:
for record in records:
session.add(record)
session.commit()
for record in records:
session.refresh(record)
session = settings.instance.session()
for record in records:
session.add(record)
session.commit()
for record in records:
session.refresh(record)
if settings.instance._session is None:
session.close()
settings.instance._update_cloud_sqlite_file()
if len(records) > 1:
return records
Expand Down
2 changes: 1 addition & 1 deletion lamindb/dev/db/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def session() -> sqm.Session:

Returns a `sqlmodel.Session` object.
"""
return sqm.Session(settings.instance.db_engine())
return settings.instance.session()
15 changes: 10 additions & 5 deletions lamindb/dev/db/_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ def __init__(self, *, tables, stmt, _settings_store: InstanceSettingsStore = Non
def _execute(self):
# cache the query result for the lifetime of the object
if self._result is None:
with sqm.Session(
settings._instance(self._settings_store).db_engine(),
expire_on_commit=False,
) as session:
self._result = session.exec(self._stmt).all()
if self._settings_store is not None:
session = sqm.Session(
settings._instance(self._settings_store).db_engine(),
expire_on_commit=False,
)
else:
session = settings.instance.session()
self._result = session.exec(self._stmt).all()
if settings.instance._session is None:
session.close()

def all(self):
"""Return all result as a list."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
dynamic = ["version", "description"]
dependencies = [
# Lamin pinned packages
"lndb_setup==0.17.0",
"lndb_setup==0.18.0",
"lnschema_core==0.20.0",
"lnschema_wetlab==0.10.0",
"lnschema_bionty==0.6.0",
Expand Down