Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
return None;
}

let a_column_names: HashSet<_> = a.columns.iter().map(|c| c.name.clone()).collect();
let b_column_names: HashSet<_> = b.columns.iter().map(|c| c.name.clone()).collect();
let a_column_names: HashSet<_> = a.columns.iter().map(|c| c.name.value.clone()).collect();
let b_column_names: HashSet<_> = b.columns.iter().map(|c| c.name.value.clone()).collect();

let ops = a
let operations: Vec<_> = a
.columns
.iter()
.filter_map(|ac| {
if b_column_names.contains(&ac.name) {
if b_column_names.contains(&ac.name.value) {
None
} else {
// drop column if it only exists in `a`
Expand All @@ -359,7 +359,7 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
}
})
.chain(b.columns.iter().filter_map(|bc| {
if a_column_names.contains(&bc.name) {
if a_column_names.contains(&bc.name.value) {
None
} else {
// add the column if it only exists in `b`
Expand All @@ -373,11 +373,15 @@ fn compare_create_table(a: &CreateTable, b: &CreateTable) -> Option<Vec<Statemen
}))
.collect();

if operations.is_empty() {
return None;
}

Some(vec![Statement::AlterTable {
name: a.name.clone(),
if_exists: a.if_not_exists,
only: false,
operations: ops,
operations,
location: None,
on_cluster: a.on_cluster.clone(),
iceberg: false,
Expand Down
36 changes: 30 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,41 @@ mod tests {
#[test]
fn diff_create_table() {
run_test_cases(
vec![TestCase {
dialect: Dialect::Generic,
sql_a: "CREATE TABLE foo(\
vec![
TestCase {
dialect: Dialect::Generic,
sql_a: "CREATE TABLE foo(\
id int PRIMARY KEY
)",
sql_b: "CREATE TABLE foo(\
sql_b: "CREATE TABLE foo(\
id int PRIMARY KEY
);\
CREATE TABLE bar (id INT PRIMARY KEY);",
expect: "CREATE TABLE bar (id INT PRIMARY KEY);",
}],
expect: "CREATE TABLE bar (id INT PRIMARY KEY);",
},
TestCase {
dialect: Dialect::Generic,
sql_a: "CREATE TABLE foo(\
id int PRIMARY KEY
)",
sql_b: "CREATE TABLE foo(\
\"id\" int PRIMARY KEY
);\
CREATE TABLE bar (id INT PRIMARY KEY);",
expect: "CREATE TABLE bar (id INT PRIMARY KEY);",
},
TestCase {
dialect: Dialect::Generic,
sql_a: "CREATE TABLE foo(\
\"id\" int PRIMARY KEY
)",
sql_b: "CREATE TABLE foo(\
id int PRIMARY KEY
);\
CREATE TABLE bar (id INT PRIMARY KEY);",
expect: "CREATE TABLE bar (id INT PRIMARY KEY);",
},
],
|ast_a, ast_b| ast_a.diff(&ast_b),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ fn migrate_alter_table(
t.columns.push(column_def.clone());
}
AlterTableOperation::DropColumn { column_name, .. } => {
t.columns.retain(|c| c.name != *column_name);
t.columns.retain(|c| c.name.value != *column_name.value);
}
AlterTableOperation::AlterColumn { column_name, op } => {
t.columns.iter_mut().for_each(|c| {
Expand Down
Loading