From 9b8f197775a06ddabf42d7b1ceefb36e3c2794a1 Mon Sep 17 00:00:00 2001 From: Philip Z Date: Wed, 23 Sep 2026 13:03:22 +0800 Subject: [PATCH] test: prove normal SQL relations use invoking runtime metadata --- ...issue-context-owned-metadata-boundaries.md | 30 +++++++ .../sql/portable/PortableSQLDatabaseTest.java | 87 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 docs/issues/issue-context-owned-metadata-boundaries.md diff --git a/docs/issues/issue-context-owned-metadata-boundaries.md b/docs/issues/issue-context-owned-metadata-boundaries.md new file mode 100644 index 000000000..df8aefef1 --- /dev/null +++ b/docs/issues/issue-context-owned-metadata-boundaries.md @@ -0,0 +1,30 @@ +# Java context-owned metadata boundaries (#134) + +This is a call-path audit, not a claim that normal relation queries previously used +the process-global registry. The supported SQL path already carried resolved +descriptors; the remaining work was to verify it against two independent runtimes +with the same entity names. + +| Entry point | Classification | Current boundary and executable evidence | +| --- | --- | --- | +| `PortableSQLDataService` relation loading | Active supported Q path | Repositories receive the service's metadata snapshot and `attachRelation` receives a resolved relation descriptor. `PortableSQLDatabaseTest.relationHydrationUsesInvokingRuntimeWhenEntityNamesMatch` runs two SQLite-backed runtimes with `TopNParent`/`TopNChild` in both, but distinct `parent_a`/`parent_b` FK columns. It alternates a wrong and absent global registry; both contexts load only their own child. `TOPN_009_relationHydrationUsesServiceMetadataWithoutGlobalRegistry` covers the absent-global case with the established Top-N fixture. | +| `GenericSQLProperty` / `GenericSQLRelation.setPropertyValue(context, …)` | Public legacy mapper; compatibility-only inside this repository | These context-bearing mappers resolve reference placeholders from the invoking context. `LegacyReferenceHydrationContextTest` alternates two same-name models with wrong/absent global metadata. This is not the normal `PortableSQLDataService` graph assembly path. | +| `PortableSQLRepository.resolveDescriptor` | Active when `streamInternal` hydrates an entity or reference | Service repositories have a metadata snapshot. Schema-only dialect repositories may have none; they can reconcile physical schema but reject cross-entity hydration instead of borrowing global metadata. `PortableSQLDatabaseTest.schemaOnlyRepositoryMustNotHydrateRelationFromGlobalMetadata` registers usable global metadata and verifies that hydration still fails closed. | +| `EntityFactory.forContext` / `forMetadata` | Supported entity construction | The factory is bound to invoking-context or explicit metadata. `EntityFactoryContextTest` uses two runtimes with the same type name and a poisoned global registry. | +| `EntityFactory.defaultFactory()` | Public, deprecated no-context compatibility entry point | It uses an explicitly registered process-global factory and fails with a migration hint when no global factory exists. It cannot guarantee runtime isolation when multiple runtimes coexist; prefer `forContext` or `forMetadata`. | +| `Entity.addRelation(context, …)` | Supported relation attachment | Resolves and validates the relation using the invoking context. `BaseEntityTest` exercises same-name models with wrong/absent global metadata and invalid relation rejection. | +| `Entity.addRelation(name, value)` | Public, deprecated no-context compatibility entry point | Not used by the normal service loader. It uses explicitly registered process-global metadata and fails with a migration hint when none exists. It cannot establish which runtime owns the entity; prefer the context overload. | +| `BaseRequest` unbound metadata fallback | Deprecated context-free JSON search compatibility path | `JsonRequests.findWithJson(context, …)` binds invoking-context metadata before evaluating fields; `JsonRequestsContextTest` alternates same-name models with poisoned global metadata. Context-free callers relying on `EntityMetaFactory.registerGlobal` have no cross-runtime isolation guarantee. | + +The two no-context public APIs remain compatibility surfaces, not recommended +multi-runtime APIs. A process-global registration is an explicit single-runtime +opt-in, not a substitute for passing a context. Their final migration policy +(warning versus fail-closed even when a global factory is registered) is a +separate compatibility decision; do not infer that deprecation alone makes +them safe across runtimes. + +Verification scope for this audit: the focused two-runtime SQL test, full Maven +tests, Java examples, and the existing live PostgreSQL/MySQL CI gate. The +SQLite test proves descriptor selection on the normal SQL relation path; it +does not claim that every SQL dialect was retested with two simultaneous +runtimes. diff --git a/teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java b/teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java index 3d392b323..7c3686651 100644 --- a/teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java +++ b/teaql-sql-portable/src/test/java/io/teaql/core/sql/portable/PortableSQLDatabaseTest.java @@ -219,6 +219,93 @@ public void TOPN_009_relationHydrationUsesServiceMetadataWithoutGlobalRegistry() } } + @Test + public void relationHydrationUsesInvokingRuntimeWhenEntityNamesMatch() throws Exception { + RelationRuntime first = relationRuntime("parent_a", 11L); + RelationRuntime second = relationRuntime("parent_b", 21L); + assertNotSame(first.metadata().resolveEntityDescriptor("TopNChild"), + second.metadata().resolveEntityDescriptor("TopNChild")); + + EntityMetaFactory previous = EntityMetaFactory.get(); + try { + EntityMetaFactory.registerGlobal(second.metadata()); + assertEquals(List.of(11L), loadedChildIds(first.context())); + + EntityMetaFactory.registerGlobal(first.metadata()); + assertEquals(List.of(21L), loadedChildIds(second.context())); + + EntityMetaFactory.registerGlobal(null); + assertEquals(List.of(11L), loadedChildIds(first.context())); + assertEquals(List.of(21L), loadedChildIds(second.context())); + } finally { + EntityMetaFactory.registerGlobal(previous); + } + } + + private static RelationRuntime relationRuntime(String foreignKeyColumn, long childId) + throws Exception { + SimpleEntityMetaFactory metadata = new SimpleEntityMetaFactory(); + EntityDescriptor parent = relationEntity( + "TopNParent", TopNParent.class, TopNParent::new, "top_n_parent_data", + List.of(new Object[] {"id", "INTEGER", Long.class}, + new Object[] {"version", "INTEGER", Long.class}, + new Object[] {"name", "VARCHAR(100)", String.class})); + metadata.register(parent); + EntityDescriptor child = relationEntity( + "TopNChild", TopNChild.class, TopNChild::new, "top_n_child_data", + List.of(new Object[] {"id", "INTEGER", Long.class}, + new Object[] {"version", "INTEGER", Long.class}, + new Object[] {"name", "VARCHAR(100)", String.class}, + new Object[] {"state", "VARCHAR(100)", String.class})); + GenericSQLRelation relation = (GenericSQLRelation) child.addObjectProperty( + metadata, "parent", "TopNParent", "children", TopNParent.class, + GenericSQLRelation::new); + relation.setTableName("top_n_child_data"); + relation.setColumnName(foreignKeyColumn); + relation.setColumnType("INTEGER"); + metadata.register(child); + + SQLiteTeaQLDatabase database = new SQLiteTeaQLDatabase(); + PortableSQLDataService service = new PortableSQLDataService("sql", database, metadata); + TeaQLRuntime runtime = TeaQLRuntime.builder() + .metadata(metadata) + .dataService("sql", service) + .idGenerationService((ctx, entity) -> 100L) + .build(); + UserContext runtimeContext = new DefaultUserContext(runtime); + runtimeContext.putAttribute("ensureTable", true); + service.ensureSchema(runtimeContext, "TopNParent"); + service.ensureSchema(runtimeContext, "TopNChild"); + database.execute("DELETE FROM top_n_parent_data"); + database.execute("INSERT INTO top_n_parent_data (id, version, name)" + + " VALUES (1, 1, 'parent')"); + database.execute("INSERT INTO top_n_child_data (id, version, name, state, " + + foreignKeyColumn + ") VALUES (" + childId + ", 1, 'child', 'visible', 1)"); + return new RelationRuntime(metadata, runtimeContext); + } + + private static List loadedChildIds(UserContext runtimeContext) { + TopNChildRequest child = new TopNChildRequest(); + child.selectProperty("id"); + child.selectProperty("version"); + child.selectProperty("name"); + TopNParentRequest parent = new TopNParentRequest(); + parent.selectProperty("id"); + parent.selectProperty("version"); + parent.selectProperty("name"); + parent.enhanceRelation("children", child); + SmartList rows = parent + .comment("load same-named relation through the invoking runtime") + .purpose("prove SQL relation hydration is context-owned") + .executeForList(runtimeContext); + assertEquals(1, rows.size()); + SmartList children = rows.get(0).getProperty("children"); + assertNotNull(children); + return children.toList(Entity::getId); + } + + private record RelationRuntime(SimpleEntityMetaFactory metadata, UserContext context) {} + @Test public void TOPN_012_canonicalRelationIndexEnsureIsIdempotentOnSQLite() { registerTopNFixture();