Skip to content

Commit abd3ce3

Browse files
committed
Parse MySQL ALTER TABLE DROP FOREIGN KEY syntax
1 parent 6ec5223 commit abd3ce3

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/ast/ddl.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,18 @@ pub enum AlterTableOperation {
151151
},
152152
/// `DROP PRIMARY KEY`
153153
///
154-
/// Note: this is a MySQL-specific operation.
154+
/// Note: this is a [MySQL]-specific operation.
155+
///
156+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
155157
DropPrimaryKey,
158+
/// `DROP FOREIGN KEY <fk_symbol>`
159+
///
160+
/// Note: this is a [MySQL]-specific operation.
161+
///
162+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
163+
DropForeignKey {
164+
name: Ident,
165+
},
156166
/// `ENABLE ALWAYS RULE rewrite_rule_name`
157167
///
158168
/// Note: this is a PostgreSQL-specific operation.
@@ -530,6 +540,7 @@ impl fmt::Display for AlterTableOperation {
530540
)
531541
}
532542
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
543+
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
533544
AlterTableOperation::DropColumn {
534545
column_name,
535546
if_exists,

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ impl Spanned for AlterTableOperation {
998998
.span()
999999
.union_opt(&with_name.as_ref().map(|n| n.span)),
10001000
AlterTableOperation::DropPrimaryKey => Span::empty(),
1001+
AlterTableOperation::DropForeignKey { name } => name.span,
10011002
AlterTableOperation::EnableAlwaysRule { name } => name.span,
10021003
AlterTableOperation::EnableAlwaysTrigger { name } => name.span,
10031004
AlterTableOperation::EnableReplicaRule { name } => name.span,

src/parser/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8002,6 +8002,11 @@ impl<'a> Parser<'a> {
80028002
&& dialect_of!(self is MySqlDialect | GenericDialect)
80038003
{
80048004
AlterTableOperation::DropPrimaryKey
8005+
} else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::KEY])
8006+
&& dialect_of!(self is MySqlDialect | GenericDialect)
8007+
{
8008+
let name = self.parse_identifier()?;
8009+
AlterTableOperation::DropForeignKey { name }
80058010
} else if self.parse_keyword(Keyword::PROJECTION)
80068011
&& dialect_of!(self is ClickHouseDialect|GenericDialect)
80078012
{

tests/sqlparser_mysql.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,16 @@ fn parse_alter_table_drop_primary_key() {
22732273
);
22742274
}
22752275

2276+
#[test]
2277+
fn parse_alter_table_drop_foreign_key() {
2278+
assert_matches!(
2279+
alter_table_op(
2280+
mysql_and_generic().verified_stmt("ALTER TABLE tab DROP FOREIGN KEY foo_ibfk_1")
2281+
),
2282+
AlterTableOperation::DropForeignKey { name } if name.value == "foo_ibfk_1"
2283+
);
2284+
}
2285+
22762286
#[test]
22772287
fn parse_alter_table_change_column() {
22782288
let expected_name = ObjectName::from(vec![Ident::new("orders")]);

0 commit comments

Comments
 (0)