Skip to content

Commit

Permalink
Merge pull request #114 from liam-hq/add_prism_parser
Browse files Browse the repository at this point in the history
feat: add prism parser
  • Loading branch information
sasamuku authored Dec 3, 2024
2 parents 226b937 + e6996b8 commit 0502d8f
Show file tree
Hide file tree
Showing 14 changed files with 1,415 additions and 1,015 deletions.
1,997 changes: 1,004 additions & 993 deletions frontend/docs/packages-license.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion frontend/packages/cli/src/cli/runPreprocess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs, { readFileSync } from 'node:fs'
import path from 'node:path'
import { parse } from '@liam-hq/db-structure'
import { parse } from '@liam-hq/db-structure/parser'

export function runPreprocess(inputPath: string, outputDir: string) {
if (!fs.existsSync(inputPath)) {
Expand Down
18 changes: 16 additions & 2 deletions frontend/packages/cli/src/cli/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ describe('CLI Smoke Test', () => {
it('should run the CLI command without errors', async () => {
try {
const { stdout, stderr } = await execAsync('npx --no-install . help')
expect(stderr).toBe('')
// NOTE: suppress the following warning:
if (
!stderr.includes(
'ExperimentalWarning: WASI is an experimental feature and might change at any time',
)
) {
expect(stderr).toBe('')
}
expect(stdout).toMatchInlineSnapshot(`
"Usage: liam [options] [command]
Expand All @@ -40,7 +47,14 @@ describe('CLI Smoke Test', () => {
const { stdout, stderr } = await execAsync(
'npx --no-install . erd build --input fixtures/input.schema.rb',
)
expect(stderr).toBe('')
// NOTE: suppress the following warning:
if (
!stderr.includes(
'ExperimentalWarning: WASI is an experimental feature and might change at any time',
)
) {
expect(stderr).toBe('')
}
expect(stdout).toBe('')
const { stdout: lsOutput } = await execAsync('ls ./dist')
expect(lsOutput.trim().length).toBeGreaterThan(0)
Expand Down
6 changes: 5 additions & 1 deletion frontend/packages/db-structure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
"private": true,
"version": "0.0.1",
"type": "module",
"main": "dist/index.js",
"exports": {
".": "./dist/index.js",
"./parser": "./dist/parser.js"
},
"dependencies": {
"@ruby/prism": "1.2.0",
"pg-query-emscripten": "5.1.0",
"valibot": "^1.0.0-beta.5"
},
Expand Down
1 change: 0 additions & 1 deletion frontend/packages/db-structure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { parse } from './parser/index.js'
export {
type DBStructure,
type Table,
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/db-structure/src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { parse } from './parser/index.js'
5 changes: 4 additions & 1 deletion frontend/packages/db-structure/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { DBStructure } from 'src/schema/index.js'
import { processor as schemarbPrismProcessor } from './schemarb-prism/index.js'
import { processor as schemarbProcessor } from './schemarb/index.js'
import { processor as postgresqlProcessor } from './sql/index.js'

type SupportedFormat = 'schemarb' | 'postgres'
type SupportedFormat = 'schemarb' | 'postgres' | 'schemarb-prism'

// TODO: Add error handling and tests
export const parse = (
Expand All @@ -12,6 +13,8 @@ export const parse = (
switch (format) {
case 'schemarb':
return schemarbProcessor(str)
case 'schemarb-prism':
return schemarbPrismProcessor(str)
case 'postgres':
return postgresqlProcessor(str)
default:
Expand Down
170 changes: 170 additions & 0 deletions frontend/packages/db-structure/src/parser/schemarb-prism/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import type { Table } from 'src/schema'
import { aColumn, aDBStructure, aTable } from 'src/schema/factories'
import { describe, expect, it } from 'vitest'
import { processor } from '.'

describe(processor, () => {
describe('should parse create_table correctry', () => {
const userTable = (override?: Partial<Table>) =>
aDBStructure({
tables: {
users: aTable({
name: 'users',
columns: {
id: aColumn({
name: 'id',
type: 'bigserial',
notNull: true,
primary: true,
unique: true,
}),
...override?.columns,
},
}),
},
})

it('not null', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.string "name", null: false
end
`)

const expected = userTable({
columns: {
name: aColumn({
name: 'name',
type: 'string',
notNull: true,
}),
},
})

expect(result).toEqual(expected)
})

it('nullable', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.string "name", null: true
end
`)

const expected = userTable({
columns: {
name: aColumn({
name: 'name',
type: 'string',
notNull: false,
}),
},
})

expect(result).toEqual(expected)
})

it('defalt value as string', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.string "name", default: "new user", null: true
end
`)

const expected = userTable({
columns: {
name: aColumn({
name: 'name',
type: 'string',
notNull: false,
default: 'new user',
}),
},
})

expect(result).toEqual(expected)
})

it('defalt value as integer', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.integer "age", default: 30, null: true
end
`)

const expected = userTable({
columns: {
age: aColumn({
name: 'age',
type: 'integer',
notNull: false,
default: 30,
}),
},
})

expect(result).toEqual(expected)
})

it('defalt value as boolean', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.boolean "active", default: true, null: true
end
`)

const expected = userTable({
columns: {
active: aColumn({
name: 'active',
type: 'boolean',
notNull: false,
default: true,
}),
},
})

expect(result).toEqual(expected)
})

it('primary key as args', async () => {
const result = await processor(/* Ruby */ `
create_table "users", id: :bigint
`)

const expected = userTable({
columns: {
id: aColumn({
name: 'id',
type: 'bigint',
notNull: true,
primary: true,
unique: true,
}),
},
})

expect(result).toEqual(expected)
})

it('unique', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.string "name", unique: true
end
`)

const expected = userTable({
columns: {
name: aColumn({
name: 'name',
type: 'string',
unique: true,
}),
},
})

expect(result).toEqual(expected)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { processor } from './parser'
Loading

0 comments on commit 0502d8f

Please sign in to comment.