Skip to content

Commit 47d2c2f

Browse files
committed
wip
1 parent 443ad17 commit 47d2c2f

17 files changed

Lines changed: 116 additions & 24 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fn emit_sql(
7676
DatabaseBackend::MySql => &pq.mysql,
7777
DatabaseBackend::Sqlite => &pq.sqlite,
7878
};
79+
println!("{} {}", "Action:".bright_cyan(), pq.action.to_string().bright_white());
7980
for (j, q) in queries.iter().enumerate() {
8081
println!(
8182
"{}{}. {}",

crates/vespertide-cli/src/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::PathBuf;
2-
31
use vespertide_config::FileFormat;
42

53
// Re-export loader functions for convenience
@@ -107,6 +105,7 @@ mod tests {
107105
use rstest::rstest;
108106
use serial_test::serial;
109107
use std::fs;
108+
use std::path::PathBuf;
110109
use tempfile::tempdir;
111110
use vespertide_config::VespertideConfig;
112111
use vespertide_core::{

crates/vespertide-core/src/action.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::schema::{
33
};
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
6+
use std::fmt;
67

78
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
89
#[serde(rename_all = "snake_case")]
@@ -69,3 +70,88 @@ pub enum MigrationAction {
6970
sql: String,
7071
},
7172
}
73+
74+
impl fmt::Display for MigrationAction {
75+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76+
match self {
77+
MigrationAction::CreateTable { table, .. } => {
78+
write!(f, "CreateTable: {}", table)
79+
}
80+
MigrationAction::DeleteTable { table } => {
81+
write!(f, "DeleteTable: {}", table)
82+
}
83+
MigrationAction::AddColumn { table, column, .. } => {
84+
write!(f, "AddColumn: {}.{}", table, column.name)
85+
}
86+
MigrationAction::RenameColumn { table, from, to } => {
87+
write!(f, "RenameColumn: {}.{} -> {}", table, from, to)
88+
}
89+
MigrationAction::DeleteColumn { table, column } => {
90+
write!(f, "DeleteColumn: {}.{}", table, column)
91+
}
92+
MigrationAction::ModifyColumnType { table, column, .. } => {
93+
write!(f, "ModifyColumnType: {}.{}", table, column)
94+
}
95+
MigrationAction::AddIndex { table, index } => {
96+
write!(f, "AddIndex: {}.{}", table, index.name)
97+
}
98+
MigrationAction::RemoveIndex { name, .. } => {
99+
write!(f, "RemoveIndex: {}", name)
100+
}
101+
MigrationAction::AddConstraint { table, constraint } => {
102+
let constraint_name = match constraint {
103+
TableConstraint::PrimaryKey { .. } => "PRIMARY KEY",
104+
TableConstraint::Unique { name, .. } => {
105+
if let Some(n) = name {
106+
return write!(f, "AddConstraint: {}.{} (UNIQUE)", table, n);
107+
}
108+
"UNIQUE"
109+
}
110+
TableConstraint::ForeignKey { name, .. } => {
111+
if let Some(n) = name {
112+
return write!(f, "AddConstraint: {}.{} (FOREIGN KEY)", table, n);
113+
}
114+
"FOREIGN KEY"
115+
}
116+
TableConstraint::Check { name, .. } => {
117+
return write!(f, "AddConstraint: {}.{} (CHECK)", table, name);
118+
}
119+
};
120+
write!(f, "AddConstraint: {}.{}", table, constraint_name)
121+
}
122+
MigrationAction::RemoveConstraint { table, constraint } => {
123+
let constraint_name = match constraint {
124+
TableConstraint::PrimaryKey { .. } => "PRIMARY KEY",
125+
TableConstraint::Unique { name, .. } => {
126+
if let Some(n) = name {
127+
return write!(f, "RemoveConstraint: {}.{} (UNIQUE)", table, n);
128+
}
129+
"UNIQUE"
130+
}
131+
TableConstraint::ForeignKey { name, .. } => {
132+
if let Some(n) = name {
133+
return write!(f, "RemoveConstraint: {}.{} (FOREIGN KEY)", table, n);
134+
}
135+
"FOREIGN KEY"
136+
}
137+
TableConstraint::Check { name, .. } => {
138+
return write!(f, "RemoveConstraint: {}.{} (CHECK)", table, name);
139+
}
140+
};
141+
write!(f, "RemoveConstraint: {}.{}", table, constraint_name)
142+
}
143+
MigrationAction::RenameTable { from, to } => {
144+
write!(f, "RenameTable: {} -> {}", from, to)
145+
}
146+
MigrationAction::RawSql { sql } => {
147+
// Truncate SQL if too long for display
148+
let display_sql = if sql.len() > 50 {
149+
format!("{}...", &sql[..47])
150+
} else {
151+
sql.clone()
152+
};
153+
write!(f, "RawSql: {}", display_sql)
154+
}
155+
}
156+
}
157+
}

crates/vespertide-loader/src/migrations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::env;
22
use std::fs;
3-
use std::path::{Path, PathBuf};
3+
use std::path::PathBuf;
44

55
use anyhow::{Context, Result};
66
use vespertide_config::VespertideConfig;

crates/vespertide-loader/src/models.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn load_models(config: &VespertideConfig) -> Result<Vec<TableDef>> {
1414
}
1515

1616
let mut tables = Vec::new();
17-
load_models_recursive(&models_dir, &mut tables)?;
17+
load_models_recursive(models_dir, &mut tables)?;
1818

1919
// Normalize tables to convert inline constraints (primary_key, foreign_key, etc.) to table-level constraints
2020
// This must happen before validation so that foreign key references can be checked
@@ -77,8 +77,6 @@ pub fn load_models_from_dir(
7777
project_root: Option<std::path::PathBuf>,
7878
) -> Result<Vec<TableDef>, Box<dyn std::error::Error>> {
7979
use std::env;
80-
use std::fs;
81-
use std::path::Path;
8280

8381
// Locate project root from CARGO_MANIFEST_DIR or use provided path
8482
let project_root = if let Some(root) = project_root {
@@ -111,7 +109,7 @@ pub fn load_models_from_dir(
111109
.map_err(|e| format!("Failed to normalize table '{}': {}", t.name, e))
112110
})
113111
.collect::<Result<Vec<_>, _>>()
114-
.map_err(|e| format!("{}", e))?;
112+
.map_err(|e| e.to_string())?;
115113

116114
Ok(normalized_tables)
117115
}
@@ -163,7 +161,6 @@ pub fn load_models_at_compile_time() -> Result<Vec<TableDef>, Box<dyn std::error
163161
#[cfg(test)]
164162
mod tests {
165163
use super::*;
166-
use rstest::rstest;
167164
use serial_test::serial;
168165
use std::fs;
169166
use tempfile::tempdir;

crates/vespertide-query/src/builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use vespertide_core::{MigrationPlan, TableDef};
1+
use vespertide_core::{MigrationAction, MigrationPlan, TableDef};
22

33
use crate::DatabaseBackend;
44
use crate::error::QueryError;
55
use crate::sql::{BuiltQuery, build_action_queries};
66

77
pub struct PlanQueries {
8+
pub action: MigrationAction,
89
pub postgres: Vec<BuiltQuery>,
910
pub mysql: Vec<BuiltQuery>,
1011
pub sqlite: Vec<BuiltQuery>,
@@ -20,6 +21,7 @@ pub fn build_plan_queries(
2021
let mysql_queries = build_action_queries(&DatabaseBackend::MySql, action, current_schema)?;
2122
let sqlite_queries = build_action_queries(&DatabaseBackend::Sqlite, action, current_schema)?;
2223
queries.push(PlanQueries {
24+
action: action.clone(),
2325
postgres: postgres_queries,
2426
mysql: mysql_queries,
2527
sqlite: sqlite_queries,

crates/vespertide-query/src/sql/add_column.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn build_add_column(
3535
temp_col.nullable = true;
3636

3737
stmts.push(BuiltQuery::AlterTable(
38-
Box::new(build_add_column_alter_for_backend(backend, &table, &temp_col)),
38+
Box::new(build_add_column_alter_for_backend(backend, table, &temp_col)),
3939
));
4040

4141
// Backfill with provided value

crates/vespertide-query/src/sql/add_constraint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use vespertide_core::{ReferenceAction, TableConstraint};
1+
use vespertide_core::TableConstraint;
22

33
use crate::error::QueryError;
4-
use super::types::{BuiltQuery, DatabaseBackend, RawSql};
4+
use super::types::{BuiltQuery, RawSql};
55
use super::helpers::reference_action_sql;
66

77
pub fn build_add_constraint(
@@ -155,6 +155,7 @@ pub fn build_add_constraint(
155155
#[cfg(test)]
156156
mod tests {
157157
use super::*;
158+
use crate::sql::types::DatabaseBackend;
158159
use insta::{assert_snapshot, with_settings};
159160
use rstest::rstest;
160161
use vespertide_core::{ReferenceAction, TableConstraint};

crates/vespertide-query/src/sql/add_index.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use sea_query::{Alias, Index};
22

33
use vespertide_core::IndexDef;
44

5-
use super::types::{BuiltQuery, DatabaseBackend};
5+
use super::types::BuiltQuery;
66

77
pub fn build_add_index(table: &str, index: &IndexDef) -> BuiltQuery {
88
let mut stmt = Index::create()
@@ -24,6 +24,7 @@ pub fn build_add_index(table: &str, index: &IndexDef) -> BuiltQuery {
2424
#[cfg(test)]
2525
mod tests {
2626
use super::*;
27+
use crate::sql::types::DatabaseBackend;
2728
use insta::{assert_snapshot, with_settings};
2829
use rstest::rstest;
2930
use vespertide_core::IndexDef;

crates/vespertide-query/src/sql/create_table.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use sea_query::{Alias, ColumnDef as SeaColumnDef, ForeignKey, Index, Table, TableCreateStatement};
1+
use sea_query::{Alias, ForeignKey, Index, Table, TableCreateStatement};
22

33
use vespertide_core::{ColumnDef, TableConstraint};
44

55
use crate::error::QueryError;
66
use super::types::{BuiltQuery, DatabaseBackend};
7-
use super::helpers::{apply_column_type, build_sea_column_def, to_sea_fk_action};
7+
use super::helpers::{build_sea_column_def, to_sea_fk_action};
88

99
pub(crate) fn build_create_table_for_backend(
1010
backend: &DatabaseBackend,
@@ -118,9 +118,8 @@ mod tests {
118118
use super::*;
119119
use insta::{assert_snapshot, with_settings};
120120
use rstest::rstest;
121-
use vespertide_core::schema::primary_key::PrimaryKeySyntax;
122121
use vespertide_core::{
123-
ColumnType, ComplexColumnType, IndexDef, SimpleColumnType, StrOrBoolOrArray,
122+
ColumnType, SimpleColumnType,
124123
};
125124

126125
fn col(name: &str, ty: ColumnType) -> ColumnDef {

0 commit comments

Comments
 (0)