Skip to content

Commit

Permalink
feat: support alter column set or drop not null in pg
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Jul 18, 2024
1 parent 2a4ad8b commit ae9ebc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,7 @@ alter_action
/ ALTER_LOCK
/ ALTER_COLUMN_DATA_TYPE
/ ALTER_COLUMN_DEFAULT
/ ALTER_COLUMN_NOT_NULL

ALTER_ADD_COLUMN
= KW_ADD __
Expand Down Expand Up @@ -1942,6 +1943,26 @@ ALTER_COLUMN_DEFAULT
type: 'alter',
}
}

ALTER_COLUMN_NOT_NULL
= KW_ALTER __ kc:KW_COLUMN? __ c:column_ref __ ac:(KW_SET / KW_DROP) __ n:literal_not_null {
/* => {
action: 'alter';
keyword?: KW_COLUMN;
nullable: literal_not_null;
type: 'alter';
} & create_column_definition;
*/
n.action = ac.toLowerCase();
return {
action: 'alter',
column: c,
keyword: kc,
resource: 'column',
nullable: n,
type: 'alter',
}
}
create_index_definition
= kc:(KW_INDEX / KW_KEY) __
c:column? __
Expand Down
2 changes: 1 addition & 1 deletion src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function columnOption(definition) {
reference_definition: referenceDefinition,
} = definition

columnOpt.push(toUpper(nullable && nullable.value))
columnOpt.push(toUpper(nullable && nullable.action), toUpper(nullable && nullable.value))
if (defaultOpt) {
const { type, value } = defaultOpt
columnOpt.push(type.toUpperCase(), exprToSQL(value))
Expand Down
7 changes: 7 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,13 @@ describe('Postgres', () => {
`ALTER TABLE "transactions" ADD COLUMN status VARCHAR(30) DEFAULT 'old', ALTER COLUMN status SET DEFAULT 'current', ALTER COLUMN name DROP DEFAULT`,
]
},
{
title: 'alter column set not null',
sql: [
'ALTER TABLE transactions ALTER COLUMN status SET NOT NULL',
'ALTER TABLE "transactions" ALTER COLUMN status SET NOT NULL',
]
},
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit ae9ebc8

Please sign in to comment.