diff --git a/frontend/packages/db-structure/src/parser/__snapshots__/index.test.ts.snap b/frontend/packages/db-structure/src/parser/__snapshots__/index.test.ts.snap index 225a1c719..3bbeee949 100644 --- a/frontend/packages/db-structure/src/parser/__snapshots__/index.test.ts.snap +++ b/frontend/packages/db-structure/src/parser/__snapshots__/index.test.ts.snap @@ -192,7 +192,7 @@ exports[`parse > should parse schema.rb to JSON correctly 1`] = ` "unique": false, }, }, - "comment": null, + "comment": "store users", "indices": { "index_users_on_company_id": { "columns": [ diff --git a/frontend/packages/db-structure/src/parser/schemarb/index.test.ts b/frontend/packages/db-structure/src/parser/schemarb/index.test.ts index 978efc87d..71a98f77e 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/index.test.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/index.test.ts @@ -23,10 +23,24 @@ describe(processor, () => { indices: { ...override?.indices, }, + comment: override?.comment ?? null, }), }, }) + it('table comment', async () => { + const result = await processor(/* Ruby */ ` + create_table "users", comment: "store our users." do |t| + end + `) + + const expected = userTable({ + comment: 'store our users.', + }) + + expect(result).toEqual(expected) + }) + it('not null', async () => { const result = await processor(/* Ruby */ ` create_table "users" do |t| @@ -190,7 +204,7 @@ describe(processor, () => { expect(result).toEqual(expected) }) - it('column commnet', async () => { + it('column comment', async () => { const result = await processor(/* Ruby */ ` create_table "users" do |t| t.string "name", comment: 'this is name' diff --git a/frontend/packages/db-structure/src/parser/schemarb/input/schema1.in.rb b/frontend/packages/db-structure/src/parser/schemarb/input/schema1.in.rb index 1994dd1eb..0e2c0e5d9 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/input/schema1.in.rb +++ b/frontend/packages/db-structure/src/parser/schemarb/input/schema1.in.rb @@ -1,4 +1,4 @@ -create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| +create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, comment: "store users", force: :cascade do |t| t.uuid "company_id", null: false t.datetime "created_at", null: false t.string "name", default: "new user", null: true diff --git a/frontend/packages/db-structure/src/parser/schemarb/parser.ts b/frontend/packages/db-structure/src/parser/schemarb/parser.ts index 0a516825f..6e3aea144 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/parser.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/parser.ts @@ -35,8 +35,28 @@ function extractTableName(argNodes: Node[]): string { return nameNode.unescaped.value } +function extractTableComment(argNodes: Node[]): string | null { + const keywordHash = argNodes.find((node) => node instanceof KeywordHashNode) + + if (keywordHash) { + const commentAssoc = keywordHash.elements.find( + (elem) => + elem instanceof AssocNode && + elem.key instanceof SymbolNode && + // @ts-expect-error: unescaped is defined as string but it is actually object + elem.key.unescaped.value === 'comment', + ) + + if (commentAssoc && commentAssoc instanceof AssocNode) { + // @ts-expect-error: unescaped is defined as string but it is actually object + return commentAssoc.value.unescaped.value + } + } + return null +} + function extractIdColumn(argNodes: Node[]): Column | null { - const idKeywordHash = argNodes.find((node) => node instanceof KeywordHashNode) + const keywordHash = argNodes.find((node) => node instanceof KeywordHashNode) const idColumn = aColumn({ name: 'id', @@ -46,8 +66,8 @@ function extractIdColumn(argNodes: Node[]): Column | null { unique: true, }) - if (idKeywordHash) { - const idAssoc = idKeywordHash.elements.find( + if (keywordHash) { + const idAssoc = keywordHash.elements.find( (elem) => elem instanceof AssocNode && elem.key instanceof SymbolNode && @@ -218,6 +238,8 @@ class DBStructureFinder extends Visitor { name: extractTableName(argNodes), }) + table.comment = extractTableComment(argNodes) + const columns: Column[] = [] const indices: Index[] = []