Skip to content

Commit

Permalink
Rename display mode tables
Browse files Browse the repository at this point in the history
entity_class_display_mode -> display_mode
display_mode__entity_class -> entity_class_display_mode

Added migration scripts and renamed all associated stuff
(queries, mapped items, import/export functions etc.)

Re #442
  • Loading branch information
soininen committed Aug 21, 2024
1 parent 6130c3b commit 998d494
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 102 deletions.
2 changes: 1 addition & 1 deletion spinedb_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
get_data_for_import,
import_alternatives,
import_data,
import_display_mode__entity_classes,
import_display_modes,
import_entities,
import_entity_alternatives,
import_entity_class_display_modes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""rename entity class display mode tables
Revision ID: 7e2e66ae0f8f
Revises: 02581198a2d8
Create Date: 2024-08-21 09:25:22.928566
"""

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "7e2e66ae0f8f"
down_revision = "02581198a2d8"
branch_labels = None
depends_on = None


def upgrade():
m = sa.MetaData(op.get_bind())
m.reflect()
with op.batch_alter_table("entity_class_display_mode") as batch_op:
batch_op.drop_constraint("pk_entity_class_display_mode", type_="primary")
batch_op.create_primary_key("pk_display_mode", ["id"])
batch_op.drop_constraint("uq_entity_class_display_mode_name", type_="unique")
batch_op.create_unique_constraint("uq_display_mode_name", ["name"])
with op.batch_alter_table("display_mode__entity_class") as batch_op:
batch_op.alter_column("entity_class_display_mode_id", new_column_name="display_mode_id")
batch_op.drop_constraint("pk_display_mode__entity_class", type_="primary")
batch_op.create_primary_key("pk_entity_class_display_mode", ["id"])
batch_op.drop_constraint("fk_display_mode__entity_class_entity_class_id_entity_class", type_="foreignkey")
batch_op.create_foreign_key(
"fk_entity_class_display_mode_entity_class_id_entity_class",
"entity_class",
["entity_class_id"],
["id"],
ondelete="CASCADE",
onupdate="CASCADE",
)
batch_op.drop_constraint(
"fk_display_mode__entity_class_entity_class_display_mode_id_entity_class_display_mode", type_="foreignkey"
)
batch_op.drop_constraint("display_status_enum", type_="check")
op.rename_table("entity_class_display_mode", "display_mode")
op.rename_table("display_mode__entity_class", "entity_class_display_mode")
with op.batch_alter_table("entity_class_display_mode") as batch_op:
batch_op.create_foreign_key(
"fk_entity_class_display_mode_display_mode_id_display_mode",
"display_mode",
["display_mode_id"],
["id"],
ondelete="CASCADE",
onupdate="CASCADE",
)
batch_op.drop_constraint(
"uq_display_mode__entity_class_entity_class_display_mode_identity_class_id", type_="unique"
)
batch_op.create_unique_constraint(
"uq_entity_class_display_mode_display_mode_identity_class_id", ["display_mode_id", "entity_class_id"]
)
batch_op.create_check_constraint("display_status_enum", "display_status IN ('visible', 'hidden', 'greyed_out')")


def downgrade():
pass
2 changes: 1 addition & 1 deletion spinedb_api/db_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class DatabaseMapping(DatabaseMappingQueryMixin, DatabaseMappingCommitMixin, Dat
"scenario": "scenario_sq",
"scenario_alternative": "scenario_alternative_sq",
"entity_class": "wide_entity_class_sq",
"display_mode": "display_mode_sq",
"entity_class_display_mode": "entity_class_display_mode_sq",
"display_mode__entity_class": "display_mode__entity_class_sq",
"superclass_subclass": "superclass_subclass_sq",
"entity": "wide_entity_sq",
"entity_group": "entity_group_sq",
Expand Down
22 changes: 11 additions & 11 deletions spinedb_api/db_mapping_query_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def __init__(self, *args, **kwargs):
self._parameter_value_metadata_sq = None
self._entity_metadata_sq = None
self._superclass_subclass_sq = None
self._display_mode_sq = None
self._entity_class_display_mode_sq = None
self._display_mode__entity_class_sq = None
# Special convenience subqueries that join two or more tables
self._wide_entity_class_sq = None
self._wide_entity_sq = None
Expand Down Expand Up @@ -349,34 +349,34 @@ def entity_group_sq(self):
return self._entity_group_sq

@property
def entity_class_display_mode_sq(self):
def display_mode_sq(self):
"""A subquery of the form:
.. code-block:: sql
SELECT * FROM entity_class_display_mode
SELECT * FROM display_mode
Returns:
:class:`~sqlalchemy.sql.expression.Alias`
"""
if self._entity_class_display_mode_sq is None:
self._entity_class_display_mode_sq = self._subquery("entity_class_display_mode")
return self._entity_class_display_mode_sq
if self._display_mode_sq is None:
self._display_mode_sq = self._subquery("display_mode")
return self._display_mode_sq

@property
def display_mode__entity_class_sq(self):
def entity_class_display_mode_sq(self):
"""A subquery of the form:
.. code-block:: sql
SELECT * FROM display_mode__entity_class
SELECT * FROM entity_class_display_mode
Returns:
:class:`~sqlalchemy.sql.expression.Alias`
"""
if self._display_mode__entity_class_sq is None:
self._display_mode__entity_class_sq = self._subquery("display_mode__entity_class")
return self._display_mode__entity_class_sq
if self._entity_class_display_mode_sq is None:
self._entity_class_display_mode_sq = self._subquery("entity_class_display_mode")
return self._entity_class_display_mode_sq

@property
def alternative_sq(self):
Expand Down
18 changes: 9 additions & 9 deletions spinedb_api/export_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def export_data(
db_map,
entity_class_ids=Asterisk,
superclass_subclass_ids=Asterisk,
display_mode_ids=Asterisk,
entity_class_display_mode_ids=Asterisk,
display_mode__entity_class_ids=Asterisk,
entity_ids=Asterisk,
entity_group_ids=Asterisk,
parameter_value_list_ids=Asterisk,
Expand All @@ -43,8 +43,8 @@ def export_data(
db_map (DatabaseMapping): The db to pull data from.
entity_class_ids (Iterable, optional): If given, only exports classes with these ids
superclass_subclass_ids (Iterable, optional): If given, only exports superclass subclasse with these ids
entity_class_display_mode_ids (Iterable, optional): If given, only exports entity class display modes with these ids
display_mode__entity_class_ids (Iterable, optional): If given, only exports entity class specific display modes with these ids
display_mode_ids (Iterable, optional): If given, only exports display modes with these ids
entity_class_display_mode_ids (Iterable, optional): If given, only exports entity class specific display modes with these ids
entity_ids (Iterable, optional): If given, only exports entities with these ids
entity_group_ids (Iterable, optional): If given, only exports groups with these ids
parameter_value_list_ids (Iterable, optional): If given, only exports lists with these ids
Expand All @@ -62,8 +62,8 @@ def export_data(
data = {
"entity_classes": export_entity_classes(db_map, entity_class_ids),
"superclass_subclasses": export_superclass_subclasses(db_map, superclass_subclass_ids),
"display_modes": export_display_modes(db_map, display_mode_ids),
"entity_class_display_modes": export_entity_class_display_modes(db_map, entity_class_display_mode_ids),
"display_mode__entity_classes": export_display_mode__entity_classes(db_map, display_mode__entity_class_ids),
"entities": export_entities(db_map, entity_ids),
"entity_alternatives": export_entity_alternatives(db_map, entity_alternative_ids),
"entity_groups": export_entity_groups(db_map, entity_group_ids),
Expand Down Expand Up @@ -140,21 +140,21 @@ def export_superclass_subclasses(db_map, ids=Asterisk):
return sorted(((x.superclass_name, x.subclass_name) for x in _get_items(db_map, "superclass_subclass", ids)))


def export_entity_class_display_modes(db_map, ids=Asterisk):
return sorted(((x.name, x.description) for x in _get_items(db_map, "entity_class_display_mode", ids)))
def export_display_modes(db_map, ids=Asterisk):
return sorted(((x.name, x.description) for x in _get_items(db_map, "display_mode", ids)))


def export_display_mode__entity_classes(db_map, ids=Asterisk):
def export_entity_class_display_modes(db_map, ids=Asterisk):
return sorted(
(
x.entity_class_display_mode_name,
x.display_mode_name,
x.entity_class_name,
x.display_order,
x.display_status,
x.display_font_color,
x.display_background_color,
)
for x in _get_items(db_map, "display_mode__entity_class", ids)
for x in _get_items(db_map, "entity_class_display_mode", ids)
)


Expand Down
10 changes: 5 additions & 5 deletions spinedb_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,21 +485,21 @@ def create_spine_metadata():
UniqueConstraint("entity_id", "alternative_id"),
)
Table(
"entity_class_display_mode",
"display_mode",
meta,
Column("id", Integer, primary_key=True),
Column("name", String(255), nullable=False, unique=True),
Column("description", Text(), server_default=null()),
)
Table(
"display_mode__entity_class",
"entity_class_display_mode",
meta,
Column("id", Integer, primary_key=True),
Column(
"entity_class_display_mode_id",
"display_mode_id",
Integer,
ForeignKey(
"entity_class_display_mode.id",
"display_mode.id",
onupdate="CASCADE",
ondelete="CASCADE",
),
Expand All @@ -520,7 +520,7 @@ def create_spine_metadata():
),
Column("display_font_color", String(6), server_default=null()),
Column("display_background_color", String(6), server_default=null()),
UniqueConstraint("entity_class_display_mode_id", "entity_class_id"),
UniqueConstraint("display_mode_id", "entity_class_id"),
)
Table(
"parameter_definition",
Expand Down
38 changes: 19 additions & 19 deletions spinedb_api/import_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def get_data_for_import(
entity_metadata=(),
parameter_value_metadata=(),
superclass_subclasses=(),
display_modes=(),
entity_class_display_modes=(),
display_mode__entity_classes=(),
# legacy
object_classes=(),
relationship_classes=(),
Expand Down Expand Up @@ -160,8 +160,8 @@ def get_data_for_import(
parameter_value_metadata (list(tuple(str,tuple,str,str,str,str))):
tuples of (class name, entity byname, parameter name, metadata name, value, alternative name)
superclass_subclasses (list(tuple(str,str))): tuples of (superclass name, subclass name)
entity_class_display_modes (list(tuple(str,str))): tuples of (name, description)
display_mode__entity_classes (list(tuple(str,str,int))): tuples of (display mode name, entity class name, display order)
display_modes (list(tuple(str,str))): tuples of (name, description)
entity_class_display_modes (list(tuple(str,str,int))): tuples of (display mode name, entity class name, display order)
Yields:
str: item type
Expand Down Expand Up @@ -258,16 +258,16 @@ def get_data_for_import(
yield from get_data_for_import(
db_map, all_errors, parameter_value_metadata=relationship_parameter_value_metadata
)
if display_modes:
yield (
"display_mode",
_get_display_modes_for_import(display_modes),
)
if entity_class_display_modes:
yield (
"entity_class_display_mode",
_get_entity_class_display_modes_for_import(entity_class_display_modes),
)
if display_mode__entity_classes:
yield (
"display_mode__entity_class",
_get_display_mode__entity_classes_for_import(display_mode__entity_classes),
)


def import_superclass_subclasses(db_map, data):
Expand Down Expand Up @@ -419,32 +419,32 @@ def import_scenarios(db_map, data):
return import_data(db_map, scenarios=data)


def import_entity_class_display_modes(db_map, data):
"""Imports entity class display modes into a Spine database using a standard format.
def import_display_modes(db_map, data):
"""Imports display modes into a Spine database using a standard format.
Args:
db_map (spinedb_api.DiffDatabaseMapping): database mapping
db_map (spinedb_api.DatabaseMapping): database mapping
data (list(str, str)): tuples of (name, description)
Returns:
int: number of items imported
list: errors
"""
return import_data(db_map, entity_class_display_modes=data)
return import_data(db_map, display_modes=data)


def import_display_mode__entity_classes(db_map, data):
"""Imports entity class display mode entity classes into a Spine database using a standard format.
def import_entity_class_display_modes(db_map, data):
"""Imports entity class display modes into a Spine database using a standard format.
Args:
db_map (spinedb_api.DiffDatabaseMapping): database mapping
db_map (spinedb_api.DatabaseMapping): database mapping
data (list(str,str,int)): tuples of (display mode name, entity class name, display order)
Returns:
int: number of items imported
list: errors
"""
return import_data(db_map, display_mode__entity_classes=data)
return import_data(db_map, entity_class_display_modes=data)


def import_scenario_alternatives(db_map, data):
Expand Down Expand Up @@ -570,14 +570,14 @@ def _get_superclass_subclasses_for_import(data):
return (dict(zip(key, x)) for x in data)


def _get_entity_class_display_modes_for_import(data):
def _get_display_modes_for_import(data):
key = ("name", "description")
return ({"name": x} if isinstance(x, str) else dict(zip(key, x)) for x in data)


def _get_display_mode__entity_classes_for_import(data):
def _get_entity_class_display_modes_for_import(data):
key = (
"entity_class_display_mode_name",
"display_mode_name",
"entity_class_name",
"display_order",
"display_status",
Expand Down
Loading

0 comments on commit 998d494

Please sign in to comment.