"""First-connection choices, stored before a credential can dispatch a sync.""" from __future__ import annotations def choices(provider): if provider == "odoo": import odoo_relational as rel defaults = {"invoices", "orders", "order_lines", "stock_moves", "stock_valuation", "stock_quant"} return ([{"key": "customer_data", "label": "Customers", "selected": True}, {"key": "product_data", "label": "Products", "selected": True}, {"key": rel.SALESPEOPLE_KEY, "label": "Odoo salespeople", "selected": True}] + [{"key": key, "label": label, "selected": bucket in defaults} for bucket, key, label, _ in rel.TABLES]) if provider == "amazon_sp": from harness import amazon_store return [{"key": "ut_amzn_" + key, "label": key.replace("_", " ").capitalize(), "selected": key in {"orders", "order_items", "inventory", "settlements"}} for key in amazon_store.DATABASE_TABLES] return [] def install_selection(rt, provider, selected, *, existing=False): """Explicit first-save intent only; preserve every pre-existing connector configuration. This runs before add_entry, so an automatic runner cannot see a credential with all-on defaults. A failed credential save leaves a safe, resumable choice document, never an unselected sync. """ offered = {row["key"] for row in choices(provider)} if not offered or not isinstance(selected, list) or any(type(key) is not str for key in selected): raise ValueError("Choose databases from this connector's setup list.") if len(set(selected)) != len(selected) or not set(selected) <= offered: raise ValueError("The database selection contains an unknown or duplicate database.") if existing: raise ValueError("This connection already exists. Change its databases in Settings.") if provider == "odoo": import odoo_relational as rel key, member = rel.CONFIG_KEY, "grids" if rel.ORDERS_KEY in selected: selected = [*selected, rel.SALESPEOPLE_KEY] else: import connectors_amazon as amazon key, member = amazon.ENABLEMENT_KEY, "databases" def install(current): if current is None: current = {} if not isinstance(current, dict): raise ValueError("The stored connector settings are unavailable.") # Only our own incomplete setup can be retried. Older selections are never repurposed. if current and current.get("initialSetup") != "selected": raise ValueError("This workspace already has database choices. Use Settings to change them.") return {**current, member: {name: name in selected for name in sorted(offered)}, "initialSetup": "selected"} rt.update(key, install, flush="sync") def seal_selection(rt, provider): from odoo_relational import CONFIG_KEY from connectors_amazon import ENABLEMENT_KEY key = CONFIG_KEY if provider == "odoo" else ENABLEMENT_KEY def seal(current): if not isinstance(current, dict) or current.get("initialSetup") != "selected": raise ValueError("The first-connection selection was not saved.") return {**current, "initialSetup": "configured"} rt.update(key, seal, flush="sync") def ensure_salespeople_selection(rt, *, namespace=None): """Selected orders require their native discovered res.users link target.""" import odoo_relational as rel orders = rel.table_key_for(rel.ORDERS_KEY, namespace) staff = rel.table_key_for(rel.SALESPEOPLE_KEY, namespace) def admit(current): config = dict(current or {}) grids = dict(config.get("grids") or {}) default = namespace is not None or config.get("initialSetup") not in {"selected", "configured"} if grids.get(orders, default) and grids.get(staff) is not True: config["grids"] = {**grids, staff: True} return config current = rt.get(rel.CONFIG_KEY) if admit(current) != (current or {}): rt.update(rel.CONFIG_KEY, admit, flush="sync") #: Every mirror table the product pool reads. A selected tenant with product data syncs all of them. PRODUCT_DATA_ENTITIES = ("product_product", "product_pricelist", "product_pricelist_item", "product_tag", "stock_quant", "stock_location", "sale_order", "sale_order_line", "res_partner") def selected_mirror_entities(rt): """None preserves old sync policy; an explicit setup limits source pulls to selected dependencies.""" import odoo_relational as rel config = rt.get(rel.CONFIG_KEY) if isinstance(config, dict) and config: ensure_salespeople_selection(rt) config = rt.get(rel.CONFIG_KEY) if not isinstance(config, dict) or config.get("initialSetup") not in {"selected", "configured"}: return None grids = config.get("grids") if not isinstance(grids, dict) or any(type(v) is not bool for v in grids.values()): raise ValueError("The first-connection database selection is unavailable.") import routes_odoo_tables as route from harness import datastore sources = route._odoo_sources() entities = set() for key, enabled in grids.items(): if not enabled: continue if key == "customer_data": entities.add("res_partner") continue if key == "product_data": # The product pool reads only the mirror, so its data pulls every table the pool reads. entities.update(PRODUCT_DATA_ENTITIES) continue if key not in sources: continue source = sources[key] if source.get("table"): entities.add(source["table"]) entities.update(source.get("tables", {}).values()) if source.get("needs_excluded"): entities.add("res_partner") return sorted(entities & set(datastore.ENTITIES)) def mirror_cursor(rt, table_key=None): """A source waits for its first sync before a reader can use its mirror.""" from harness import datastore if rt is None: return datastore.ro_con() rt.assert_datastore_matches() mirror = rt.mirror_handle() if str(table_key or "").startswith("ut_odoo_model_"): import odoo_relational as rel config = rt.get(rel.CONFIG_KEY) if not config.get("grids", {}).get(table_key, False): raise RuntimeError("Enable this database in connection settings before reading it.") cur = mirror.ro_cursor() try: if rel.dynamic_model_source(cur, table_key, tenant_key=rt.key) is None: raise RuntimeError("The selected database is completing its first sync. Try again shortly.") return cur except Exception: cur.close() raise entities = selected_mirror_entities(rt) if entities is None: import odoo_relational as rel if (table_key is not None and rel.base_table_key(table_key) in rel.stock_source_specs() and not mirror.entity_live("stock_quant")): raise RuntimeError("The selected database is completing its first sync. Try again shortly.") return mirror.ro_con() if table_key is not None: import odoo_relational as rel import routes_odoo_tables as route config = rt.get(rel.CONFIG_KEY) if not config.get("grids", {}).get(table_key, False): raise RuntimeError("Enable this database in connection settings before reading it.") source = route._odoo_sources().get(table_key) if source is None: return mirror.ro_con() entities = set(source.get("tables", {}).values()) if source.get("table"): entities.add(source["table"]) if source.get("needs_excluded"): entities.add("res_partner") if any(not mirror.entity_live(key) for key in entities): raise RuntimeError("The selected database is completing its first sync. Try again shortly.") # An empty first selection has no readers to run and need not create a mirror file. return mirror.ro_cursor() if entities else None