Skip to content

[Feature]: [jdbc] Schema creation/validation is scattered across five code paths; missing columns on existing tables only surface on first write #3297

Closed

Description

@xzxiaoshan

Conclusion

Consolidate all scattered DDL execution and column validation into one place: DDL stays defined once in the dialect layer; execution and validation happen together inside AbstractJdbcDialectBuilder.build(). The framework never alters existing tables — missing columns fail the build with the reference CREATE TABLE DDL — and store constructors drop all schema responsibilities.

Current state

DDL definitions are centralized in the dialect layer (createTableDdls()), but execution and validation are scattered across five places:

  • AbstractJdbcDialectBuilder.build() (autoCreateTable)
  • JdbcAgentStateStore constructor (createIfNotExist, plus a table-existence probe)
  • JdbcStore.Builder (initializeSchema)
  • JdbcRemoteSnapshotClient (same flag; JdbcSnapshotSpec forwards it)
  • AbstractJdbcDialect.ensureLockTable() (lazy lock-table creation)

Problems

  1. Duplicate execution: JdbcDistributedStore.create() creates all three tables via the builder, then each lazily built component re-runs its own CREATE TABLE IF NOT EXISTS on first access. Three flag names for one concept, with inconsistent semantics — the builder's false just skips creation, while the store's false probes and throws.
  2. No schema-mismatch detection: migrating from the deprecated mysql/postgresql extensions lands on the same table name, CREATE TABLE IF NOT EXISTS is a no-op on the legacy table, and it is silently adopted. Unversioned reads work fine; the first write fails with Unknown column 'version' — discovery is deferred to runtime.
  3. Wrong layer for validation, wrong source for expected columns: column validation needs the dialect (expected schema) and a connection (which the builder already holds at assembly time) — it is assembly-time work, not per-store constructor logic; as long as it lives in one store, the store/snapshots tables get no equivalent guard. Hand-written column lists (requiredColumns) are a second copy of the DDL and will drift.

Proposal

  • Derive expected columns by parsing createTableDdls(): recognize CREATE TABLE statements only, take the leading identifier of each definition, skip constraint entries (PRIMARY/INDEX/...). No hand-maintained lists.
  • Handle both modes in build(): with autoCreateTable=true, execute all DDL first, then validate all three tables; with false, validate without executing any DDL. Validation uses SELECT * FROM t WHERE 1=0 + ResultSetMetaData, case-insensitive comparison; report all missing columns at once and log the reference CREATE TABLE DDL. The framework never executes schema changes against existing tables.
  • Remove schema logic from store constructors; mark the boolean switches @Deprecated (delegating to the same validation during the transition) and drop them next major version.
  • Leave ensureLockTable lazy as-is — MySQL/Postgres use native advisory locks and never touch the lock table.
  • Also fix SchedulerConfig / DataPlaneConfig, where JdbcStore.builder(ds).initializeSchema(true) is called without a dialect (build() throws IllegalStateException when dialect is null).

Activity

  1. added
    enhancement
    on Sep 26, 2026
  2. self-assigned this
    on Sep 26, 2026
  3. oss-maintainer commented on Sep 26, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: enhancement (area/extensions) | Status: Evaluated — mostly confirmed, two claims need correction | Severity: medium (migration-time failure surfaced only at runtime) | Feasibility: high

    Verified claim-by-claim against main @ 6a056809a (engine-assisted read of agentscope-extensions/agentscope-extensions-jdbc):

    Claim Result Evidence
    Schema execution is spread over five entry points CONFIRMED builder autoCreateTable — dialect/AbstractJdbcDialectBuilder.java:63,93-115,148-161; store ctor createIfNotExist + existence probe — state/JdbcAgentStateStore.java:65-117; KV builder initializeSchema — store/JdbcStore.java:59-81,283-319; snapshot client — snapshot/JdbcRemoteSnapshotClient.java:45-79 (flag forwarded by snapshot/JdbcSnapshotSpec.java:30-50); lazy lock table — dialect/AbstractJdbcDialect.java:252-266,296-311
    DDL definitions are already centralized in the dialect layer CONFIRMED AbstractJdbcDialect.createTableDdls() aggregates store + session + snapshot DDL — dialect/AbstractJdbcDialect.java:116-129, via dialect/table/StoreDialect.java:43-56, SessionStateDialect.java:41-57, SnapshotDialect.java:34-54
    Duplicate execution on the JdbcDistributedStore path CONFIRMED the store creates all tables through the builder, then each lazily built component re-runs its own CREATE TABLE IF NOT EXISTS
    Inconsistent semantics of a false flag PARTIAL builder autoCreateTable(false) silently skips (AbstractJdbcDialectBuilder.java:148-161) while JdbcAgentStateStore(..., false) probes and throws (JdbcAgentStateStore.java:80-117); but JdbcStore.Builder.initializeSchema(false) also just silently skips (JdbcStore.java:59-81), so the split is 2-vs-2 rather than one unified convention
    No schema-mismatch detection for an already-existing table CONFIRMED state path validates only table presence (rs.next() on a catalog query, JdbcAgentStateStore.java:102-117); store/snapshot false paths do not probe at all (JdbcStore.java:73-81, JdbcRemoteSnapshotClient.java:62-79)
    Missing columns surface only on first write NOT EXACT CREATE TABLE IF NOT EXISTS being a no-op is right, but the legacy-gap failure can also hit the first read: the store select already projects version (dialect/table/StoreDialect.java:90-97, write path :62-87, vendor/H2Dialect.java:62-76). The "deferred discovery" argument stands either way
    Hand-written requiredColumns lists duplicate the DDL NOT CONFIRMED no requiredColumns-style model exists in the module today; the only schema shape source is the per-vendor DDL lists (e.g. vendor/H2Dialect.java:40-59,83-103,134-143). If such a list exists it is being introduced by in-flight work, not by current main
    SchedulerConfig / DataPlaneConfig call JdbcStore.builder(ds).initializeSchema(true) with no dialect CORRECTED both call sites are real (service-scheduler/.../SchedulerConfig.java:35,113-114, service-dataplane/.../DataPlaneConfig.java:39,119-123) but they import the deprecated MySQL JdbcStore, which auto-detects a dialect (agentscope-extensions-mysql/.../store/JdbcStore.java:103-116). The dialect-less IllegalStateException only exists on the new module (agentscope-extensions-jdbc/.../store/JdbcStore.java:310-316), so those two call sites are not currently broken

    Related work to align with (do not duplicate): PR #3244 (fix(jdbc): fail fast with actionable migration DDL when adopting the deprecated stores' legacy table, OPEN, +226/-2) implements the narrow version of problem 2 for the state table only — touched files are state/JdbcAgentStateStore.java, dialect/vendor/H2Dialect.java and state/JdbcAgentStateStoreH2Test.java. The table names it guards are the same ones this issue wants to cover centrally, so the consolidation should absorb that mechanism rather than sit beside it.

    Assessment. The core of the proposal — keep DDL declared once in the dialect layer, move both execution and validation into AbstractJdbcDialectBuilder.build(), and derive expected columns from the parsed createTableDdls() instead of a second hand-maintained list — is coherent and localized: 6 classes plus the three dialect/table/* contracts, no cross-module cascade into core/harness/distribution. Detect-only (never auto-ALTER) is the right boundary for a framework, and the SELECT * FROM t WHERE 1=0 + ResultSetMetaData approach is portable across the four supported dialects.

    Two things worth deciding before implementation:

    1. Validating at build time moves failures from "first SQL" to "construction/startup". That is the intent, but it is a behavioural change for existing users whose tables are fine — worth stating in the release note, and the error text should carry the missing columns plus the reference DDL in one message (the issue already says this).
    2. Deprecating the per-store booleans keeps @Deprecated delegation for one minor version, which means both the old and the new path must stay green in tests; the deprecation target (next major) should be named in the Javadoc so it is tracked.
    3. Case-insensitive column comparison needs a rule for quoted/upper-case identifiers (Postgres folds lower, MySQL/MariaDB does not, H2 DATABASE_TO_UPPER depends on mode) — recommend comparing on the JDBC metadata ResultSetMetaData.getColumnName set and documenting the fold, otherwise the guard will produce false mismatches on case-sensitive vendors.

    Suggested test surface, all H2/embedded-datasource, no external services: absent table + autoCreateTable=true (creates, then validates), absent table + false (single clear failure), legacy table missing version (validation reports the column and the reference DDL, no DDL executed), idempotent second build(), and one case-folding matrix test per vendor dialect.

    No automated PR is generated from this evaluation: the issue is a design consolidation across a module owned by an in-flight PR, and PR-shape decisions here need a maintainer call on points 1-2 above.


    Automated evaluation by github-manager-bot

  4. added
    area/extensions
    on Sep 26, 2026
  5. xzxiaoshan commented on Sep 28, 2026

    @xzxiaoshan
    CollaboratorAuthor

    complated as #3309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/extensionsenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions