From f2a1f2dbc85b26f702cd7ab0e1b533fc135a3930 Mon Sep 17 00:00:00 2001 From: Chris Impey Date: Fri, 22 Nov 2024 17:19:35 +0000 Subject: [PATCH] fix: don't require column expression for alter first --- pegjs/mariadb.pegjs | 10 +++++----- src/alter.js | 2 +- test/cmd.spec.js | 4 ++-- test/mysql-mariadb.spec.js | 7 +++++++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 92435f48..335dc39c 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -1027,12 +1027,12 @@ alter_table_stmt } alter_column_suffix - = k:('after'i / 'first'i) __ i:column_ref { - return { - keyword: k, - expr: i + = k:"first"i { + return { keyword: k }; + } + / k:"after"i __ i:column_ref { + return { keyword: k, expr: i }; } - } alter_action_list = head:alter_action tail:(__ COMMA __ alter_action)* { diff --git a/src/alter.js b/src/alter.js index 79ec14a3..4dc0688c 100644 --- a/src/alter.js +++ b/src/alter.js @@ -64,7 +64,7 @@ function alterExprToSQL(expr) { toUpper(prefix), name && name.trim(), dataType.filter(hasVal).join(' '), - suffix && `${toUpper(suffix.keyword)} ${columnRefToSQL(suffix.expr)}`, + suffix && `${toUpper(suffix.keyword)}${suffix.expr ? ` ${columnRefToSQL(suffix.expr)}` : ''}`, ] return alterArray.filter(hasVal).join(' ') } diff --git a/test/cmd.spec.js b/test/cmd.spec.js index 97e5eb09..b48c0a85 100644 --- a/test/cmd.spec.js +++ b/test/cmd.spec.js @@ -175,8 +175,8 @@ describe('Command SQL', () => { it('should change column', () => { expect(getParsedSql('alter table places change city city2 varchar(255)')) .to.equal('ALTER TABLE `places` CHANGE `city` `city2` VARCHAR(255)'); - expect(getParsedSql('alter table places change city city2 varchar(255) first city')) - .to.equal('ALTER TABLE `places` CHANGE `city` `city2` VARCHAR(255) FIRST `city`'); + expect(getParsedSql('alter table places change city city2 varchar(255) first')) + .to.equal('ALTER TABLE `places` CHANGE `city` `city2` VARCHAR(255) FIRST'); }) it('should support alter column with algorithm and lock option', () => { diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index 8d7496ba..d77e1faf 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -1009,6 +1009,13 @@ describe('mysql', () => { "ALTER TABLE `product` MODIFY COLUMN `type` ENUM('one', 'two') NOT NULL AFTER `name`" ] }, + { + title: 'alter table with first column', + sql: [ + "ALTER TABLE product MODIFY COLUMN type enum('one','two') NOT NULL FIRST", + "ALTER TABLE `product` MODIFY COLUMN `type` ENUM('one', 'two') NOT NULL FIRST" + ] + }, { title: 'create table with check constraint', sql: [