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_kZhbf9xXT865BdVdu7Dl6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide/Cargo.toml":"Patch","crates/vespertide-naming/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide-macro/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-loader/Cargo.toml":"Patch"},"note":"Fix index column issue","date":"2026-04-13T08:37:23.992645500Z"}
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.

15 changes: 12 additions & 3 deletions crates/vespertide-planner/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,22 @@ pub fn diff_schemas(from: &[TableDef], to: &[TableDef]) -> Result<MigrationPlan,
}

// Added columns
// Note: Inline foreign keys are already converted to TableConstraint::ForeignKey
// by normalize(), so they will be handled in the constraint diff below.
// Inline constraints (index, unique, foreign_key, primary_key) are already
// promoted to table-level TableConstraint entries by normalize(), so they
// will be emitted as separate AddConstraint actions in the constraint diff
// below. Strip them from the column def to prevent apply_action's normalize()
// from adding phantom constraints to the evolving schema, which would cause
// premature index recreation during SQLite temp table rebuilds.
for (col, def) in &to_cols {
if !from_cols.contains_key(col) {
let mut col_def = (*def).clone();
col_def.primary_key = None;
col_def.unique = None;
col_def.index = None;
col_def.foreign_key = None;
actions.push(MigrationAction::AddColumn {
table: name.to_string(),
column: Box::new((*def).clone()),
column: Box::new(col_def),
fill_with: None,
});
}
Expand Down
124 changes: 123 additions & 1 deletion crates/vespertide-query/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ pub struct PlanQueries {
pub sqlite: Vec<BuiltQuery>,
}

/// Extract the target table name from any migration action.
/// Returns `None` for `RawSql` (no table) and `RenameTable` (ambiguous).
fn action_target_table(action: &MigrationAction) -> Option<&str> {
match action {
MigrationAction::CreateTable { table, .. }
| MigrationAction::DeleteTable { table }
| MigrationAction::AddColumn { table, .. }
| MigrationAction::RenameColumn { table, .. }
| MigrationAction::DeleteColumn { table, .. }
| MigrationAction::ModifyColumnType { table, .. }
| MigrationAction::ModifyColumnNullable { table, .. }
| MigrationAction::ModifyColumnDefault { table, .. }
| MigrationAction::ModifyColumnComment { table, .. }
| MigrationAction::AddConstraint { table, .. }
| MigrationAction::RemoveConstraint { table, .. } => Some(table),
MigrationAction::RenameTable { .. } | MigrationAction::RawSql { .. } => None,
}
}

pub fn build_plan_queries(
plan: &MigrationPlan,
current_schema: &[TableDef],
Expand All @@ -27,8 +46,13 @@ pub fn build_plan_queries(
// but haven't been physically created as DB indexes yet.
// Without this, a temp table rebuild would recreate these indexes prematurely,
// causing "index already exists" errors when their AddConstraint actions run later.
//
// This applies to ANY action that may trigger a SQLite temp table rebuild
// (AddColumn with NOT NULL, ModifyColumn*, DeleteColumn, AddConstraint FK/PK/Check,
// RemoveConstraint), not just AddConstraint.
let action_table = action_target_table(action);
let pending_constraints: Vec<vespertide_core::TableConstraint> =
if let MigrationAction::AddConstraint { table, .. } = action {
if let Some(table) = action_table {
plan.actions[i + 1..]
.iter()
.filter_map(|a| {
Expand Down Expand Up @@ -765,4 +789,102 @@ mod tests {
assert_snapshot!(sql);
});
}

// ── Two NOT NULL AddColumns with inline index + AddConstraint ────────

/// Regression test: when two NOT NULL columns with inline `index: true`
/// are added sequentially, the second AddColumn triggers a SQLite temp
/// table rebuild. At that point the evolving schema already contains the
/// first column's index (from normalization). Without pending constraint
/// awareness, the rebuild recreates that index, and the later
/// AddConstraint for the same index fails with "index already exists".
#[rstest]
#[case::postgres("postgres", DatabaseBackend::Postgres)]
#[case::mysql("mysql", DatabaseBackend::MySql)]
#[case::sqlite("sqlite", DatabaseBackend::Sqlite)]
fn test_two_not_null_add_columns_with_inline_index_no_duplicate(
#[case] label: &str,
#[case] backend: DatabaseBackend,
) {
use vespertide_core::DefaultValue;
use vespertide_core::schema::str_or_bool::StrOrBoolOrArray;

let schema = vec![TableDef {
name: "article".into(),
description: None,
columns: vec![
col("id", ColumnType::Simple(SimpleColumnType::Integer)),
col("title", ColumnType::Simple(SimpleColumnType::Text)),
],
constraints: vec![],
}];

let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
actions: vec![
// 1. Add NOT NULL column with inline index
MigrationAction::AddColumn {
table: "article".into(),
column: Box::new(ColumnDef {
name: "category_pinned".into(),
r#type: ColumnType::Simple(SimpleColumnType::Boolean),
nullable: false,
default: Some(DefaultValue::Bool(false)),
comment: None,
primary_key: None,
unique: None,
index: Some(StrOrBoolOrArray::Bool(true)),
foreign_key: None,
}),
fill_with: None,
},
// 2. Add another NOT NULL column with inline index
MigrationAction::AddColumn {
table: "article".into(),
column: Box::new(ColumnDef {
name: "main_pinned".into(),
r#type: ColumnType::Simple(SimpleColumnType::Boolean),
nullable: false,
default: Some(DefaultValue::Bool(false)),
comment: None,
primary_key: None,
unique: None,
index: Some(StrOrBoolOrArray::Bool(true)),
foreign_key: None,
}),
fill_with: None,
},
// 3. AddConstraint for main_pinned index
MigrationAction::AddConstraint {
table: "article".into(),
constraint: TableConstraint::Index {
name: None,
columns: vec!["main_pinned".into()],
},
},
// 4. AddConstraint for category_pinned index
MigrationAction::AddConstraint {
table: "article".into(),
constraint: TableConstraint::Index {
name: None,
columns: vec!["category_pinned".into()],
},
},
],
};

let result = build_plan_queries(&plan, &schema).unwrap();

// Core invariant: no duplicate indexes across actions
assert_no_duplicate_indexes_per_action(&result);
assert_no_orphan_duplicate_indexes(&result);

let sql = collect_all_sql(&result, backend);
with_settings!({ snapshot_suffix => format!("two_not_null_inline_index_{}", label) }, {
assert_snapshot!(sql);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/vespertide-query/src/builder.rs
expression: sql
---
-- Action 0: AddColumn { table: "article", column: ColumnDef { name: "category_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
ALTER TABLE `article` ADD COLUMN `category_pinned` bool NOT NULL DEFAULT false

-- Action 1: AddColumn { table: "article", column: ColumnDef { name: "main_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
ALTER TABLE `article` ADD COLUMN `main_pinned` bool NOT NULL DEFAULT false

-- Action 2: AddConstraint { table: "article", constraint: Index { name: None, columns: ["main_pinned"] } }
CREATE INDEX `ix_article__main_pinned` ON `article` (`main_pinned`)

-- Action 3: AddConstraint { table: "article", constraint: Index { name: None, columns: ["category_pinned"] } }
CREATE INDEX `ix_article__category_pinned` ON `article` (`category_pinned`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/vespertide-query/src/builder.rs
expression: sql
---
-- Action 0: AddColumn { table: "article", column: ColumnDef { name: "category_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
ALTER TABLE "article" ADD COLUMN "category_pinned" bool NOT NULL DEFAULT false

-- Action 1: AddColumn { table: "article", column: ColumnDef { name: "main_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
ALTER TABLE "article" ADD COLUMN "main_pinned" bool NOT NULL DEFAULT false

-- Action 2: AddConstraint { table: "article", constraint: Index { name: None, columns: ["main_pinned"] } }
CREATE INDEX "ix_article__main_pinned" ON "article" ("main_pinned")

-- Action 3: AddConstraint { table: "article", constraint: Index { name: None, columns: ["category_pinned"] } }
CREATE INDEX "ix_article__category_pinned" ON "article" ("category_pinned")
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/vespertide-query/src/builder.rs
expression: sql
---
-- Action 0: AddColumn { table: "article", column: ColumnDef { name: "category_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
CREATE TABLE "article_temp" ( "id" integer, "title" text, "category_pinned" boolean NOT NULL DEFAULT false );
INSERT INTO "article_temp" ("id", "title", "category_pinned") SELECT "id", "title", false AS "category_pinned" FROM "article";
DROP TABLE "article";
ALTER TABLE "article_temp" RENAME TO "article"

-- Action 1: AddColumn { table: "article", column: ColumnDef { name: "main_pinned", type: Simple(Boolean), nullable: false, default: Some(Bool(false)), comment: None, primary_key: None, unique: None, index: Some(Bool(true)), foreign_key: None }, fill_with: None }
CREATE TABLE "article_temp" ( "id" integer, "title" text, "category_pinned" boolean NOT NULL DEFAULT false, "main_pinned" boolean NOT NULL DEFAULT false );
INSERT INTO "article_temp" ("id", "title", "category_pinned", "main_pinned") SELECT "id", "title", "category_pinned", false AS "main_pinned" FROM "article";
DROP TABLE "article";
ALTER TABLE "article_temp" RENAME TO "article"

-- Action 2: AddConstraint { table: "article", constraint: Index { name: None, columns: ["main_pinned"] } }
CREATE INDEX "ix_article__main_pinned" ON "article" ("main_pinned")

-- Action 3: AddConstraint { table: "article", constraint: Index { name: None, columns: ["category_pinned"] } }
CREATE INDEX "ix_article__category_pinned" ON "article" ("category_pinned")
Loading