Skip to content

Commit 82657f4

Browse files
committed
chore: update sqlparser to 0.61.0 from 0.57.0
1 parent f6eeda7 commit 82657f4

6 files changed

Lines changed: 83 additions & 47 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ camino = "1.1.9"
2222
chrono = "0.4.40"
2323
clap = { version = "4.5.29", features = ["derive"], optional = true }
2424
sqlformat = "0.3.5"
25-
sqlparser = { version = "0.57.0" }
25+
sqlparser = { version = "0.61.0" }
2626
thiserror = "2.0.12"
2727
winnow = "0.7.3"

src/diff.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use std::{cmp::Ordering, collections::HashSet, fmt};
22

33
use bon::bon;
44
use sqlparser::ast::{
5-
AlterTableOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
6-
AlterTypeOperation, CreateDomain, CreateIndex, CreateTable, DropDomain, Ident, ObjectName,
5+
helpers::attached_token::AttachedToken, AlterTable, AlterTableOperation, AlterType,
6+
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, CreateDomain,
7+
CreateExtension, CreateIndex, CreateTable, DropDomain, DropExtension, Ident, ObjectName,
78
ObjectType, Statement, UserDefinedTypeRepresentation,
89
};
910
use thiserror::Error;
@@ -82,12 +83,12 @@ impl Diff for Vec<Statement> {
8283
Statement::CreateType { name, .. } => {
8384
find_and_compare_create_type(sa, name, other)
8485
}
85-
Statement::CreateExtension {
86+
Statement::CreateExtension(CreateExtension {
8687
name,
8788
if_not_exists,
8889
cascade,
8990
..
90-
} => {
91+
}) => {
9192
find_and_compare_create_extension(sa, name, *if_not_exists, *cascade, other)
9293
}
9394
Statement::CreateDomain(a) => find_and_compare_create_domain(sa, a, other),
@@ -115,9 +116,11 @@ impl Diff for Vec<Statement> {
115116
_ => false,
116117
}))
117118
}
118-
Statement::CreateExtension { name: b_name, .. } => {
119+
Statement::CreateExtension(CreateExtension { name: b_name, .. }) => {
119120
Ok(self.iter().find(|sa| match sa {
120-
Statement::CreateExtension { name: a_name, .. } => a_name == b_name,
121+
Statement::CreateExtension(CreateExtension {
122+
name: a_name, ..
123+
}) => a_name == b_name,
121124
_ => false,
122125
}))
123126
}
@@ -264,19 +267,19 @@ fn find_and_compare_create_extension(
264267
sa,
265268
other,
266269
|sb| match sb {
267-
Statement::CreateExtension { name: b_name, .. } => a_name == b_name,
270+
Statement::CreateExtension(CreateExtension { name: b_name, .. }) => a_name == b_name,
268271
_ => false,
269272
},
270273
|| {
271-
Ok(Some(vec![Statement::DropExtension {
274+
Ok(Some(vec![Statement::DropExtension(DropExtension {
272275
names: vec![a_name.clone()],
273276
if_exists: if_not_exists,
274277
cascade_or_restrict: if cascade {
275278
Some(sqlparser::ast::ReferentialAction::Cascade)
276279
} else {
277280
None
278281
},
279-
}]))
282+
})]))
280283
},
281284
)
282285
}
@@ -351,7 +354,7 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
351354
} else {
352355
// drop column if it only exists in `a`
353356
Some(AlterTableOperation::DropColumn {
354-
column_name: ac.name.clone(),
357+
column_names: vec![ac.name.clone()],
355358
if_exists: a.if_not_exists,
356359
drop_behavior: None,
357360
has_column_keyword: true,
@@ -377,15 +380,16 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
377380
return None;
378381
}
379382

380-
Some(vec![Statement::AlterTable {
383+
Some(vec![Statement::AlterTable(AlterTable {
384+
table_type: None,
381385
name: a.name.clone(),
382386
if_exists: a.if_not_exists,
383387
only: false,
384388
operations,
385389
location: None,
386390
on_cluster: a.on_cluster.clone(),
387-
iceberg: false,
388-
}])
391+
end_token: AttachedToken::empty(),
392+
})])
389393
}
390394

391395
fn compare_create_index(
@@ -423,18 +427,18 @@ fn compare_create_index(
423427
fn compare_create_type(
424428
a: &Statement,
425429
a_name: &ObjectName,
426-
a_rep: &UserDefinedTypeRepresentation,
430+
a_rep: &Option<UserDefinedTypeRepresentation>,
427431
b: &Statement,
428432
b_name: &ObjectName,
429-
b_rep: &UserDefinedTypeRepresentation,
433+
b_rep: &Option<UserDefinedTypeRepresentation>,
430434
) -> Result<Option<Vec<Statement>>, DiffError> {
431435
if a_name == b_name && a_rep == b_rep {
432436
return Ok(None);
433437
}
434438

435439
let operations = match a_rep {
436-
UserDefinedTypeRepresentation::Enum { labels: a_labels } => match b_rep {
437-
UserDefinedTypeRepresentation::Enum { labels: b_labels } => {
440+
Some(UserDefinedTypeRepresentation::Enum { labels: a_labels }) => match b_rep {
441+
Some(UserDefinedTypeRepresentation::Enum { labels: b_labels }) => {
438442
match a_labels.len().cmp(&b_labels.len()) {
439443
Ordering::Equal => {
440444
let rename_labels: Vec<_> = a_labels

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,19 @@ mod tests {
429429
);
430430
}
431431

432+
#[test]
433+
fn apply_alter_table_drop_columns_snowflake() {
434+
run_test_cases(
435+
vec![TestCase {
436+
dialect: Dialect::Snowflake,
437+
sql_a: "CREATE TABLE bar (foo TEXT, bar TEXT, id INT PRIMARY KEY)",
438+
sql_b: "ALTER TABLE bar DROP COLUMN foo, bar",
439+
expect: "CREATE TABLE bar (id INT PRIMARY KEY);",
440+
}],
441+
|ast_a, ast_b| ast_a.migrate(&ast_b),
442+
);
443+
}
444+
432445
#[test]
433446
fn apply_alter_table_alter_column() {
434447
run_test_cases(

src/migration.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::fmt;
22

33
use bon::bon;
44
use sqlparser::ast::{
5-
AlterColumnOperation, AlterTableOperation, AlterType, AlterTypeAddValuePosition,
6-
AlterTypeOperation, ColumnOption, ColumnOptionDef, CreateTable, GeneratedAs, ObjectName,
7-
ObjectNamePart, ObjectType, Statement, UserDefinedTypeRepresentation,
5+
AlterColumnOperation, AlterTable, AlterTableOperation, AlterType, AlterTypeAddValuePosition,
6+
AlterTypeOperation, ColumnOption, ColumnOptionDef, CreateExtension, CreateTable, DropExtension,
7+
GeneratedAs, ObjectName, ObjectNamePart, ObjectType, Statement, UserDefinedTypeRepresentation,
88
};
99
use thiserror::Error;
1010

@@ -76,7 +76,7 @@ impl Migrate for Vec<Statement> {
7676
Statement::CreateTable(ca) => other
7777
.iter()
7878
.find(|sb| match sb {
79-
Statement::AlterTable { name, .. } => *name == ca.name,
79+
Statement::AlterTable(AlterTable { name, .. }) => *name == ca.name,
8080
Statement::Drop {
8181
object_type, names, ..
8282
} => {
@@ -114,10 +114,12 @@ impl Migrate for Vec<Statement> {
114114
_ => false,
115115
})
116116
.map_or(Some(Ok(orig)), |sb| sa.migrate(sb).transpose()),
117-
Statement::CreateExtension { name, .. } => other
117+
Statement::CreateExtension(CreateExtension { name, .. }) => other
118118
.iter()
119119
.find(|sb| match sb {
120-
Statement::DropExtension { names, .. } => names.contains(name),
120+
Statement::DropExtension(DropExtension { names, .. }) => {
121+
names.contains(name)
122+
}
121123
_ => false,
122124
})
123125
.map_or(Some(Ok(orig)), |sb| sa.migrate(sb).transpose()),
@@ -152,9 +154,9 @@ impl Migrate for Statement {
152154
fn migrate(self, other: &Self) -> Result<Option<Self>, MigrateError> {
153155
match self {
154156
Self::CreateTable(ca) => match other {
155-
Self::AlterTable {
157+
Self::AlterTable(AlterTable {
156158
name, operations, ..
157-
} => {
159+
}) => {
158160
if *name == ca.name {
159161
Ok(Some(Self::CreateTable(migrate_alter_table(
160162
ca, operations,
@@ -264,8 +266,13 @@ fn migrate_alter_table(
264266
AlterTableOperation::AddColumn { column_def, .. } => {
265267
t.columns.push(column_def.clone());
266268
}
267-
AlterTableOperation::DropColumn { column_name, .. } => {
268-
t.columns.retain(|c| c.name.value != *column_name.value);
269+
AlterTableOperation::DropColumn { column_names, .. } => {
270+
t.columns.retain(|c| {
271+
column_names
272+
.iter()
273+
.find(|name| c.name.value == name.value)
274+
.is_none()
275+
});
269276
}
270277
AlterTableOperation::AlterColumn { column_name, op } => {
271278
t.columns.iter_mut().for_each(|c| {
@@ -297,7 +304,8 @@ fn migrate_alter_table(
297304
}
298305
AlterColumnOperation::SetDataType {
299306
data_type,
300-
using: _, // not applicable since we're not running the query
307+
using: _, // not applicable since we're not running the query
308+
had_set: _, // this doesn't change the meaning
301309
} => {
302310
c.data_type = data_type.clone();
303311
}
@@ -339,9 +347,9 @@ fn migrate_alter_table(
339347

340348
fn migrate_alter_type(
341349
name: ObjectName,
342-
representation: UserDefinedTypeRepresentation,
350+
representation: Option<UserDefinedTypeRepresentation>,
343351
other: &AlterType,
344-
) -> Result<(ObjectName, UserDefinedTypeRepresentation), MigrateError> {
352+
) -> Result<(ObjectName, Option<UserDefinedTypeRepresentation>), MigrateError> {
345353
match &other.operation {
346354
AlterTypeOperation::Rename(r) => {
347355
let mut parts = name.0;
@@ -352,7 +360,7 @@ fn migrate_alter_type(
352360
Ok((name, representation))
353361
}
354362
AlterTypeOperation::AddValue(a) => match representation {
355-
UserDefinedTypeRepresentation::Enum { mut labels } => {
363+
Some(UserDefinedTypeRepresentation::Enum { mut labels }) => {
356364
match &a.position {
357365
Some(AlterTypeAddValuePosition::Before(before_name)) => {
358366
let index = labels
@@ -379,9 +387,9 @@ fn migrate_alter_type(
379387
None => labels.push(a.value.clone()),
380388
}
381389

382-
Ok((name, UserDefinedTypeRepresentation::Enum { labels }))
390+
Ok((name, Some(UserDefinedTypeRepresentation::Enum { labels })))
383391
}
384-
UserDefinedTypeRepresentation::Composite { .. } => Err(MigrateError::builder()
392+
Some(_) | None => Err(MigrateError::builder()
385393
.kind(MigrateErrorKind::AlterTypeInvalidOp(Box::new(
386394
other.operation.clone(),
387395
)))
@@ -393,15 +401,15 @@ fn migrate_alter_type(
393401
.build()),
394402
},
395403
AlterTypeOperation::RenameValue(rv) => match representation {
396-
UserDefinedTypeRepresentation::Enum { labels } => {
404+
Some(UserDefinedTypeRepresentation::Enum { labels }) => {
397405
let labels = labels
398406
.into_iter()
399407
.map(|l| if l == rv.from { rv.to.clone() } else { l })
400408
.collect::<Vec<_>>();
401409

402-
Ok((name, UserDefinedTypeRepresentation::Enum { labels }))
410+
Ok((name, Some(UserDefinedTypeRepresentation::Enum { labels })))
403411
}
404-
UserDefinedTypeRepresentation::Composite { .. } => Err(MigrateError::builder()
412+
Some(_) | None => Err(MigrateError::builder()
405413
.kind(MigrateErrorKind::AlterTypeInvalidOp(Box::new(
406414
other.operation.clone(),
407415
)))

src/name_gen.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use sqlparser::ast::{
2-
AlterTableOperation, AlterType, ColumnDef, CreateIndex, CreateTable, ObjectName, ObjectType,
3-
Statement,
2+
AlterTable, AlterTableOperation, AlterType, ColumnDef, CreateIndex, CreateTable, ObjectName,
3+
ObjectType, RenameTableNameKind, Statement,
44
};
55

66
use crate::SyntaxTree;
@@ -15,9 +15,9 @@ pub fn generate_name(
1515
.iter()
1616
.filter_map(|s| match s {
1717
Statement::CreateTable(CreateTable { name, .. }) => Some(format!("create_{name}")),
18-
Statement::AlterTable {
18+
Statement::AlterTable(AlterTable {
1919
name, operations, ..
20-
} => alter_table_name(name, operations),
20+
}) => alter_table_name(name, operations),
2121
Statement::Drop {
2222
object_type, names, ..
2323
} => {
@@ -73,9 +73,14 @@ fn alter_table_name(name: &ObjectName, operations: &[AlterTableOperation]) -> Op
7373
column_def: ColumnDef { name, .. },
7474
..
7575
} => Some(format!("add_{name}")),
76-
AlterTableOperation::DropColumn { column_name, .. } => {
77-
Some(format!("drop_{column_name}"))
78-
}
76+
AlterTableOperation::DropColumn { column_names, .. } => Some(format!(
77+
"drop_{}",
78+
column_names
79+
.iter()
80+
.map(|ident| ident.value.clone())
81+
.collect::<Vec<_>>()
82+
.join("_")
83+
)),
7984
AlterTableOperation::RenameColumn {
8085
old_column_name,
8186
new_column_name,
@@ -85,7 +90,13 @@ fn alter_table_name(name: &ObjectName, operations: &[AlterTableOperation]) -> Op
8590
}
8691
AlterTableOperation::RenameTable { table_name } => {
8792
table_verb = "rename";
88-
Some(format!("to_{table_name}"))
93+
Some(format!(
94+
"to_{table_name}",
95+
table_name = match table_name {
96+
RenameTableNameKind::As(name) => name,
97+
RenameTableNameKind::To(name) => name,
98+
}
99+
))
89100
}
90101
_ => None,
91102
})

0 commit comments

Comments
 (0)