Repository navigation
feat: support descending index columns with (col [desc]) - #1018
Conversation
Mark an index column or expression descending with -col. The parser rejects it in a pk index, the SQL exporters write DESC, the DBML exporter writes the - back, and the PostgreSQL, MySQL, SQL Server and Oracle importers read DESC.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Hi @Roshan1299, thank you for your PR. I've just discussed with the team and we've came up with a better syntax, which introduces some changes. We're considering supporting a per-field setting: I think this is more extensible in case we support more settings. And fortunately, our parser already parses this as an array indexing. So unfortunately, you may need to rework the PR a bit 🐱, or if you will, I can take it from here 😄 |
|
Sounds good, I'll rework it, most of the PR (model, exporters, importers) stays the same. One thing I noticed trying it on the parser: it only parses as array indexing without the space. Also, should |
I think we can modify the parser to allow space when inside
You can also test the parser behavior more easily with: https://dbml.dbdiagram.io/playground/. It has an AST viewer mode. |
As agreed in holistics#1018: a sort order is a per-column setting, `(a [desc], b)`, and `[asc]` is accepted too. The parser now reads a `[` after a space as indexing when it sits directly inside `(` or `[`, where no settings list can follow an expression.
|
Reworked to the new syntax: |
huydo862003
left a comment
There was a problem hiding this comment.
Thank you for your contribution @Roshan1299, it would be nice if you can support DESC inside dbml-connector too 🐱
Summary
Index columns can now have a sort order, written as a per-column setting inside the parentheses, the syntax agreed in this PR:
The PostgreSQL exporter turns that into:
The MySQL, SQL Server and Oracle exporters add
DESCthe same way, the DBML exporter writes(col [desc])back, and the PostgreSQL, MySQL, SQL Server and Oracle importers readDESCinto the new field.[asc]is accepted and means the default, so it isn't stored and isn't written back. Columns without a sort order are unchanged, so existing output stays the same.A column with a sort order goes inside parentheses even on its own:
created_at [desc]outside them is the index's own settings list, as before, and gives "Unknown index setting 'desc'".A primary key index with a descending column, like
(id, status [desc]) [pk], is an error ("A primary key index field cannot be descending"), since PostgreSQL and Oracle don't take a sort order in aPRIMARY KEYconstraint. For the same reason, an importedPRIMARY KEY (a DESC)keeps its columns but drops the sort order.Issue
Closes #172
Lasting Changes (Technical)
[after a space is now array indexing when it sits directly inside(or[(the innermost context), as discussed in feat: support descending index columns with (col [desc]) #1018, so(a [desc], b)and(a[desc], b)both parse. Outside them, a space before[still starts a settings list (name varchar [not null],a [unique]).@dbml/coremodel have an optionalsortOrder: 'desc'. It's left out otherwise, so the JSON for existing schemas doesn't change.destructureIndexNodereturns{ name, descending }entries. New helpergetIndexColumnSortOrderinutils/validate.tsreads[asc]or[desc](any case, one setting, no value). Anything else in that position, like(a [unique]), gives "An index column setting must be [asc] or [desc]".(missing [desc])still reports the missing column. That change stays inindexes/bind.ts; the sharedscanNonListNodeForBindingis untouched.ctx.getText()on the whole element, which also took in any options after it (lower(status)DESC). It now takes only the expression. That changes the imported text only where an index expression was followed by an option.parser/mysql-parse/output/expression_indexes.out.json:idx_year_descnow has"sortOrder": "desc".index_sort_orderfixtures for the 4 SQL exporters and 4 SQL importers, interpreter tests (sort orders with and without the space,[asc], the primary key error and an ascending primary key, bad settings,a [desc]outside parentheses,-a, a missing column) and a parser test for the space inside(and[.docs.md, andllms.txtmentions it.Checklist
Please check directly on the box once each of these are done
dbml-homepage/static/llms.txt(if docs/features changed)Tests:
dbml-parse2132 anddbml-core430 pass. With the source files atmaster, 5 of the 7 new interpreter tests and the parser test fail; the other two (a [desc]outside parentheses,-a) pass on both, since they guard what must not change. All 9 new or updated core fixtures fail with the core source atmaster. ESLint andtsc --noEmitare clean on the changed files apart from an unused import already onmaster.The random-input property tests (
__tests__/properties,__tests__/fuzz) failed 3 times in about 12 fulldbml-parseruns on this branch, each time a different test (should not produce valid elements for garbage input,should return valid result structure on malformed input,should roundtrip with valid dbml injected with random strings), and none in 7 full runs with the source atmaster. None of them failed again in over 55 runs of those files alone, and the garbage-input one generates no[at all; parse-then-print round-trips(a [desc], b),(a [desc] , b [ asc ])and broken input like(a [ (b [cexactly. I couldn't pin a cause, so I'm noting it in case you see it in CI.The MySQL importer fixture writes its expression as
((lower(status)) DESC)without backticks, because a backtick inside an index expression already breaks the DBML output onmaster.