Skip to content

Commit e83706c

Browse files
committed
Refactor inline
1 parent 35cbd22 commit e83706c

File tree

68 files changed

+2813
-3238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2813
-3238
lines changed

Cargo.lock

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

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,6 @@ fn format_action(action: &MigrationAction) -> String {
8989
column.bright_cyan().bold()
9090
)
9191
}
92-
MigrationAction::AddIndex { table, index } => {
93-
format!(
94-
"{} {} {} {}",
95-
"Add index:".bright_green(),
96-
index.name.bright_cyan().bold(),
97-
"on".bright_white(),
98-
table.bright_cyan()
99-
)
100-
}
101-
MigrationAction::RemoveIndex { table, name } => {
102-
format!(
103-
"{} {} {} {}",
104-
"Remove index:".bright_red(),
105-
name.bright_cyan().bold(),
106-
"from".bright_white(),
107-
table.bright_cyan()
108-
)
109-
}
11092
MigrationAction::RenameTable { from, to } => {
11193
format!(
11294
"{} {} {} {}",
@@ -171,6 +153,13 @@ fn format_constraint_type(constraint: &vespertide_core::TableConstraint) -> Stri
171153
vespertide_core::TableConstraint::Check { name, expr } => {
172154
format!("{} CHECK ({})", name, expr)
173155
}
156+
vespertide_core::TableConstraint::Index { name, columns } => {
157+
if let Some(n) = name {
158+
format!("{} INDEX ({})", n, columns.join(", "))
159+
} else {
160+
format!("INDEX ({})", columns.join(", "))
161+
}
162+
}
174163
}
175164
}
176165

@@ -230,7 +219,6 @@ mod tests {
230219
auto_increment: false,
231220
columns: vec!["id".into()],
232221
}],
233-
indexes: vec![],
234222
};
235223
let path = models_dir.join(format!("{name}.json"));
236224
fs::write(path, serde_json::to_string_pretty(&table).unwrap()).unwrap();
@@ -284,19 +272,24 @@ mod tests {
284272
format!("{} {}.{}", "Modify column type:".bright_yellow(), "users".bright_cyan(), "id".bright_cyan().bold())
285273
)]
286274
#[case(
287-
MigrationAction::AddIndex {
275+
MigrationAction::AddConstraint {
288276
table: "users".into(),
289-
index: vespertide_core::IndexDef {
290-
name: "idx".into(),
277+
constraint: vespertide_core::TableConstraint::Index {
278+
name: Some("idx".into()),
291279
columns: vec!["id".into()],
292-
unique: false,
293280
},
294281
},
295-
format!("{} {} {} {}", "Add index:".bright_green(), "idx".bright_cyan().bold(), "on".bright_white(), "users".bright_cyan())
282+
format!("{} {} {} {}", "Add constraint:".bright_green(), "idx INDEX (id)".bright_cyan().bold(), "on".bright_white(), "users".bright_cyan())
296283
)]
297284
#[case(
298-
MigrationAction::RemoveIndex { table: "users".into(), name: "idx".into() },
299-
format!("{} {} {} {}", "Remove index:".bright_red(), "idx".bright_cyan().bold(), "from".bright_white(), "users".bright_cyan())
285+
MigrationAction::RemoveConstraint {
286+
table: "users".into(),
287+
constraint: vespertide_core::TableConstraint::Index {
288+
name: Some("idx".into()),
289+
columns: vec!["id".into()],
290+
},
291+
},
292+
format!("{} {} {} {}", "Remove constraint:".bright_red(), "idx INDEX (id)".bright_cyan().bold(), "from".bright_white(), "users".bright_cyan())
300293
)]
301294
#[case(
302295
MigrationAction::RenameTable { from: "users".into(), to: "accounts".into() },

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ mod tests {
261261
auto_increment: false,
262262
columns: vec!["id".into()],
263263
}],
264-
indexes: vec![],
265264
}
266265
}
267266

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,38 @@ pub fn cmd_log(backend: DatabaseBackend) -> Result<()> {
5858
}
5959

6060
for (i, pq) in plan_queries.iter().enumerate() {
61+
let queries = match backend {
62+
DatabaseBackend::Postgres => &pq.postgres,
63+
DatabaseBackend::MySql => &pq.mysql,
64+
DatabaseBackend::Sqlite => &pq.sqlite,
65+
};
66+
67+
// Build non-empty SQL statements
68+
let sql_statements: Vec<String> = queries
69+
.iter()
70+
.map(|q| q.build(backend).trim().to_string())
71+
.filter(|sql| !sql.is_empty())
72+
.collect();
73+
74+
// Print action description
6175
println!(
6276
" {}. {}",
6377
(i + 1).to_string().bright_magenta().bold(),
64-
match backend {
65-
DatabaseBackend::Postgres => pq
66-
.postgres
67-
.iter()
68-
.map(|q| q.build(DatabaseBackend::Postgres))
69-
.collect::<Vec<_>>()
70-
.join(";\n")
71-
.trim()
72-
.bright_white(),
73-
DatabaseBackend::MySql => pq
74-
.mysql
75-
.iter()
76-
.map(|q| q.build(DatabaseBackend::MySql))
77-
.collect::<Vec<_>>()
78-
.join(";\n")
79-
.trim()
80-
.bright_white(),
81-
DatabaseBackend::Sqlite => pq
82-
.sqlite
83-
.iter()
84-
.map(|q| q.build(DatabaseBackend::Sqlite))
85-
.collect::<Vec<_>>()
86-
.join(";\n")
87-
.trim()
88-
.bright_white(),
89-
}
78+
pq.action.to_string().bright_cyan()
9079
);
80+
81+
// Print SQL statements with sub-numbering if multiple
82+
for (j, sql) in sql_statements.iter().enumerate() {
83+
let prefix = if sql_statements.len() > 1 {
84+
format!(" {}-{}.", i + 1, j + 1)
85+
.bright_magenta()
86+
.bold()
87+
.to_string()
88+
} else {
89+
" ".to_string()
90+
};
91+
println!("{} {}", prefix, sql.bright_white());
92+
}
9193
}
9294

9395
println!();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub fn cmd_new(name: String, format: Option<FileFormat>) -> Result<()> {
3232
name: name.clone(),
3333
columns: Vec::new(),
3434
constraints: Vec::new(),
35-
indexes: Vec::new(),
3635
};
3736

3837
match format {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ mod tests {
178178
auto_increment: false,
179179
columns: vec!["id".into()],
180180
}],
181-
indexes: vec![],
182181
};
183182
let path = models_dir.join(format!("{name}.json"));
184183
fs::write(path, serde_json::to_string_pretty(&table).unwrap()).unwrap();

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ mod tests {
163163
auto_increment: false,
164164
columns: vec!["id".into()],
165165
}],
166-
indexes: vec![],
167166
};
168167
let path = models_dir.join(format!("{name}.json"));
169168
fs::write(path, serde_json::to_string_pretty(&table).unwrap()).unwrap();
@@ -399,12 +398,11 @@ mod tests {
399398
}],
400399
constraints: vec![],
401400
},
402-
MigrationAction::AddIndex {
401+
MigrationAction::AddConstraint {
403402
table: "users".into(),
404-
index: vespertide_core::IndexDef {
405-
name: "idx_id".into(),
403+
constraint: TableConstraint::Index {
404+
name: Some("idx_id".into()),
406405
columns: vec!["id".into()],
407-
unique: false,
408406
},
409407
},
410408
],
@@ -463,7 +461,6 @@ mod tests {
463461
auto_increment: false,
464462
columns: vec!["id".into()],
465463
}],
466-
indexes: vec![],
467464
}];
468465

469466
let result = emit_sql(&plan, DatabaseBackend::Sqlite, &current_schema);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,19 @@ pub fn cmd_status() -> Result<()> {
7575
current_models.len().to_string().bright_yellow()
7676
);
7777
for model in &current_models {
78+
// Count Index constraints
79+
let index_count = model
80+
.constraints
81+
.iter()
82+
.filter(|c| matches!(c, vespertide_core::TableConstraint::Index { .. }))
83+
.count();
7884
println!(
7985
" {} {} ({} {}, {} {})",
8086
"-".bright_white(),
8187
model.name.bright_green(),
8288
model.columns.len().to_string().bright_blue(),
8389
"columns".bright_white(),
84-
model.indexes.len().to_string().bright_blue(),
90+
index_count.to_string().bright_blue(),
8591
"indexes".bright_white()
8692
);
8793
}
@@ -193,7 +199,6 @@ mod tests {
193199
auto_increment: false,
194200
columns: vec!["id".into()],
195201
}],
196-
indexes: vec![],
197202
};
198203
let path = models_dir.join(format!("{name}.json"));
199204
fs::write(path, serde_json::to_string_pretty(&table).unwrap()).unwrap();

crates/vespertide-cli/src/utils.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ mod tests {
171171
auto_increment: false,
172172
columns: vec!["id".into()],
173173
}],
174-
indexes: vec![],
175174
};
176175
fs::write("models/users.yaml", serde_yaml::to_string(&table).unwrap()).unwrap();
177176

@@ -207,7 +206,6 @@ mod tests {
207206
auto_increment: false,
208207
columns: vec!["id".into()],
209208
}],
210-
indexes: vec![],
211209
};
212210
let content = serde_json::to_string_pretty(&table).unwrap();
213211
fs::write("models/subdir/subtable.json", content).unwrap();
@@ -301,7 +299,6 @@ mod tests {
301299
foreign_key: Some(ForeignKeySyntax::String("invalid_format".into())),
302300
}],
303301
constraints: vec![],
304-
indexes: vec![],
305302
};
306303
fs::write(
307304
"models/orders.json",

crates/vespertide-core/src/action.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::schema::{
2-
ColumnDef, ColumnName, ColumnType, IndexDef, IndexName, TableConstraint, TableName,
3-
};
1+
use crate::schema::{ColumnDef, ColumnName, ColumnType, TableConstraint, TableName};
42
use schemars::JsonSchema;
53
use serde::{Deserialize, Serialize};
64
use std::fmt;
@@ -46,14 +44,6 @@ pub enum MigrationAction {
4644
column: ColumnName,
4745
new_type: ColumnType,
4846
},
49-
AddIndex {
50-
table: TableName,
51-
index: IndexDef,
52-
},
53-
RemoveIndex {
54-
table: TableName,
55-
name: IndexName,
56-
},
5747
AddConstraint {
5848
table: TableName,
5949
constraint: TableConstraint,
@@ -92,12 +82,6 @@ impl fmt::Display for MigrationAction {
9282
MigrationAction::ModifyColumnType { table, column, .. } => {
9383
write!(f, "ModifyColumnType: {}.{}", table, column)
9484
}
95-
MigrationAction::AddIndex { table, index } => {
96-
write!(f, "AddIndex: {}.{}", table, index.name)
97-
}
98-
MigrationAction::RemoveIndex { name, .. } => {
99-
write!(f, "RemoveIndex: {}", name)
100-
}
10185
MigrationAction::AddConstraint { table, constraint } => {
10286
let constraint_name = match constraint {
10387
TableConstraint::PrimaryKey { .. } => "PRIMARY KEY",
@@ -116,6 +100,12 @@ impl fmt::Display for MigrationAction {
116100
TableConstraint::Check { name, .. } => {
117101
return write!(f, "AddConstraint: {}.{} (CHECK)", table, name);
118102
}
103+
TableConstraint::Index { name, .. } => {
104+
if let Some(n) = name {
105+
return write!(f, "AddConstraint: {}.{} (INDEX)", table, n);
106+
}
107+
"INDEX"
108+
}
119109
};
120110
write!(f, "AddConstraint: {}.{}", table, constraint_name)
121111
}
@@ -137,6 +127,12 @@ impl fmt::Display for MigrationAction {
137127
TableConstraint::Check { name, .. } => {
138128
return write!(f, "RemoveConstraint: {}.{} (CHECK)", table, name);
139129
}
130+
TableConstraint::Index { name, .. } => {
131+
if let Some(n) = name {
132+
return write!(f, "RemoveConstraint: {}.{} (INDEX)", table, n);
133+
}
134+
"INDEX"
135+
}
140136
};
141137
write!(f, "RemoveConstraint: {}.{}", table, constraint_name)
142138
}
@@ -159,7 +155,7 @@ impl fmt::Display for MigrationAction {
159155
#[cfg(test)]
160156
mod tests {
161157
use super::*;
162-
use crate::schema::{IndexDef, ReferenceAction, SimpleColumnType};
158+
use crate::schema::{ReferenceAction, SimpleColumnType};
163159
use rstest::rstest;
164160

165161
fn default_column() -> ColumnDef {
@@ -222,23 +218,25 @@ mod tests {
222218
},
223219
"ModifyColumnType: users.age"
224220
)]
225-
#[case::add_index(
226-
MigrationAction::AddIndex {
221+
#[case::add_constraint_index_with_name(
222+
MigrationAction::AddConstraint {
227223
table: "users".into(),
228-
index: IndexDef {
229-
name: "idx_email".into(),
224+
constraint: TableConstraint::Index {
225+
name: Some("ix_users__email".into()),
230226
columns: vec!["email".into()],
231-
unique: false,
232227
},
233228
},
234-
"AddIndex: users.idx_email"
229+
"AddConstraint: users.ix_users__email (INDEX)"
235230
)]
236-
#[case::remove_index(
237-
MigrationAction::RemoveIndex {
231+
#[case::remove_constraint_index_with_name(
232+
MigrationAction::RemoveConstraint {
238233
table: "users".into(),
239-
name: "idx_email".into(),
234+
constraint: TableConstraint::Index {
235+
name: Some("ix_users__email".into()),
236+
columns: vec!["email".into()],
237+
},
240238
},
241-
"RemoveIndex: idx_email"
239+
"RemoveConstraint: users.ix_users__email (INDEX)"
242240
)]
243241
#[case::rename_table(
244242
MigrationAction::RenameTable {

0 commit comments

Comments
 (0)