From b199dfb7e3ac0b113a072f91d726d121c508366a Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Wed, 4 Dec 2024 20:47:52 +0900 Subject: [PATCH 1/3] refactor: rename a local var name --- .../packages/db-structure/src/parser/schemarb/parser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/packages/db-structure/src/parser/schemarb/parser.ts b/frontend/packages/db-structure/src/parser/schemarb/parser.ts index 0a516825f..07269b5ba 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/parser.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/parser.ts @@ -36,7 +36,7 @@ function extractTableName(argNodes: Node[]): string { } 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 +46,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 && From fb807d7a32702a32a3224f7f9313c494ccec92e3 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Wed, 4 Dec 2024 20:48:51 +0900 Subject: [PATCH 2/3] Parse and handle of table comments in `schemarb` - Added functionality to parse `comment` attributes for tables in `schema.rb`. - Added new tests to verify the parsing and handling of table comments. - Updated snapshots and input test files to reflect the inclusion of table comments. --- .../parser/__snapshots__/index.test.ts.snap | 2 +- .../src/parser/schemarb/index.test.ts | 14 ++++++++++++ .../src/parser/schemarb/input/schema1.in.rb | 2 +- .../src/parser/schemarb/parser.ts | 22 +++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) 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..27596a649 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('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| 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 07269b5ba..6e3aea144 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/parser.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/parser.ts @@ -35,6 +35,26 @@ 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 keywordHash = argNodes.find((node) => node instanceof KeywordHashNode) @@ -218,6 +238,8 @@ class DBStructureFinder extends Visitor { name: extractTableName(argNodes), }) + table.comment = extractTableComment(argNodes) + const columns: Column[] = [] const indices: Index[] = [] From 0ce136d2f307ec2c606ca75aedc58fa23e89b7b1 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Wed, 4 Dec 2024 21:39:40 +0900 Subject: [PATCH 3/3] Fix a typo; and update a test description --- .../packages/db-structure/src/parser/schemarb/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 27596a649..71a98f77e 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/index.test.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/index.test.ts @@ -28,7 +28,7 @@ describe(processor, () => { }, }) - it('comment', async () => { + it('table comment', async () => { const result = await processor(/* Ruby */ ` create_table "users", comment: "store our users." do |t| end @@ -204,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'