aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/state_impl
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-09-01 18:44:48 +0200
committerWojtek Kosior <koszko@koszko.org>2022-09-28 12:54:55 +0200
commitd150d656bdab394f649a67490b146c5798361187 (patch)
tree4fef7b190cc4c5db625e06c5b0d76ca55bc3badd /src/hydrilla/proxy/state_impl
parentf2b91572b163099b29c940cf6fe5814a047fdc51 (diff)
downloadhaketilo-hydrilla-d150d656bdab394f649a67490b146c5798361187.tar.gz
haketilo-hydrilla-d150d656bdab394f649a67490b146c5798361187.zip
[proxy] make it possible to enable and disable mapping versions from web UI
Diffstat (limited to 'src/hydrilla/proxy/state_impl')
-rw-r--r--src/hydrilla/proxy/state_impl/_operations/recompute_dependencies.py28
-rw-r--r--src/hydrilla/proxy/state_impl/items.py127
2 files changed, 144 insertions, 11 deletions
diff --git a/src/hydrilla/proxy/state_impl/_operations/recompute_dependencies.py b/src/hydrilla/proxy/state_impl/_operations/recompute_dependencies.py
index 5403ec3..9419f91 100644
--- a/src/hydrilla/proxy/state_impl/_operations/recompute_dependencies.py
+++ b/src/hydrilla/proxy/state_impl/_operations/recompute_dependencies.py
@@ -49,21 +49,26 @@ AnyInfoVar = t.TypeVar(
def _get_infos_of_type(cursor: sqlite3.Cursor, info_type: t.Type[AnyInfoVar],) \
-> t.Mapping[int, AnyInfoVar]:
+ join_mapping_statuses = 'JOIN mapping_statuses AS ms USING (item_id)'
+ condition = "i.type = 'M' AND ms.enabled != 'D'"
+ if info_type is item_infos.ResourceInfo:
+ join_mapping_statuses = ''
+ condition = "i.type = 'R'"
+
cursor.execute(
- '''
+ f'''
SELECT
ive.item_version_id,
ive.definition,
ive.repo,
ive.repo_iteration
FROM
- item_versions_extra AS ive
- JOIN items AS i USING (item_id)
- LEFT JOIN mapping_statuses AS ms USING (item_id)
+ item_versions_extra AS ive
+ JOIN items AS i USING (item_id)
+ {join_mapping_statuses}
WHERE
- i.type = ? AND COALESCE(ms.enabled != 'D', TRUE);
- ''',
- (info_type.type.value[0].upper(),)
+ {condition};
+ '''
)
result: dict[int, AnyInfoVar] = {}
@@ -92,11 +97,14 @@ def _get_current_required_state(
cursor.execute(
'''
SELECT
- definition, repo, repo_iteration
+ ive.definition, ive.repo, ive.repo_iteration
FROM
- item_versions_extra
+ item_versions_extra AS ive
+ JOIN items AS i USING (item_id)
WHERE
- item_id NOT IN __unlocked_ids AND active = 'R';
+ i.type = 'M' AND
+ item_id NOT IN __unlocked_ids AND
+ ive.active = 'R';
''',
)
diff --git a/src/hydrilla/proxy/state_impl/items.py b/src/hydrilla/proxy/state_impl/items.py
index ddfef7c..919223b 100644
--- a/src/hydrilla/proxy/state_impl/items.py
+++ b/src/hydrilla/proxy/state_impl/items.py
@@ -48,7 +48,7 @@ def _set_installed_status(cursor: sqlite3.Cursor, id: str, new_status: str) \
(new_status, id)
)
-def _get_statuses(cursor: sqlite3.Cursor, id: str) -> t.Tuple[str, str]:
+def _get_statuses(cursor: sqlite3.Cursor, id: str) -> tuple[str, str]:
cursor.execute(
'''
SELECT
@@ -114,6 +114,105 @@ def _uninstall_version(ref: VersionRefVar) -> t.Optional[VersionRefVar]:
class ConcreteMappingRef(st.MappingRef):
state: base.HaketiloStateWithFields = dc.field(hash=False, compare=False)
+ def _get_status_data(self, cursor: sqlite3.Cursor) \
+ -> tuple[str, str, int]:
+ cursor.execute(
+ '''
+ SELECT
+ ms.enabled, ms.frozen, ms.active_version_id
+ FROM
+ mapping_statuses
+ WHERE
+ item_id = ?;
+ ''',
+ (self.id,)
+ )
+
+ rows = cursor.fetchall()
+
+ if rows == []:
+ raise st.MissingItemError()
+
+ (enabled_status, frozen_status, active_version_id), = rows
+
+ return (enabled_status, frozen_status, active_version_id)
+
+
+ def update_status(
+ self,
+ enabled: st.EnabledStatus,
+ frozen: t.Optional[st.FrozenStatus] = None,
+ version_id_to_activate: t.Optional[str] = None
+ ) -> None:
+ assert frozen is None or enabled == st.EnabledStatus.ENABLED
+ assert version_id_to_activate is None or \
+ frozen != st.FrozenStatus.NOT_FROZEN
+
+ with self.state.cursor(transaction=True) as cursor:
+ cursor.execute(
+ '''
+ SELECT
+ enabled, frozen, active_version_id
+ FROM
+ mapping_statuses
+ WHERE
+ item_id = ?;
+ ''',
+ (self.id,)
+ )
+
+ rows = cursor.fetchall()
+
+ if rows == []:
+ raise st.MissingItemError()
+
+ (old_enabled_status, old_frozen_status,
+ old_active_version_id), = rows
+
+ if enabled.value == old_enabled_status and frozen is None:
+ return
+
+ new_enabled_status = enabled.value
+
+ new_frozen_status = None if frozen is None else frozen.value
+
+ if version_id_to_activate is not None:
+ new_active_version_id = version_id_to_activate
+ elif enabled == st.EnabledStatus.ENABLED:
+ new_active_version_id = str(old_active_version_id)
+ else:
+ new_active_version_id = None
+
+ cursor.execute(
+ '''
+ UPDATE
+ mapping_statuses
+ SET
+ enabled = ?,
+ frozen = ?,
+ active_version_id = ?
+ WHERE
+ item_id = ?;
+ ''', (
+ new_enabled_status,
+ new_frozen_status,
+ new_active_version_id,
+ self.id
+ ))
+
+ if enabled == st.EnabledStatus.ENABLED:
+ if old_enabled_status == 'E' and \
+ new_active_version_id == str(old_active_version_id) and \
+ (new_frozen_status == 'E' or
+ old_frozen_status == 'N' or
+ new_frozen_status == old_frozen_status):
+ return
+ else:
+ if old_active_version_id is None:
+ return
+
+ self.state.recompute_dependencies([int(self.id)])
+
def get_version_display_infos(self) \
-> t.Sequence[st.MappingVersionDisplayInfo]:
with self.state.cursor() as cursor:
@@ -267,6 +366,32 @@ class ConcreteMappingVersionRef(st.MappingVersionRef):
def uninstall(self) -> t.Optional['ConcreteMappingVersionRef']:
return _uninstall_version(self)
+ def update_mapping_status(
+ self,
+ enabled: st.EnabledStatus,
+ frozen: t.Optional[st.FrozenStatus] = None
+ ) -> None:
+ with self.state.cursor(transaction=True) as cursor:
+ cursor.execute(
+ 'SELECT item_id FROM item_versions WHERE item_version_id = ?;',
+ (self.id,)
+ )
+
+ rows = cursor.fetchall()
+
+ if rows == []:
+ raise st.MissingItemError()
+
+ (mapping_id,), = rows
+
+ mapping_ref = ConcreteMappingRef(str(mapping_id), self.state)
+
+ id_to_pass: t.Optional[str] = self.id
+ if enabled.value != 'E' or frozen is None or frozen.value == 'N':
+ id_to_pass = None
+
+ mapping_ref.update_status(enabled, frozen, id_to_pass)
+
def get_all_version_display_infos(self) \
-> t.Sequence[st.MappingVersionDisplayInfo]:
with self.state.cursor() as cursor: