Skip to content

Commit 8b96dfd

Browse files
committed
wip
1 parent 2e4c657 commit 8b96dfd

68 files changed

Lines changed: 960 additions & 86 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.

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

Lines changed: 339 additions & 52 deletions
Large diffs are not rendered by default.

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

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ pub fn build_action_queries(
7676
MigrationAction::RawSql { sql } => Ok(vec![build_raw_sql(sql.clone())]),
7777

7878
MigrationAction::AddConstraint { table, constraint } => {
79-
build_add_constraint(table, constraint)
79+
build_add_constraint(backend, table, constraint, current_schema)
8080
}
8181

8282
MigrationAction::RemoveConstraint { table, constraint } => {
83-
build_remove_constraint(table, constraint)
83+
build_remove_constraint(backend, table, constraint, current_schema)
8484
}
8585
}
8686
}
@@ -152,6 +152,46 @@ mod tests {
152152
DatabaseBackend::Postgres,
153153
&["DEFAULT", "'active'"]
154154
)]
155+
#[case::create_table_with_default_mysql(
156+
"create_table_with_default_mysql",
157+
MigrationAction::CreateTable {
158+
table: "users".into(),
159+
columns: vec![ColumnDef {
160+
name: "status".into(),
161+
r#type: ColumnType::Simple(SimpleColumnType::Text),
162+
nullable: true,
163+
default: Some("'active'".into()),
164+
comment: None,
165+
primary_key: None,
166+
unique: None,
167+
index: None,
168+
foreign_key: None,
169+
}],
170+
constraints: vec![],
171+
},
172+
DatabaseBackend::Postgres,
173+
&["DEFAULT", "'active'"]
174+
)]
175+
#[case::create_table_with_default_sqlite(
176+
"create_table_with_default_sqlite",
177+
MigrationAction::CreateTable {
178+
table: "users".into(),
179+
columns: vec![ColumnDef {
180+
name: "status".into(),
181+
r#type: ColumnType::Simple(SimpleColumnType::Text),
182+
nullable: true,
183+
default: Some("'active'".into()),
184+
comment: None,
185+
primary_key: None,
186+
unique: None,
187+
index: None,
188+
foreign_key: None,
189+
}],
190+
constraints: vec![],
191+
},
192+
DatabaseBackend::Postgres,
193+
&["DEFAULT", "'active'"]
194+
)]
155195
#[case::create_table_with_inline_primary_key_postgres(
156196
"create_table_with_inline_primary_key_postgres",
157197
MigrationAction::CreateTable {
@@ -172,6 +212,46 @@ mod tests {
172212
DatabaseBackend::Postgres,
173213
&["PRIMARY KEY"]
174214
)]
215+
#[case::create_table_with_inline_primary_key_mysql(
216+
"create_table_with_inline_primary_key_mysql",
217+
MigrationAction::CreateTable {
218+
table: "users".into(),
219+
columns: vec![ColumnDef {
220+
name: "id".into(),
221+
r#type: ColumnType::Simple(SimpleColumnType::Integer),
222+
nullable: false,
223+
default: None,
224+
comment: None,
225+
primary_key: Some(PrimaryKeySyntax::Bool(true)),
226+
unique: None,
227+
index: None,
228+
foreign_key: None,
229+
}],
230+
constraints: vec![],
231+
},
232+
DatabaseBackend::Postgres,
233+
&["PRIMARY KEY"]
234+
)]
235+
#[case::create_table_with_inline_primary_key_sqlite(
236+
"create_table_with_inline_primary_key_sqlite",
237+
MigrationAction::CreateTable {
238+
table: "users".into(),
239+
columns: vec![ColumnDef {
240+
name: "id".into(),
241+
r#type: ColumnType::Simple(SimpleColumnType::Integer),
242+
nullable: false,
243+
default: None,
244+
comment: None,
245+
primary_key: Some(PrimaryKeySyntax::Bool(true)),
246+
unique: None,
247+
index: None,
248+
foreign_key: None,
249+
}],
250+
constraints: vec![],
251+
},
252+
DatabaseBackend::Postgres,
253+
&["PRIMARY KEY"]
254+
)]
175255
#[case::create_table_with_fk_postgres(
176256
"create_table_with_fk_postgres",
177257
MigrationAction::CreateTable {
@@ -192,6 +272,46 @@ mod tests {
192272
DatabaseBackend::Postgres,
193273
&["REFERENCES \"users\" (\"id\")", "ON DELETE CASCADE", "ON UPDATE RESTRICT"]
194274
)]
275+
#[case::create_table_with_fk_mysql(
276+
"create_table_with_fk_mysql",
277+
MigrationAction::CreateTable {
278+
table: "posts".into(),
279+
columns: vec![
280+
col("id", ColumnType::Simple(SimpleColumnType::Integer)),
281+
col("user_id", ColumnType::Simple(SimpleColumnType::Integer)),
282+
],
283+
constraints: vec![TableConstraint::ForeignKey {
284+
name: Some("fk_user".into()),
285+
columns: vec!["user_id".into()],
286+
ref_table: "users".into(),
287+
ref_columns: vec!["id".into()],
288+
on_delete: Some(ReferenceAction::Cascade),
289+
on_update: Some(ReferenceAction::Restrict),
290+
}],
291+
},
292+
DatabaseBackend::Postgres,
293+
&["REFERENCES \"users\" (\"id\")", "ON DELETE CASCADE", "ON UPDATE RESTRICT"]
294+
)]
295+
#[case::create_table_with_fk_sqlite(
296+
"create_table_with_fk_sqlite",
297+
MigrationAction::CreateTable {
298+
table: "posts".into(),
299+
columns: vec![
300+
col("id", ColumnType::Simple(SimpleColumnType::Integer)),
301+
col("user_id", ColumnType::Simple(SimpleColumnType::Integer)),
302+
],
303+
constraints: vec![TableConstraint::ForeignKey {
304+
name: Some("fk_user".into()),
305+
columns: vec!["user_id".into()],
306+
ref_table: "users".into(),
307+
ref_columns: vec!["id".into()],
308+
on_delete: Some(ReferenceAction::Cascade),
309+
on_update: Some(ReferenceAction::Restrict),
310+
}],
311+
},
312+
DatabaseBackend::Postgres,
313+
&["REFERENCES \"users\" (\"id\")", "ON DELETE CASCADE", "ON UPDATE RESTRICT"]
314+
)]
195315
fn test_build_migration_action(
196316
#[case] title: &str,
197317
#[case] action: MigrationAction,

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mod tests {
116116
use super::*;
117117
use insta::{assert_snapshot, with_settings};
118118
use rstest::rstest;
119-
use vespertide_core::{ColumnType, ComplexColumnType};
119+
use vespertide_core::{ColumnDef, ColumnType, ComplexColumnType, SimpleColumnType, TableDef};
120120

121121
#[rstest]
122122
#[case::modify_column_type_postgres(
@@ -139,34 +139,54 @@ mod tests {
139139
#[case] backend: DatabaseBackend,
140140
#[case] expected: &[&str],
141141
) {
142+
// For SQLite, we need to provide current schema
143+
let current_schema = vec![TableDef {
144+
name: "users".into(),
145+
columns: vec![
146+
ColumnDef {
147+
name: "id".into(),
148+
r#type: ColumnType::Simple(SimpleColumnType::Integer),
149+
nullable: false,
150+
default: None,
151+
comment: None,
152+
primary_key: None,
153+
unique: None,
154+
index: None,
155+
foreign_key: None,
156+
},
157+
ColumnDef {
158+
name: "age".into(),
159+
r#type: ColumnType::Simple(SimpleColumnType::Integer),
160+
nullable: true,
161+
default: None,
162+
comment: None,
163+
primary_key: None,
164+
unique: None,
165+
index: None,
166+
foreign_key: None,
167+
},
168+
],
169+
constraints: vec![],
170+
indexes: vec![],
171+
}];
172+
142173
let result = build_modify_column_type(
143174
&backend,
144175
"users",
145176
"age",
146177
&ColumnType::Complex(ComplexColumnType::Varchar { length: 50 }),
147-
&[],
178+
&current_schema,
148179
);
149180

150181
// SQLite may return multiple queries
151-
let sql = if result.is_ok() {
182+
let sql =
152183
result
153184
.unwrap()
154185
.iter()
155186
.map(|q| q.build(backend))
156187
.collect::<Vec<_>>()
157188
.join(";\n")
158-
} else {
159-
// SQLite may error if schema information is missing
160-
if backend == DatabaseBackend::Sqlite {
161-
return; // Skip SQLite test as it requires schema information
162-
}
163-
result
164-
.unwrap()
165-
.iter()
166-
.map(|q| q.build(backend))
167-
.collect::<Vec<_>>()
168-
.join(";\n")
169-
};
189+
;
170190

171191
for exp in expected {
172192
assert!(
@@ -176,6 +196,7 @@ mod tests {
176196
sql
177197
);
178198
}
199+
println!("sql: {}", sql);
179200

180201
with_settings!({ snapshot_suffix => format!("modify_column_type_{}", title) }, {
181202
assert_snapshot!(sql);

0 commit comments

Comments
 (0)