Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_P-BaCMMqTbkbBgarbYpYM.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide-naming/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide-macro/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch","crates/vespertide-loader/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide/Cargo.toml":"Patch"},"note":"Fix json convert issue","date":"2026-02-09T07:54:00.243061300Z"}
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 71 additions & 3 deletions crates/vespertide-query/src/sql/add_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use vespertide_core::{ColumnDef, TableDef};

use super::helpers::{
build_create_enum_type_sql, build_sea_column_def_with_table, build_sqlite_temp_table_create,
normalize_enum_default, normalize_fill_with, recreate_indexes_after_rebuild,
convert_default_for_backend, normalize_enum_default, normalize_fill_with,
recreate_indexes_after_rebuild,
};
use super::rename_table::build_rename_table;
use super::types::{BuiltQuery, DatabaseBackend};
Expand Down Expand Up @@ -66,9 +67,11 @@ pub fn build_add_column(
}
let normalized_fill = normalize_fill_with(fill_with);
let fill_expr = if let Some(fill) = normalized_fill.as_deref() {
Expr::cust(normalize_enum_default(&column.r#type, fill))
let converted = convert_default_for_backend(fill, backend);
Expr::cust(normalize_enum_default(&column.r#type, &converted))
} else if let Some(def) = &column.default {
Expr::cust(normalize_enum_default(&column.r#type, &def.to_sql()))
let converted = convert_default_for_backend(&def.to_sql(), backend);
Expr::cust(normalize_enum_default(&column.r#type, &converted))
} else {
Expr::cust("NULL")
};
Expand Down Expand Up @@ -124,6 +127,7 @@ pub fn build_add_column(

// Backfill with provided value
if let Some(fill) = normalize_fill_with(fill_with) {
let fill = convert_default_for_backend(&fill, backend);
let update_stmt = Query::update()
.table(Alias::new(table))
.value(Alias::new(&column.name), Expr::cust(fill))
Expand Down Expand Up @@ -604,6 +608,70 @@ mod tests {
});
}

/// Test adding NOT NULL column with '[]'::json default on SQLite
/// SQLite should strip the ::json cast, MySQL should use CAST(... AS JSON)
#[rstest]
#[case::postgres(DatabaseBackend::Postgres)]
#[case::mysql(DatabaseBackend::MySql)]
#[case::sqlite(DatabaseBackend::Sqlite)]
fn test_add_column_with_pg_type_cast_default(#[case] backend: DatabaseBackend) {
let column = ColumnDef {
name: "story_index".into(),
r#type: ColumnType::Simple(SimpleColumnType::Json),
nullable: false,
default: Some("'[]'::json".into()),
comment: None,
primary_key: None,
unique: None,
index: None,
foreign_key: None,
};
let current_schema = vec![TableDef {
name: "project".into(),
description: None,
columns: vec![ColumnDef {
name: "id".into(),
r#type: ColumnType::Simple(SimpleColumnType::Integer),
nullable: false,
default: None,
comment: None,
primary_key: None,
unique: None,
index: None,
foreign_key: None,
}],
constraints: vec![],
}];
let result = build_add_column(&backend, "project", &column, None, &current_schema).unwrap();
let sql = result
.iter()
.map(|q| q.build(backend))
.collect::<Vec<String>>()
.join("\n");

// SQLite must NOT contain ::json syntax
if backend == DatabaseBackend::Sqlite {
assert!(
!sql.contains("::json"),
"SQLite SQL should not contain ::json cast, got: {}",
sql
);
}

// MySQL should use CAST syntax
if backend == DatabaseBackend::MySql {
assert!(
!sql.contains("::json"),
"MySQL SQL should not contain ::json cast, got: {}",
sql
);
}

with_settings!({ snapshot_suffix => format!("pg_type_cast_default_{:?}", backend) }, {
assert_snapshot!(sql);
});
}

#[rstest]
#[case::postgres(DatabaseBackend::Postgres)]
#[case::mysql(DatabaseBackend::MySql)]
Expand Down
Loading