Skip to content

Add numeric_precision/numeric_scale to get_columns and seq_in_fk to get_foreign_keys for DDL type fidelity (#840) #142

Closed

Description

@aesslinger

Problem

Tabularis PR #939
("fix: restore type fidelity in Generate SQL / Create Table (#840)") adds
three new optional fields to the host's TableColumn and ForeignKey
models so that generated CREATE TABLE DDL preserves type modifiers
like numeric(10,2) and composite foreign key column order.

The plugin does not currently return these fields, so:

  • numeric(10,2) columns generate as bare numeric (missing
    precision/scale)
  • Composite foreign keys may have columns in the wrong order (the host
    falls back to row order instead of seq_in_fk)

What PR #939 adds

get_columns — numeric_precision and numeric_scale

New optional TableColumn fields (Option<i32>, skip_serializing_if = Option::is_none):

{
  "name": "price",
  "data_type": "numeric",
  "character_maximum_length": null,
  "numeric_precision": 10,
  "numeric_scale": 2,
  ...
}

The host's sqlGenerator.ts uses them: if (!hasModifier && column.numeric_precision != null) { const scale = column.numeric_scale ?? 0; return \${type}(${column.numeric_precision},${scale})`; }`. Without
them, it falls back to the bare type name.

The builtin's get_columns query now selects c.numeric_precision and
c.numeric_scale from information_schema.columns. The plugin's query
already selects c.character_maximum_length but not these two fields.

get_foreign_keys — seq_in_fk

New optional ForeignKey field (Option<i32>, skip_serializing_if = Option::is_none):

{
  "name": "fk_comp",
  "column_name": "a",
  "ref_table": "ref_t",
  "ref_column": "x",
  "seq_in_fk": 1,
  ...
}

For composite FKs, the host groups entries by name and orders by
seq_in_fk (falling back to row order when absent). Without it,
composite FK columns may be in the wrong order in generated DDL.

The builtin's FK query now uses WITH ORDINALITY on
unnest(con.conkey, con.confkey) to get key_seq (the true composite-key
column order, independent of raw attnum) and orders by it. The plugin's
query uses plain unnest without WITH ORDINALITY.

FK field names — already compatible

PR #939's PLUGIN_GUIDE.md uses name, ref_table, ref_column. The
plugin already uses these names — no change needed.

Can the plugin ship this before #939 lands?

Yes. All three fields are purely additive:

  • The host's Rust models use Option<T> with #[serde(skip_serializing_if = "Option::is_none")] — absent fields deserialize fine.
  • The TypeScript sqlGenerator.ts uses null-safe checks
    (column.numeric_precision != null, column.numeric_scale ?? 0,
    a.seq_in_fk ?? Number.MAX_SAFE_INTEGER) — absent fields fall back to
    current behavior.
  • The current host (without #939) ignores unknown JSON fields — returning
    extra fields is safe.
  • When #939 lands, the host picks up the new fields automatically.

There is no dependency ordering requirement. The plugin can ship this
before or after #939.

Suggested implementation

get_columns (+ get_all_columns_batch, get_view_columns)

  1. Add c.numeric_precision and c.numeric_scale to the SQL query in
    fetch_table_columns (and the batch/view query variants).
  2. Extract them in row_to_table_column as Option<i32>.
  3. Include them as optional fields in the JSON response (only when
    present, matching character_maximum_length's conditional-insert
    pattern).

get_foreign_keys (+ get_all_foreign_keys_batch)

  1. Change unnest(con.conkey, con.confkey) AS cols(src_attnum, ref_attnum)
    to WITH ORDINALITY AS cols(src_attnum, ref_attnum, key_seq).
  2. Add cols.key_seq to the SELECT.
  3. Change ORDER BY ... cols.src_attnum to ORDER BY ... cols.key_seq.
  4. Extract key_seq as Option<i64> and narrow to Option<i32>.
  5. Include seq_in_fk in the JSON response in row_to_foreign_key.

Parity

The builtin's get_columns and get_foreign_keys implementations in
tabularis/src-tauri/src/drivers/postgres/mod.rs (as updated by #939)
are the reference. The parity suite's golden files
(get_columns_all_types.json, get_foreign_keys_orders.json, etc.) are
updated by #939 to include the new fields — the plugin's parity tests
will need to match.

Tests

  • Unit tests for row_to_table_column extracting numeric_precision/
    numeric_scale (can be tested without a live DB — the function takes
    a &tokio_postgres::Row).
  • Unit tests for row_to_foreign_key extracting seq_in_fk.
  • Live/parity tests confirming the golden files match with the new
    fields.

Scope

Three functions to update (get_columns, get_foreign_keys,
get_all_columns_batch / get_all_foreign_keys_batch / get_view_columns
share the same query/row-mapping code). All changes are additive — no
existing field changes, no breaking behavior.

Reference: Tabularis PR #939

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

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions