SignalMesh / core /component_data_domains.py
k0880k
fix(migration): assign guided tour steps to component domain
47e4ff6
Raw History Blame Contribute Delete
6.35 kB
"""Component-owned data domains for Joomla migration.
A Joomla *component* is the data application. Plugins, modules, templates and
packages may be required for that component to function, but they are runtime
branches/install carriers, not independent database domains. Migration cargo
therefore enters the extension-data phase only when a source table can be
assigned to an installed component.
This module deliberately uses conservative ownership rules:
* a table equal to the component token or beginning ``token_`` belongs to it;
* a small explicit map covers Joomla core names that do not follow that rule;
* unknown tables remain unowned and are quarantined by the migration policy;
* package/plugin/module membership is handled by the compatibility graph, not
by granting those extension types ownership of tables.
Examples: ``com_dpcalendar`` owns ``dpcalendar_*``; ``com_docman`` owns
``docman_*``; ``com_easyblog`` owns ``easyblog_*``. ``plg_system_foo`` never
becomes a table owner merely because a similarly named table exists.
"""
from __future__ import annotations
import json
import re
from typing import Any
CORE_AUTHOR = "Joomla! Project"
# Core components whose historical table names are not a straight token prefix.
# These are still *component* domains; the aliases only make that ownership
# explicit. Tables handled by a dedicated core migration phase can be present
# here too — the caller's claimed-core set wins before component-data routing.
KNOWN_TABLES: dict[str, set[str]] = {
"com_banners": {"banners", "banner_clients", "banner_tracks"},
"com_contact": {"contact_details"},
"com_users": {"user_notes", "user_profiles"},
"com_associations": {"associations"},
"com_content": {"content_rating"},
"com_fields": {"fields_categories"},
"com_finder": {"finder_filters", "finder_types"},
"com_guidedtours": {"guidedtours", "guidedtour_steps"},
"com_languages": {"languages", "overrider"},
"com_messages": {"messages", "messages_cfg"},
"com_newsfeeds": {"newsfeeds"},
"com_privacy": {"privacy_consents", "privacy_requests"},
"com_redirect": {"redirect_links"},
"com_scheduler": {"scheduler_jobs", "scheduler_metadata"},
}
def _manifest(raw: Any) -> dict:
if isinstance(raw, dict):
return raw
try:
return json.loads(raw or "{}") or {}
except (TypeError, ValueError):
return {}
def token(element: str) -> str:
"""Stable physical table prefix implied by a component element."""
value = str(element or "").strip().lower()
if value.startswith("com_"):
value = value[4:]
value = re.sub(r"[^a-z0-9_]+", "_", value).strip("_")
return value
def _columns(conn, table: str) -> set[str]:
try:
with conn.cursor() as cur:
cur.execute(f"SHOW COLUMNS FROM `{table}`")
return {str(r.get("Field") or "") for r in cur.fetchall() if r.get("Field")}
except Exception:
return set()
def components(conn, prefix: str) -> list[dict]:
"""Installed component rows in a normalized shape."""
table = prefix + "extensions"
cols = _columns(conn, table)
if not {"extension_id", "type", "element"} <= cols:
return []
wanted = [c for c in (
"extension_id", "name", "type", "element", "package_id",
"manifest_cache", "state", "enabled", "params",
) if c in cols]
select = ", ".join(f"`{c}`" for c in wanted)
where = "`type`='component'"
if "state" in cols:
where += " AND `state` >= 0"
with conn.cursor() as cur:
cur.execute(f"SELECT {select} FROM `{table}` WHERE {where}")
rows = list(cur.fetchall())
out = []
for row in rows:
element = str(row.get("element") or "")
manifest = _manifest(row.get("manifest_cache"))
out.append({
**row,
"element": element,
"token": token(element),
"author": str(manifest.get("author") or ""),
"version": str(manifest.get("version") or ""),
"core": str(manifest.get("author") or "") == CORE_AUTHOR,
})
return out
def owns(component: dict, table: str) -> bool:
"""Whether this physical table is data owned by this component."""
logical = str(table or "").lower()
element = str(component.get("element") or "").lower()
if logical in KNOWN_TABLES.get(element, set()):
return True
stem = str(component.get("token") or token(element))
return bool(stem and (logical == stem or logical.startswith(stem + "_")))
def owner(conn, prefix: str, table: str) -> dict | None:
"""The most specific installed component owning ``table``, if proven."""
matches = [c for c in components(conn, prefix) if owns(c, table)]
if not matches:
return None
# A longer token is a more specific namespace (com_foo_bar beats com_foo).
matches.sort(key=lambda c: (len(c.get("token") or ""), c.get("element") or ""), reverse=True)
return matches[0]
def domains(sconn, dconn, sp: str, dp: str, *, source_tables: set[str] | None = None,
target_tables: set[str] | None = None) -> list[dict]:
"""Component -> physical data-table topology across source and target."""
source_tables = set(source_tables or ())
target_tables = set(target_tables or ())
src_components = components(sconn, sp)
dst_elements = {c.get("element") for c in components(dconn, dp)}
out = []
for comp in src_components:
owned_src = sorted(t for t in source_tables if owns(comp, t))
owned_dst = sorted(t for t in target_tables if owns(comp, t))
if not owned_src and not owned_dst:
continue
out.append({
"component": comp.get("element") or "",
"name": comp.get("name") or comp.get("element") or "",
"token": comp.get("token") or "",
"author": comp.get("author") or "",
"source_version": comp.get("version") or "",
"target_installed": comp.get("element") in dst_elements,
"source_tables": owned_src,
"target_tables": owned_dst,
"shared_tables": sorted(set(owned_src) & set(owned_dst)),
"missing_target_tables": sorted(set(owned_src) - set(owned_dst)),
})
return sorted(out, key=lambda x: (x["component"], x["name"]))