Repository navigation
fix(core): parse exported JSON back with refs and table groups - #1073
Open
lucasarano wants to merge 1 commit into
Open
lucasarano wants to merge 1 commit into
lucasarano wants to merge 1 commit into
Conversation
Exporting a database to JSON and parsing that JSON again failed as soon as the model had a ref or a table group, with errors like `Can't find table "null"."posts"`.
The JSON export nests tables, refs and table groups inside each schema. When the Database constructor built those nested schemas, each Schema created its own refs and table groups inside its constructor, before the schema had been added to `database.schemas`. Looking up the endpoint tables then went through `findOrCreateSchema`, which did not see the schema being built and returned a new empty one, so the table was never found. I changed `processSchemas` to register every schema first and then create the nested enums, tables, refs, deps and table groups in the same order the top-level elements already use. That also covers a ref pointing to a table in a schema that comes later in the list.
Table groups had a second problem: `TableGroup.export()` writes each member as `{ tableName, schemaName }`, but the constructor only read `name`. It now falls back to `tableName`, so the exported shape is accepted without changing the JSON output.
I added a round trip test in model_exporter.spec.ts that parses DBML with a same-schema ref, a cross-schema ref and a table group, exports it to JSON, parses the JSON and checks the DBML export matches the original. It fails on master and passes with this change.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Exporting a database to JSON and parsing that JSON again failed as soon as the model had a ref or a table group, with errors like
Can't find table "null"."posts".The JSON export nests tables, refs and table groups inside each schema. When the Database constructor built those nested schemas, each Schema created its own refs and table groups inside its constructor, before the schema had been added to
database.schemas. Looking up the endpoint tables then went throughfindOrCreateSchema, which did not see the schema being built and returned a new empty one, so the table was never found. I changedprocessSchemasto register every schema first and then create the nested enums, tables, refs, deps and table groups in the same order the top-level elements already use. That also covers a ref pointing to a table in a schema that comes later in the list.Table groups had a second problem:
TableGroup.export()writes each member as{ tableName, schemaName }, but the constructor only readname. It now falls back totableName, so the exported shape is accepted without changing the JSON output.I added a round trip test in model_exporter.spec.ts that parses DBML with a same-schema ref, a cross-schema ref and a table group, exports it to JSON, parses the JSON and checks the DBML export matches the original. It fails on master and passes with this change.
Fixes #514
I made this fix with Claude Code. Happy to change anything.