Skip to content

Commit 49fd5c8

Browse files
authored
Merge pull request #20 from dev-five-git/fix-query-builder
Fix query builder
2 parents 0a3e8c7 + 19810d7 commit 49fd5c8

136 files changed

Lines changed: 2837 additions & 958 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"crates/vespertide-macro/Cargo.toml":"Patch","crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide/Cargo.toml":"Patch"},"note":"Optimize query","date":"2025-12-15T06:52:49.007765800Z"}

Cargo.lock

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

crates/vespertide-cli/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/models
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dev-five-git/vespertide/refs/heads/main/schemas/migration.schema.json",
3+
"actions": [
4+
{
5+
"columns": [],
6+
"constraints": [],
7+
"table": "test_table",
8+
"type": "create_table"
9+
}
10+
],
11+
"comment": "test message",
12+
"created_at": "2025-12-15T06:12:57Z",
13+
"version": 1
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dev-five-git/vespertide/refs/heads/main/schemas/model.schema.json",
3+
"columns": [],
4+
"constraints": [],
5+
"indexes": [],
6+
"name": "test_table"
7+
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ fn format_constraint_type(constraint: &vespertide_core::TableConstraint) -> Stri
169169
}
170170
}
171171
vespertide_core::TableConstraint::Check { name, expr } => {
172-
if let Some(n) = name {
173-
format!("{} CHECK ({})", n, expr)
174-
} else {
175-
format!("CHECK ({})", expr)
176-
}
172+
format!("{} CHECK ({})", name, expr)
177173
}
178174
}
179175
}
@@ -345,7 +341,7 @@ mod tests {
345341
MigrationAction::AddConstraint {
346342
table: "users".into(),
347343
constraint: vespertide_core::TableConstraint::Check {
348-
name: Some("check_age".into()),
344+
name: "check_age".into(),
349345
expr: "age > 0".into(),
350346
},
351347
},
@@ -389,11 +385,17 @@ mod tests {
389385
MigrationAction::RemoveConstraint {
390386
table: "users".into(),
391387
constraint: vespertide_core::TableConstraint::Check {
392-
name: None,
388+
name: "check_age".into(),
393389
expr: "age > 0".into(),
394390
},
395391
},
396-
format!("{} {} {} {}", "Remove constraint:".bright_red(), "CHECK (age > 0)".bright_cyan().bold(), "from".bright_white(), "users".bright_cyan())
392+
format!(
393+
"{} {} {} {}",
394+
"Remove constraint:".bright_red(),
395+
"check_age CHECK (age > 0)".bright_cyan().bold(),
396+
"from".bright_white(),
397+
"users".bright_cyan()
398+
)
397399
)]
398400
#[serial]
399401
fn format_action_cases(#[case] action: MigrationAction, #[case] expected: String) {

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Result;
22
use colored::Colorize;
3-
use vespertide_query::build_plan_queries;
3+
use vespertide_query::{DatabaseBackend, build_plan_queries};
44

55
use crate::utils::load_migrations;
66

@@ -54,11 +54,9 @@ pub fn cmd_log() -> Result<()> {
5454
println!(
5555
" {}. {}",
5656
(i + 1).to_string().bright_magenta().bold(),
57-
q.sql.trim().bright_white()
57+
q.build(DatabaseBackend::Postgres).trim().bright_white()
5858
);
59-
if !q.binds.is_empty() {
60-
println!(" {} {:?}", "binds:".bright_cyan(), q.binds);
61-
}
59+
println!(" {} {:?}", "binds:".bright_cyan(), q.binds());
6260
}
6361
println!();
6462
}

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
22
use colored::Colorize;
33
use vespertide_planner::plan_next_migration;
4-
use vespertide_query::build_plan_queries;
4+
use vespertide_query::{DatabaseBackend, build_plan_queries};
55

66
use crate::utils::{load_config, load_migrations, load_models};
77

@@ -60,11 +60,9 @@ fn emit_sql(plan: &vespertide_core::MigrationPlan) -> Result<()> {
6060
println!(
6161
"{}. {}",
6262
(i + 1).to_string().bright_magenta().bold(),
63-
q.sql.trim().bright_white()
63+
q.build(DatabaseBackend::Postgres).trim().bright_white()
6464
);
65-
if !q.binds.is_empty() {
66-
println!(" {} {:?}", "binds:".bright_cyan(), q.binds);
67-
}
65+
println!(" {} {:?}", "binds:".bright_cyan(), q.binds());
6866
}
6967

7068
Ok(())
@@ -137,31 +135,28 @@ mod tests {
137135
let tmp = tempdir().unwrap();
138136
let _guard = CwdGuard::new(&tmp.path().to_path_buf());
139137

140-
let cfg = write_config();
138+
let _cfg = write_config();
141139
write_model("users");
142-
fs::create_dir_all(cfg.migrations_dir()).unwrap();
143140

141+
// No migrations yet -> plan will create table
144142
let result = cmd_sql();
145143
assert!(result.is_ok());
146144
}
147145

148146
#[test]
149-
fn emit_sql_no_actions_early_return() {
147+
#[serial]
148+
fn cmd_sql_no_changes() {
149+
let tmp = tempdir().unwrap();
150+
let _guard = CwdGuard::new(&tmp.path().to_path_buf());
151+
152+
let cfg = write_config();
153+
write_model("users");
154+
155+
// Create initial migration to establish baseline
150156
let plan = MigrationPlan {
151157
comment: None,
152158
created_at: None,
153159
version: 1,
154-
actions: vec![],
155-
};
156-
assert!(emit_sql(&plan).is_ok());
157-
}
158-
159-
#[test]
160-
fn emit_sql_with_metadata() {
161-
let plan = MigrationPlan {
162-
comment: Some("init".into()),
163-
created_at: Some("2024-01-01T00:00:00Z".into()),
164-
version: 1,
165160
actions: vec![MigrationAction::CreateTable {
166161
table: "users".into(),
167162
columns: vec![ColumnDef {
@@ -181,6 +176,27 @@ mod tests {
181176
}],
182177
}],
183178
};
184-
assert!(emit_sql(&plan).is_ok());
179+
fs::create_dir_all(cfg.migrations_dir()).unwrap();
180+
let path = cfg.migrations_dir().join("0001_init.json");
181+
fs::write(path, serde_json::to_string_pretty(&plan).unwrap()).unwrap();
182+
183+
let result = cmd_sql();
184+
assert!(result.is_ok());
185+
}
186+
187+
#[test]
188+
#[serial]
189+
fn emit_sql_prints_created_at_and_comment() {
190+
let plan = MigrationPlan {
191+
comment: Some("with comment".into()),
192+
created_at: Some("2024-01-02T00:00:00Z".into()),
193+
version: 1,
194+
actions: vec![MigrationAction::RawSql {
195+
sql: "SELECT 1;".into(),
196+
}],
197+
};
198+
199+
let result = emit_sql(&plan);
200+
assert!(result.is_ok());
185201
}
186202
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"modelsDir": "models",
3+
"migrationsDir": "migrations",
4+
"tableNamingCase": "snake",
5+
"columnNamingCase": "snake",
6+
"modelFormat": "json",
7+
"migrationFormat": "json",
8+
"migrationFilenamePattern": "%04v_%m",
9+
"modelExportDir": "src/models"
10+
}

crates/vespertide-core/src/schema/constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub enum TableConstraint {
2727
on_update: Option<ReferenceAction>,
2828
},
2929
Check {
30-
name: Option<String>,
30+
name: String,
3131
expr: String,
3232
},
3333
}

0 commit comments

Comments
 (0)