From ae9ebc8c8cd49c36968d05d3ab9083e9d6b3cd67 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Thu, 18 Jul 2024 08:42:34 +0800 Subject: [PATCH] feat: support alter column set or drop not null in pg --- pegjs/postgresql.pegjs | 21 +++++++++++++++++++++ src/column.js | 2 +- test/postgres.spec.js | 7 +++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 04861596..342f31d3 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -1719,6 +1719,7 @@ alter_action / ALTER_LOCK / ALTER_COLUMN_DATA_TYPE / ALTER_COLUMN_DEFAULT + / ALTER_COLUMN_NOT_NULL ALTER_ADD_COLUMN = KW_ADD __ @@ -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? __ diff --git a/src/column.js b/src/column.js index 52dd94d0..2b2b0d2a 100644 --- a/src/column.js +++ b/src/column.js @@ -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)) diff --git a/test/postgres.spec.js b/test/postgres.spec.js index e9d94ba0..23d9761e 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -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 => {