Skip to content

Commit

Permalink
Merge pull request #152 from liam-hq/add_default_parsing_for_postgres…
Browse files Browse the repository at this point in the history
…ql_parser

Add default parsing for postgresql parser
  • Loading branch information
MH4GF authored Dec 4, 2024
2 parents 552a18d + fe7cbb1 commit e41bb43
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe(processor, () => {
expect(result).toEqual(expected)
})

it('defalt value as string', async () => {
it('default value as string', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.string "name", default: "new user", null: true
Expand All @@ -88,7 +88,7 @@ describe(processor, () => {
expect(result).toEqual(expected)
})

it('defalt value as integer', async () => {
it('default value as integer', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.integer "age", default: 30, null: true
Expand All @@ -109,7 +109,7 @@ describe(processor, () => {
expect(result).toEqual(expected)
})

it('defalt value as boolean', async () => {
it('default value as boolean', async () => {
const result = await processor(/* Ruby */ `
create_table "users" do |t|
t.boolean "active", default: true, null: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,28 @@ export const convertToDBStructure = (ast: RawStmtWrapper[]): DBStructure => {
?.filter(isStringNode)
.map((n) => n.String.sval)
.join('') || '',
default: null, // TODO
default:
colDef.constraints
?.filter(isConstraintNode)
.reduce<string | number | boolean | null>((defaultValue, c) => {
const constraint = c.Constraint
if (
constraint.contype !== 'CONSTR_DEFAULT' ||
!constraint.raw_expr ||
!('A_Const' in constraint.raw_expr)
)
return defaultValue

const aConst = constraint.raw_expr.A_Const
if ('sval' in aConst && 'sval' in aConst.sval)
return aConst.sval.sval
if ('ival' in aConst && 'ival' in aConst.ival)
return aConst.ival.ival
if ('boolval' in aConst && 'boolval' in aConst.boolval)
return aConst.boolval.boolval

return defaultValue
}, null) || null,
check: null, // TODO
primary:
colDef.constraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,92 @@ describe(processor, () => {
expect(result).toEqual(expected)
})

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

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

expect(result).toEqual(expected)
})

it('default value as varchar', async () => {
const result = await processor(/* PostgreSQL */ `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) DEFAULT 'new user'
);
`)

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

expect(result).toEqual(expected)
})

it('default value as integer', async () => {
const result = await processor(/* PostgreSQL */ `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
age INTEGER DEFAULT 30
);
`)

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

expect(result).toEqual(expected)
})

it('default value as boolean', async () => {
const result = await processor(/* PostgreSQL */ `
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
active BOOLEAN DEFAULT TRUE
);
`)

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

expect(result).toEqual(expected)
})

it('should parse foreign keys to relationships', async () => {
const result = await processor(/* PostgreSQL */ `
CREATE TABLE users (
Expand Down Expand Up @@ -140,7 +226,5 @@ describe(processor, () => {

expect(result).toEqual(expected)
})

// TODO: Implement default value
})
})

0 comments on commit e41bb43

Please sign in to comment.