Skip to content

Commit

Permalink
Revert "Make database mapping work when fetching external changes (#333
Browse files Browse the repository at this point in the history
…)"

This reverts commit d187d29, reversing
changes made to 474d3a7.
  • Loading branch information
soininen committed Jan 31, 2024
1 parent 0f310f8 commit 58f12b3
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 1,831 deletions.
105 changes: 0 additions & 105 deletions spinedb_api/conflict_resolution.py

This file was deleted.

30 changes: 10 additions & 20 deletions spinedb_api/db_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from alembic.config import Config
from alembic.util.exc import CommandError

from .conflict_resolution import select_in_memory_item_always
from .filters.tools import pop_filter_configs, apply_filter_stack, load_filters
from .spine_db_client import get_db_url_from_server
from .mapped_items import item_factory
Expand Down Expand Up @@ -366,16 +365,13 @@ def get_item(self, item_type, fetch=True, skip_removed=True, **kwargs):
return {}
return item.public_item

def get_items(
self, item_type, fetch=True, skip_removed=True, resolve_conflicts=select_in_memory_item_always, **kwargs
):
def get_items(self, item_type, fetch=True, skip_removed=True, **kwargs):
"""Finds and returns all the items of one type.
Args:
item_type (str): One of <spine_item_types>.
fetch (bool, optional): Whether to fetch the DB before returning the items.
skip_removed (bool, optional): Whether to ignore removed items.
resolve_conflicts (Callable): function that resolves fetch conflicts
**kwargs: Fields and values for one the unique keys as specified for the item type
in :ref:`db_mapping_schema`.
Expand All @@ -386,7 +382,7 @@ def get_items(
mapped_table = self.mapped_table(item_type)
mapped_table.check_fields(kwargs, valid_types=(type(None),))
if fetch:
self.do_fetch_all(item_type, resolve_conflicts=resolve_conflicts, **kwargs)
self.do_fetch_all(item_type, **kwargs)
get_items = mapped_table.valid_values if skip_removed else mapped_table.values
return [x.public_item for x in get_items() if all(x.get(k) == v for k, v in kwargs.items())]

Expand Down Expand Up @@ -622,7 +618,7 @@ def purge_items(self, item_type):
"""
return bool(self.remove_items(item_type, Asterisk))

def fetch_more(self, item_type, offset=0, limit=None, resolve_conflicts=select_in_memory_item_always, **kwargs):
def fetch_more(self, item_type, offset=0, limit=None, **kwargs):
"""Fetches items from the DB into the in-memory mapping, incrementally.
Args:
Expand All @@ -636,12 +632,7 @@ def fetch_more(self, item_type, offset=0, limit=None, resolve_conflicts=select_i
list(:class:`PublicItem`): The items fetched.
"""
item_type = self.real_item_type(item_type)
return [
x.public_item
for x in self.do_fetch_more(
item_type, offset=offset, limit=limit, resolve_conflicts=resolve_conflicts, **kwargs
)
]
return [x.public_item for x in self.do_fetch_more(item_type, offset=offset, limit=limit, **kwargs)]

def fetch_all(self, *item_types):
"""Fetches items from the DB into the in-memory mapping.
Expand Down Expand Up @@ -706,18 +697,13 @@ def commit_session(self, comment):
date = datetime.now(timezone.utc)
ins = self._metadata.tables["commit"].insert()
with self.engine.begin() as connection:
commit_item = {"user": user, "date": date, "comment": comment}
try:
commit_id = connection.execute(ins, commit_item).inserted_primary_key[0]
commit_id = connection.execute(ins, dict(user=user, date=date, comment=comment)).inserted_primary_key[0]
except DBAPIError as e:
raise SpineDBAPIError(f"Fail to commit: {e.orig.args}") from e
commit_item["id"] = commit_id
commit_table = self.mapped_table("commit")
commit_table.add_item_from_db(commit_item)
commit_item_id = commit_table.id_map.item_id(commit_id)
for tablename, (to_add, to_update, to_remove) in dirty_items:
for item in to_add + to_update + to_remove:
item.commit(commit_item_id)
item.commit(commit_id)
# Remove before add, to help with keeping integrity constraints
self._do_remove_items(connection, tablename, *{x["id"] for x in to_remove})
self._do_update_items(connection, tablename, *to_update)
Expand All @@ -735,6 +721,10 @@ def rollback_session(self):
if self._memory:
self._memory_dirty = False

def refresh_session(self):
"""Resets the fetch status so new items from the DB can be retrieved."""
self._refresh()

def has_external_commits(self):
"""Tests whether the database has had commits from other sources than this mapping.
Expand Down
Loading

0 comments on commit 58f12b3

Please sign in to comment.