Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance postgres parser test #105

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,52 @@ exports[`parse > should parse postgresql to JSON correctly 1`] = `
"relationships": {},
"tables": {
"users": {
"columns": {},
Copy link
Member Author

@FunamaYukina FunamaYukina Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a mistake in resolving the conflict in PR #90, so I fixed it🙏.

"columns": {
"created_at": {
"check": null,
"comment": null,
"default": null,
"increment": false,
"name": "created_at",
"notNull": false,
"primary": false,
"type": "timestamp",
"unique": false,
},
"email": {
"check": null,
"comment": null,
"default": null,
"increment": false,
"name": "email",
"notNull": true,
"primary": false,
"type": "varchar",
"unique": true,
},
"id": {
"check": null,
"comment": null,
"default": null,
"increment": false,
"name": "id",
"notNull": true,
"primary": true,
"type": "serial",
"unique": false,
},
"username": {
"check": null,
"comment": null,
"default": null,
"increment": false,
"name": "username",
"notNull": true,
"primary": false,
"type": "varchar",
"unique": true,
},
},
"comment": null,
"indices": [],
"name": "users",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type {
ColumnDef,
Constraint,
CreateStmt,
Node,
String as PgString,
} from '@pgsql/types'
import type { Constraint, CreateStmt, Node } from '@pgsql/types'
import type { Columns, DBStructure, Table } from 'src/schema'
import type { RawStmtWrapper } from './parser'

// Transform function for AST to DBStructure
export const convertToDBStructure = (ast: RawStmtWrapper[]): DBStructure => {
const tables: Record<string, Table> = {}

interface PgString {
sval: string
str: string
}

function isStringNode(node: Node): node is { String: PgString } {
return (node as { String: { str: string } }).String !== undefined
return (
(node as { String: { sval: string; str: string } }).String !==
undefined &&
(node as { String: { sval: string; str: string } }).String.str !==
'pg_catalog'
FunamaYukina marked this conversation as resolved.
Show resolved Hide resolved
)
}

function isConstraintNode(node: Node): node is { Constraint: Constraint } {
Expand All @@ -40,21 +44,18 @@ export const convertToDBStructure = (ast: RawStmtWrapper[]): DBStructure => {

const tableName = createStmt.relation.relname
const columns: Columns = {}
createStmt.tableElts
.filter(
(elt: Node): elt is { ColumnDef: ColumnDef } => 'ColumnDef' in elt,
)
.map((elt) => {
for (const elt of createStmt.tableElts) {
if ('ColumnDef' in elt) {
const colDef = elt.ColumnDef
return {
columns[colDef.colname || ''] = {
name: colDef.colname || '',
type:
colDef.typeName?.names
?.filter(isStringNode)
.map((n) => n.String.sval)
.join(' ') || '',
default: '', // TODO
check: '', // TODO
.map((n) => n.String.str)
.join('') || '',
default: null, // TODO
check: null, // TODO
primary:
colDef.constraints
?.filter(isConstraintNode)
Expand All @@ -77,9 +78,10 @@ export const convertToDBStructure = (ast: RawStmtWrapper[]): DBStructure => {
colDef.typeName?.names
?.filter(isStringNode)
.some((n) => n.String.sval === 'serial') || false,
comment: '', // TODO
comment: null, // TODO
}
})
}
}

if (tableName) {
tables[tableName] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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: 'serial',
notNull: true,
primary: true,
}),
...override?.columns,
},
}),
},
})

it('not null', () => {
const result = processor(/* PostgreSQL */ `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
`)

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

expect(result).toEqual(expected)
})

it('nullable', () => {
const result = processor(/* PostgreSQL */ `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);
`)

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

expect(result).toEqual(expected)
})

// TODO: Implement default value
})
})