fix(schema-diff): require explicit expression index provenance

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-16 14:39:43 +08:00
1 parent 01ea889569
commit 4660a11f25
28 files changed
+673 -39

No files matched your search

@@ -540,6 +540,7 @@ fn clickhouse_index_from_skipping_row(row: &[serde_json::Value]) -> IndexInfo {
}),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
}
}
@@ -709,6 +710,7 @@ pub async fn list_indexes(client: &ChClient, database: &str, table: &str) -> Res
index_type: Some("primary".to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}
@@ -199,6 +199,7 @@ pub async fn list_indexes(client: &CloudflareD1Client, _schema: &str, table: &st
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}
Ok(indexes)
@@ -209,6 +209,7 @@ fn index_info_from_row(row: &mysql_async::Row) -> IndexInfo {
index_type: Some(get_str_by_name(row, "Type")),
included_columns: None,
comment: (!comment_parts.is_empty()).then(|| comment_parts.join(", ")),
key_is_expression: Vec::new(),
}
}
+6
View File
@@ -821,6 +821,7 @@ fn index_info_from_model(model: IndexModel) -> IndexInfo {
index_type,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
}
}
@@ -3525,6 +3526,7 @@ mod tests {
index_type: Some("email: 1".to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
assert_eq!(spec.name, "email_1");
@@ -3549,6 +3551,7 @@ mod tests {
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
assert_eq!(
@@ -3791,6 +3794,7 @@ mod tests {
index_type: Some("_id: 1".to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
},
IndexInfo {
name: "users_email_unique".to_string(),
@@ -3801,6 +3805,7 @@ mod tests {
index_type: Some("email: 1".to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
},
IndexInfo {
name: "users_status_idx".to_string(),
@@ -3811,6 +3816,7 @@ mod tests {
index_type: Some("status: 1".to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
},
];
let after = vec![before[0].clone(), before[2].clone()];
+1
View File
@@ -5170,6 +5170,7 @@ pub async fn list_indexes(pool: &MySqlPool, database: &str, table: &str) -> Resu
index_type: Some(get_str_by_name(&row, "INDEX_TYPE")),
included_columns: None,
comment: get_opt_str(&row, "INDEX_COMMENT").filter(|value| !value.is_empty()),
key_is_expression: Vec::new(),
});
index_position
};
@@ -323,6 +323,7 @@ fn table_key_index(name: &str, line: &str, is_unique: bool, is_primary: bool, in
index_type: Some(index_type.to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
})
}
@@ -342,6 +343,7 @@ fn secondary_index(line: &str) -> Option<IndexInfo> {
index_type: mysql_keyword_argument(after_name, "USING").or_else(|| Some("INDEX".to_string())),
included_columns: None,
comment: mysql_quoted_string_argument(after_name, "COMMENT"),
key_is_expression: Vec::new(),
})
}
+1
View File
@@ -241,6 +241,7 @@ pub async fn list_indexes(pool: &mysql_async::Pool, schema: &str, table: &str) -
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
}
})
.collect())
+27 -2
View File
@@ -5281,7 +5281,8 @@ const POSTGRES_INDEXES_SQL: &str = "SELECT i.relname AS index_name, \
am.amname AS index_type, \
ix.indnkeyatts AS nkeyatts, \
ix.indkey AS indkey, \
obj_description(i.oid, 'pg_class') AS index_comment \
obj_description(i.oid, 'pg_class') AS index_comment, \
array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression \
FROM pg_index ix \
JOIN pg_class t ON t.oid = ix.indrelid \
JOIN pg_class i ON i.oid = ix.indexrelid \
@@ -5309,7 +5310,16 @@ const POSTGRES_INDEXES_COMPAT_SQL: &str = "SELECT i.relname AS index_name, \
am.amname AS index_type, \
NULL::smallint AS nkeyatts, \
ix.indkey AS indkey, \
obj_description(i.oid, 'pg_class') AS index_comment \
obj_description(i.oid, 'pg_class') AS index_comment, \
ARRAY( \
SELECT a.attname IS NULL \
FROM generate_series(1, array_length(string_to_array(ix.indkey::text, ' '), 1)) AS pos(n) \
LEFT JOIN pg_attribute a \
ON a.attrelid = t.oid \
AND a.attnum = (string_to_array(ix.indkey::text, ' '))[pos.n]::int2 \
AND a.attnum > 0 \
ORDER BY pos.n \
) AS key_is_expression \
FROM pg_index ix \
JOIN pg_class t ON t.oid = ix.indrelid \
JOIN pg_class i ON i.oid = ix.indexrelid \
@@ -5385,6 +5395,11 @@ async fn list_indexes_with_sql(
let split_at = nkeyatts.min(all_cols.len());
let key_cols = all_cols[..split_at].to_vec();
let included = if split_at < all_cols.len() { all_cols[split_at..].to_vec() } else { vec![] };
// `a.attname IS NULL` at a given key position means that key part came back from
// pg_get_indexdef (a functional/expression key part), not from a real column (#6295).
let all_is_expr: Vec<bool> = row.try_get::<_, Vec<bool>>(9).unwrap_or_default();
let key_is_expression =
if all_is_expr.len() == all_cols.len() { all_is_expr[..split_at].to_vec() } else { Vec::new() };
IndexInfo {
name: pg_row_try_string(row, 0),
columns: key_cols,
@@ -5394,6 +5409,7 @@ async fn list_indexes_with_sql(
index_type: row.try_get::<_, Option<String>>(5).ok().flatten(),
included_columns: if included.is_empty() { None } else { Some(included) },
comment: row.try_get::<_, Option<String>>(8).ok().flatten(),
key_is_expression,
}
})
.collect())
@@ -8432,6 +8448,15 @@ mod tests {
assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("string_to_array(ix.indkey::text, ' ')"));
}
#[test]
fn postgres_index_metadata_tracks_expression_key_provenance() {
// #6295 fix: each index key part's `a.attname IS NULL` tags whether it came from a real
// column or from pg_get_indexdef, so DDL generation never has to guess from the text.
assert!(POSTGRES_INDEXES_SQL.contains("array_agg(a.attname IS NULL ORDER BY k.n) AS key_is_expression"));
assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("SELECT a.attname IS NULL"));
assert!(POSTGRES_INDEXES_COMPAT_SQL.contains("AS key_is_expression"));
}
#[test]
fn postgres_owner_metadata_casts_relkind_to_text() {
assert!(POSTGRES_OWNERS_SQL.contains("c.relkind::text AS relkind"));
+1
View File
@@ -176,6 +176,7 @@ pub async fn list_indexes(pool: &Pool, _schema: &str, table: &str) -> Result<Vec
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
}
})
.collect())
+1
View File
@@ -179,6 +179,7 @@ pub async fn list_indexes(client: &RqliteClient, _schema: &str, table: &str) ->
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}
+1
View File
@@ -2272,6 +2272,7 @@ pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Res
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}
Ok(indexes)
+1
View File
@@ -2656,6 +2656,7 @@ pub async fn list_indexes(client: &mut SqlServerClient, schema: &str, table: &st
Some(inc_str.split(',').map(|s| s.to_string()).collect())
},
comment: row.get::<&str, _>(7).filter(|s: &&str| !s.is_empty()).map(|s: &str| s.to_string()),
key_is_expression: Vec::new(),
}
})
.collect())
+1
View File
@@ -221,6 +221,7 @@ pub async fn list_indexes(client: &TursoClient, _schema: &str, table: &str) -> R
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}