Skip to content

Commit a0bc13a

Browse files
committed
Merge branch 'fix-enum-upper' of https://github.com/dev-five-git/vespertide into fix-enum-upper
2 parents 6a0d595 + 38aa6e1 commit a0bc13a

31 files changed

Lines changed: 1527 additions & 288 deletions

File tree

crates/vespertide-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-cli"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-cli/src/commands/diff.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ fn format_action(action: &MigrationAction) -> String {
184184
table.bright_cyan()
185185
)
186186
}
187+
MigrationAction::ReplaceConstraint {
188+
table, from, to, ..
189+
} => {
190+
format!(
191+
"{} {} {} {} {} {}",
192+
"Replace constraint:".bright_yellow(),
193+
format_constraint_type(from).bright_cyan().bold(),
194+
"->".bright_white(),
195+
format_constraint_type(to).bright_cyan().bold(),
196+
"on".bright_white(),
197+
table.bright_cyan()
198+
)
199+
}
187200
}
188201
}
189202

@@ -234,7 +247,9 @@ mod tests {
234247
use std::path::PathBuf;
235248
use tempfile::tempdir;
236249
use vespertide_config::VespertideConfig;
237-
use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType, TableConstraint, TableDef};
250+
use vespertide_core::{
251+
ColumnDef, ColumnType, ReferenceAction, SimpleColumnType, TableConstraint, TableDef,
252+
};
238253

239254
struct CwdGuard {
240255
original: PathBuf,
@@ -565,6 +580,28 @@ mod tests {
565580
"This is a very long comment...".bright_cyan().bold()
566581
)
567582
)]
583+
#[case(
584+
MigrationAction::ReplaceConstraint {
585+
table: "posts".into(),
586+
from: vespertide_core::TableConstraint::ForeignKey {
587+
name: Some("fk_user".into()),
588+
columns: vec!["user_id".into()],
589+
ref_table: "users".into(),
590+
ref_columns: vec!["id".into()],
591+
on_delete: None,
592+
on_update: None,
593+
},
594+
to: vespertide_core::TableConstraint::ForeignKey {
595+
name: Some("fk_user".into()),
596+
columns: vec!["user_id".into()],
597+
ref_table: "users".into(),
598+
ref_columns: vec!["id".into()],
599+
on_delete: Some(ReferenceAction::Cascade),
600+
on_update: None,
601+
},
602+
},
603+
format!("{} {} {} {} {} {}", "Replace constraint:".bright_yellow(), "fk_user FK (user_id) -> users".bright_cyan().bold(), "->".bright_white(), "fk_user FK (user_id) -> users".bright_cyan().bold(), "on".bright_white(), "posts".bright_cyan())
604+
)]
568605
#[serial]
569606
fn format_action_cases(#[case] action: MigrationAction, #[case] expected: String) {
570607
assert_eq!(format_action(&action), expected);

crates/vespertide-cli/src/commands/revision.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ fn rewrite_plan_for_recreation(
602602
| MigrationAction::ModifyColumnDefault { table, .. }
603603
| MigrationAction::ModifyColumnComment { table, .. }
604604
| MigrationAction::AddConstraint { table, .. }
605-
| MigrationAction::RemoveConstraint { table, .. } => Some(table.as_str()),
605+
| MigrationAction::RemoveConstraint { table, .. }
606+
| MigrationAction::ReplaceConstraint { table, .. } => Some(table.as_str()),
606607
_ => None,
607608
};
608609
table.is_none_or(|t| !tables_to_recreate.contains(t))

crates/vespertide-config/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-config"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-core"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-core/src/action.rs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ pub enum MigrationAction {
8686
table: TableName,
8787
constraint: TableConstraint,
8888
},
89+
ReplaceConstraint {
90+
table: TableName,
91+
from: TableConstraint,
92+
to: TableConstraint,
93+
},
8994
RenameTable {
9095
from: TableName,
9196
to: TableName,
@@ -207,6 +212,13 @@ impl MigrationAction {
207212
constraint: constraint.with_prefix(prefix),
208213
}
209214
}
215+
MigrationAction::ReplaceConstraint { table, from, to } => {
216+
MigrationAction::ReplaceConstraint {
217+
table: format!("{}{}", prefix, table),
218+
from: from.with_prefix(prefix),
219+
to: to.with_prefix(prefix),
220+
}
221+
}
210222
MigrationAction::RenameTable { from, to } => MigrationAction::RenameTable {
211223
from: format!("{}{}", prefix, from),
212224
to: format!("{}{}", prefix, to),
@@ -339,6 +351,33 @@ impl fmt::Display for MigrationAction {
339351
};
340352
write!(f, "RemoveConstraint: {}.{}", table, constraint_name)
341353
}
354+
MigrationAction::ReplaceConstraint { table, to, .. } => {
355+
let constraint_name = match to {
356+
TableConstraint::PrimaryKey { .. } => "PRIMARY KEY",
357+
TableConstraint::Unique { name, .. } => {
358+
if let Some(n) = name {
359+
return write!(f, "ReplaceConstraint: {}.{} (UNIQUE)", table, n);
360+
}
361+
"UNIQUE"
362+
}
363+
TableConstraint::ForeignKey { name, .. } => {
364+
if let Some(n) = name {
365+
return write!(f, "ReplaceConstraint: {}.{} (FOREIGN KEY)", table, n);
366+
}
367+
"FOREIGN KEY"
368+
}
369+
TableConstraint::Check { name, .. } => {
370+
return write!(f, "ReplaceConstraint: {}.{} (CHECK)", table, name);
371+
}
372+
TableConstraint::Index { name, .. } => {
373+
if let Some(n) = name {
374+
return write!(f, "ReplaceConstraint: {}.{} (INDEX)", table, n);
375+
}
376+
"INDEX"
377+
}
378+
};
379+
write!(f, "ReplaceConstraint: {}.{}", table, constraint_name)
380+
}
342381
MigrationAction::RenameTable { from, to } => {
343382
write!(f, "RenameTable: {} -> {}", from, to)
344383
}
@@ -1056,4 +1095,176 @@ mod tests {
10561095
panic!("Expected RemoveConstraint");
10571096
}
10581097
}
1098+
1099+
#[rstest]
1100+
#[case::replace_constraint_primary_key(
1101+
MigrationAction::ReplaceConstraint {
1102+
table: "users".into(),
1103+
from: TableConstraint::PrimaryKey {
1104+
auto_increment: false,
1105+
columns: vec!["id".into()],
1106+
},
1107+
to: TableConstraint::PrimaryKey {
1108+
auto_increment: true,
1109+
columns: vec!["id".into()],
1110+
},
1111+
},
1112+
"ReplaceConstraint: users.PRIMARY KEY"
1113+
)]
1114+
#[case::replace_constraint_unique_with_name(
1115+
MigrationAction::ReplaceConstraint {
1116+
table: "users".into(),
1117+
from: TableConstraint::Unique {
1118+
name: None,
1119+
columns: vec!["email".into()],
1120+
},
1121+
to: TableConstraint::Unique {
1122+
name: Some("uq_email".into()),
1123+
columns: vec!["email".into()],
1124+
},
1125+
},
1126+
"ReplaceConstraint: users.uq_email (UNIQUE)"
1127+
)]
1128+
#[case::replace_constraint_unique_without_name(
1129+
MigrationAction::ReplaceConstraint {
1130+
table: "users".into(),
1131+
from: TableConstraint::Unique {
1132+
name: Some("uq_email".into()),
1133+
columns: vec!["email".into()],
1134+
},
1135+
to: TableConstraint::Unique {
1136+
name: None,
1137+
columns: vec!["email".into()],
1138+
},
1139+
},
1140+
"ReplaceConstraint: users.UNIQUE"
1141+
)]
1142+
#[case::replace_constraint_foreign_key_with_name(
1143+
MigrationAction::ReplaceConstraint {
1144+
table: "posts".into(),
1145+
from: TableConstraint::ForeignKey {
1146+
name: None,
1147+
columns: vec!["user_id".into()],
1148+
ref_table: "users".into(),
1149+
ref_columns: vec!["id".into()],
1150+
on_delete: None,
1151+
on_update: None,
1152+
},
1153+
to: TableConstraint::ForeignKey {
1154+
name: Some("fk_user".into()),
1155+
columns: vec!["user_id".into()],
1156+
ref_table: "users".into(),
1157+
ref_columns: vec!["id".into()],
1158+
on_delete: None,
1159+
on_update: None,
1160+
},
1161+
},
1162+
"ReplaceConstraint: posts.fk_user (FOREIGN KEY)"
1163+
)]
1164+
#[case::replace_constraint_foreign_key_without_name(
1165+
MigrationAction::ReplaceConstraint {
1166+
table: "posts".into(),
1167+
from: TableConstraint::ForeignKey {
1168+
name: Some("fk_user".into()),
1169+
columns: vec!["user_id".into()],
1170+
ref_table: "users".into(),
1171+
ref_columns: vec!["id".into()],
1172+
on_delete: None,
1173+
on_update: None,
1174+
},
1175+
to: TableConstraint::ForeignKey {
1176+
name: None,
1177+
columns: vec!["user_id".into()],
1178+
ref_table: "users".into(),
1179+
ref_columns: vec!["id".into()],
1180+
on_delete: None,
1181+
on_update: None,
1182+
},
1183+
},
1184+
"ReplaceConstraint: posts.FOREIGN KEY"
1185+
)]
1186+
#[case::replace_constraint_check(
1187+
MigrationAction::ReplaceConstraint {
1188+
table: "users".into(),
1189+
from: TableConstraint::Check {
1190+
name: "chk_age".into(),
1191+
expr: "age > 0".into(),
1192+
},
1193+
to: TableConstraint::Check {
1194+
name: "chk_age".into(),
1195+
expr: "age >= 0".into(),
1196+
},
1197+
},
1198+
"ReplaceConstraint: users.chk_age (CHECK)"
1199+
)]
1200+
#[case::replace_constraint_index_with_name(
1201+
MigrationAction::ReplaceConstraint {
1202+
table: "users".into(),
1203+
from: TableConstraint::Index {
1204+
name: None,
1205+
columns: vec!["email".into()],
1206+
},
1207+
to: TableConstraint::Index {
1208+
name: Some("ix_users__email".into()),
1209+
columns: vec!["email".into()],
1210+
},
1211+
},
1212+
"ReplaceConstraint: users.ix_users__email (INDEX)"
1213+
)]
1214+
#[case::replace_constraint_index_without_name(
1215+
MigrationAction::ReplaceConstraint {
1216+
table: "users".into(),
1217+
from: TableConstraint::Index {
1218+
name: Some("ix_users__email".into()),
1219+
columns: vec!["email".into()],
1220+
},
1221+
to: TableConstraint::Index {
1222+
name: None,
1223+
columns: vec!["email".into()],
1224+
},
1225+
},
1226+
"ReplaceConstraint: users.INDEX"
1227+
)]
1228+
fn test_display_replace_constraint(#[case] action: MigrationAction, #[case] expected: &str) {
1229+
assert_eq!(action.to_string(), expected);
1230+
}
1231+
1232+
#[test]
1233+
fn test_action_with_prefix_replace_constraint() {
1234+
let action = MigrationAction::ReplaceConstraint {
1235+
table: "posts".into(),
1236+
from: TableConstraint::ForeignKey {
1237+
name: Some("fk_user".into()),
1238+
columns: vec!["user_id".into()],
1239+
ref_table: "users".into(),
1240+
ref_columns: vec!["id".into()],
1241+
on_delete: Some(ReferenceAction::Cascade),
1242+
on_update: None,
1243+
},
1244+
to: TableConstraint::ForeignKey {
1245+
name: Some("fk_user".into()),
1246+
columns: vec!["user_id".into()],
1247+
ref_table: "users".into(),
1248+
ref_columns: vec!["id".into()],
1249+
on_delete: Some(ReferenceAction::SetNull),
1250+
on_update: None,
1251+
},
1252+
};
1253+
let prefixed = action.with_prefix("myapp_");
1254+
if let MigrationAction::ReplaceConstraint { table, from, to } = prefixed {
1255+
assert_eq!(table.as_str(), "myapp_posts");
1256+
if let TableConstraint::ForeignKey { ref_table, .. } = from {
1257+
assert_eq!(ref_table.as_str(), "myapp_users");
1258+
} else {
1259+
panic!("Expected ForeignKey constraint in from");
1260+
}
1261+
if let TableConstraint::ForeignKey { ref_table, .. } = to {
1262+
assert_eq!(ref_table.as_str(), "myapp_users");
1263+
} else {
1264+
panic!("Expected ForeignKey constraint in to");
1265+
}
1266+
} else {
1267+
panic!("Expected ReplaceConstraint");
1268+
}
1269+
}
10591270
}

crates/vespertide-exporter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-exporter"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-loader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-loader"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-macro/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-macro"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

crates/vespertide-naming/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vespertide-naming"
3-
version = "0.1.57"
3+
version = "0.1.58"
44
edition.workspace = true
55
license.workspace = true
66
repository.workspace = true

0 commit comments

Comments
 (0)