Skip to content

Commit

Permalink
Merge pull request #2233 from brainsiq/master
Browse files Browse the repository at this point in the history
fix: don't require column expression for alter first
  • Loading branch information
taozhi8833998 authored Nov 25, 2024
2 parents a4eeb11 + f2a1f2d commit 542cda2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
10 changes: 5 additions & 5 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)* {
Expand Down
2 changes: 1 addition & 1 deletion src/alter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')
}
Expand Down
4 changes: 2 additions & 2 deletions test/cmd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 7 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 542cda2

Please sign in to comment.