Skip to content

Commit

Permalink
Migrate to SQLAlchemy 1.4
Browse files Browse the repository at this point in the history
No user-facing changes.

Re spine-tools/Spine-Database-API#121
  • Loading branch information
soininen committed Jan 14, 2025
1 parent fcaa2a0 commit d5dbade
Show file tree
Hide file tree
Showing 17 changed files with 384 additions and 343 deletions.
21 changes: 12 additions & 9 deletions benchmarks/compound_model_filter_accepts_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
This script benchmarks CompoundModelBase.filter_accepts_model().
"""

import os
import sys

if sys.platform == "win32" and "HOMEPATH" not in os.environ:
import pathlib

os.environ["HOMEPATH"] = str(pathlib.Path(sys.executable).parent)

import time
Expand All @@ -19,9 +21,7 @@
from spinetoolbox.spine_db_manager import SpineDBManager


def call_filter_accepts_model(
loops: int, compound_model: CompoundModelBase, single_model: SingleModelBase
) -> float:
def call_filter_accepts_model(loops: int, compound_model: CompoundModelBase, single_model: SingleModelBase) -> float:
duration = 0.0
for _ in range(loops):
start = time.perf_counter()
Expand All @@ -36,11 +36,14 @@ def run_benchmark(output_file: Optional[str]):
db_mngr = SpineDBManager(QSettings(), parent=None)
logger = StdOutLogger()
db_map = db_mngr.get_db_map("sqlite://", logger, create=True)
entity_class, error = db_map.add_entity_class_item(name="Object")
assert error is None
db_map.add_entity_class_item(name="Subject")
relationship_class, error = db_map.add_entity_class_item(name="Object__Subject", dimension_name_list=("Object", "Subject"))
assert error is None
with db_map:
entity_class, error = db_map.add_entity_class_item(name="Object")
assert error is None
db_map.add_entity_class_item(name="Subject")
relationship_class, error = db_map.add_entity_class_item(
name="Object__Subject", dimension_name_list=("Object", "Subject")
)
assert error is None
compound_model = CompoundParameterValueModel(None, db_mngr, db_map)
compound_model.set_filter_class_ids({db_map: {entity_class["id"]}})
single_model = SingleModelBase(compound_model, db_map, relationship_class["id"], committed=False)
Expand All @@ -49,7 +52,7 @@ def run_benchmark(output_file: Optional[str]):
"CompoundModelBase.filter_accepts_model[filter by class ids]",
call_filter_accepts_model,
compound_model,
single_model
single_model,
)
if output_file:
pyperf.add_runs(output_file, benchmark)
Expand Down
15 changes: 9 additions & 6 deletions benchmarks/db_mngr_get_item.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
This script benchmarks SpineDBManager.get_item().
"""

import os
import sys

if sys.platform == "win32" and "HOMEPATH" not in os.environ:
import pathlib

os.environ["HOMEPATH"] = str(pathlib.Path(sys.executable).parent)

import time
Expand Down Expand Up @@ -37,12 +39,13 @@ def run_benchmark(output_file: Optional[str]):
db_mngr = SpineDBManager(QSettings(), parent=None)
logger = StdOutLogger()
db_map = db_mngr.get_db_map("sqlite://", logger, create=True)
inner_loops = 10
ids = []
for i in range(inner_loops):
item, error = db_map.add_entity_class_item(name=str(i))
assert error is None
ids.append(item["id"])
with db_map:
inner_loops = 10
ids = []
for i in range(inner_loops):
item, error = db_map.add_entity_class_item(name=str(i))
assert error is None
ids.append(item["id"])
runner = pyperf.Runner()
benchmark = runner.bench_time_func(
"SpineDBManager.get_value[parameter_value, DisplayRole]",
Expand Down
39 changes: 21 additions & 18 deletions benchmarks/db_mngr_get_value.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
This script benchmarks SpineDBManager.get_value().
"""

import os
import sys

if sys.platform == "win32" and "HOMEPATH" not in os.environ:
import pathlib

os.environ["HOMEPATH"] = str(pathlib.Path(sys.executable).parent)

import time
Expand Down Expand Up @@ -37,24 +39,25 @@ def run_benchmark(output_file: Optional[str]):
db_mngr = SpineDBManager(QSettings(), parent=None)
logger = StdOutLogger()
db_map = db_mngr.get_db_map("sqlite://", logger, create=True)
db_map.add_entity_class_item(name="Object")
db_map.add_parameter_definition_item(name="x", entity_class_name="Object")
db_map.add_entity_item(name="object", entity_class_name="Object")
value_items = []
for i in range(100):
alternative_name = str(i)
db_map.add_alternative_item(name=str(i))
value, value_type = to_database(i)
item, error = db_map.add_parameter_value_item(
entity_class_name="Object",
parameter_definition_name="x",
entity_byname=("object",),
alternative_name=alternative_name,
value=value,
type=value_type,
)
assert error is None
value_items.append(item)
with db_map:
db_map.add_entity_class_item(name="Object")
db_map.add_parameter_definition_item(name="x", entity_class_name="Object")
db_map.add_entity_item(name="object", entity_class_name="Object")
value_items = []
for i in range(100):
alternative_name = str(i)
db_map.add_alternative_item(name=str(i))
value, value_type = to_database(i)
item, error = db_map.add_parameter_value_item(
entity_class_name="Object",
parameter_definition_name="x",
entity_byname=("object",),
alternative_name=alternative_name,
value=value,
type=value_type,
)
assert error is None
value_items.append(item)
runner = pyperf.Runner()
benchmark = runner.bench_time_func(
"SpineDBManager.get_value[parameter_value, DisplayRole]",
Expand Down
19 changes: 8 additions & 11 deletions spinetoolbox/project_item/logging_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,16 @@ def _convert_legacy_resource_filter_ids_to_filter_settings(self):
if not url:
continue
try:
db_map = DatabaseMapping(url)
with DatabaseMapping(url) as db_map:
scenario_filter_ids = resource_filter_ids.get(SCENARIO_FILTER_TYPE)
if scenario_filter_ids is not None:
specific_filter_settings = self._filter_settings.known_filters.setdefault(
resource.label, {}
).setdefault(SCENARIO_FILTER_TYPE, {})
for row in db_map.query(db_map.scenario_sq):
specific_filter_settings[row.name]: row.id = row.id in scenario_filter_ids
except (SpineDBAPIError, SpineDBVersionError):
continue
try:
scenario_filter_ids = resource_filter_ids.get(SCENARIO_FILTER_TYPE)
if scenario_filter_ids is not None:
specific_filter_settings = self._filter_settings.known_filters.setdefault(
resource.label, {}
).setdefault(SCENARIO_FILTER_TYPE, {})
for row in db_map.query(db_map.scenario_sq):
specific_filter_settings[row.name]: row.id = row.id in scenario_filter_ids
finally:
db_map.close()
self._legacy_resource_filter_ids = None

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion spinetoolbox/spine_db_editor/graphics_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def entity_class_id(self, db_map):
def entity_class_ids(self, db_map):
return {self.entity_class_id(db_map)} | {
x["superclass_id"]
for x in db_map.get_items("superclass_subclass", subclass_id=self.entity_class_id(db_map))
for x in self.db_mngr.get_items(db_map, "superclass_subclass", subclass_id=self.entity_class_id(db_map))
}

def entity_id(self, db_map):
Expand Down
15 changes: 10 additions & 5 deletions spinetoolbox/spine_db_editor/mvcmodels/multi_db_tree_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,19 @@ def db_map_data(self, db_map):

def db_map_data_field(self, db_map, field, default=None):
"""Returns field from data for this item in given db_map or None if not found."""
return self.db_map_data(db_map).get(field, default)
try:
lock = self.db_mngr.get_lock(db_map)
except KeyError:
return default
with lock:
return self.db_map_data(db_map).get(field, default)

def _create_new_children(self, db_map, children_ids, **kwargs):
"""
Creates new items from ids associated to a db map.
Args:
db_map (DiffDatabaseMapping): create children for this db_map
db_map (DatabaseMapping): create children for this db_map
children_ids (iter): create children from these ids
Returns:
Expand Down Expand Up @@ -336,7 +341,7 @@ def append_children_by_id(self, db_map_ids, **kwargs):
Appends children by id.
Args:
db_map_ids (dict): maps DiffDatabaseMapping instances to list of ids
db_map_ids (dict): maps DatabaseMapping instances to list of ids
"""
new_children = []
for db_map, ids in db_map_ids.items():
Expand All @@ -348,7 +353,7 @@ def remove_children_by_id(self, db_map_ids):
Removes children by id.
Args:
db_map_ids (dict): maps DiffDatabaseMapping instances to list of ids
db_map_ids (dict): maps DatabaseMapping instances to list of ids
"""
for db_map, ids in db_map_ids.items():
for child in self.find_children_by_id(db_map, *ids, reverse=True):
Expand All @@ -372,7 +377,7 @@ def update_children_by_id(self, db_map_ids, **kwargs):
another db_map --> we need to merge
Args:
db_map_ids (dict): maps DiffDatabaseMapping instances to list of ids
db_map_ids (dict): maps DatabaseMapping instances to list of ids
"""
# Find rows to update and db_map ids to add
rows_to_update = set()
Expand Down
7 changes: 3 additions & 4 deletions spinetoolbox/spine_db_editor/widgets/custom_delegates.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,16 +787,15 @@ def _create_entity_group_editor(self, parent, index):
databases = database_index.data(Qt.ItemDataRole.DisplayRole).split(", ")
entity_class = self.parent().class_item
dbs_by_entity_group = {}
db_mngr = self.parent().db_mngr
for db_map in entity_class.db_maps:
if parent.db_mngr.name_registry.display_name(db_map.sa_url) not in databases:
# Allow groups that are in selected DBs under "databases" column.
continue
class_item = self.parent().db_mngr.get_item_by_field(db_map, "entity_class", "name", entity_class.name)
class_item = db_mngr.get_item_by_field(db_map, "entity_class", "name", entity_class.name)
if not class_item:
continue
ent_groups = self.parent().db_mngr.get_items_by_field(
db_map, "entity_group", "entity_class_id", class_item["id"]
)
ent_groups = db_mngr.get_items_by_field(db_map, "entity_group", "entity_class_id", class_item["id"])
for ent_group in ent_groups:
dbs_by_entity_group.setdefault(ent_group["group_name"], set()).add(db_map)
name_list = set()
Expand Down
Loading

0 comments on commit d5dbade

Please sign in to comment.