|
| 1 | +//! `fill_with` UPDATE emission for `ModifyColumnType`. |
| 2 | +//! |
| 3 | +//! `fill_with` maps a removed enum label to the surviving label that replaces |
| 4 | +//! it. Both sides of the mapping are **bare** labels ??no SQL quoting ??because |
| 5 | +//! [`Expr::val`] binds them as data values and the query builder adds exactly |
| 6 | +//! one layer of quoting itself. |
| 7 | +
|
| 8 | +use std::collections::BTreeMap; |
| 9 | +use std::sync::Once; |
| 10 | + |
| 11 | +use sea_query::{Alias, Expr, ExprTrait, Query}; |
| 12 | + |
| 13 | +use crate::sql::types::BuiltQuery; |
| 14 | + |
| 15 | +/// Emitted at most once per process by [`strip_legacy_outer_quotes`]. |
| 16 | +static LEGACY_QUOTE_WARNING: Once = Once::new(); |
| 17 | + |
| 18 | +#[expect( |
| 19 | + clippy::print_stderr, |
| 20 | + reason = "one-time deprecation notice for legacy pre-quoted fill_with values; stderr keeps the emitted SQL on stdout intact" |
| 21 | +)] |
| 22 | +fn warn_legacy_quoted_replacement(column: &str, removed: &str, replacement: &str, bare: &str) { |
| 23 | + LEGACY_QUOTE_WARNING.call_once(|| { |
| 24 | + eprintln!( |
| 25 | + "vespertide: warning: modify_column_type.fill_with replacement \ |
| 26 | + {replacement} for {column}.{removed} is wrapped in SQL single \ |
| 27 | + quotes. fill_with values are bare enum labels; the quotes were \ |
| 28 | + stripped for compatibility. Rewrite the migration to use {bare}." |
| 29 | + ); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +/// Backward compatibility for migration files written against the older schema |
| 34 | +/// documentation, which showed the replacement already wrapped in SQL single |
| 35 | +/// quotes (`{"cancelled": "'pending'"}`). |
| 36 | +/// |
| 37 | +/// Since [`Expr::val`] binds the replacement as a *data value*, a pre-quoted |
| 38 | +/// label is escaped into `'''pending'''`, whose content is the 9-character |
| 39 | +/// token `'pending'` rather than the 7-character label `pending`. `PostgreSQL` |
| 40 | +/// then rejects the UPDATE with `invalid input value for enum`. |
| 41 | +/// |
| 42 | +/// When the value both starts and ends with a single quote, exactly one outer |
| 43 | +/// layer is stripped and a one-time warning is emitted. Everything else is |
| 44 | +/// passed through untouched. |
| 45 | +fn strip_legacy_outer_quotes<'a>(column: &str, removed: &str, replacement: &'a str) -> &'a str { |
| 46 | + let Some(bare) = replacement |
| 47 | + .strip_prefix('\'') |
| 48 | + .and_then(|inner| inner.strip_suffix('\'')) |
| 49 | + else { |
| 50 | + return replacement; |
| 51 | + }; |
| 52 | + |
| 53 | + warn_legacy_quoted_replacement(column, removed, replacement, bare); |
| 54 | + bare |
| 55 | +} |
| 56 | + |
| 57 | +/// Build UPDATE statements for `fill_with` mappings (removed enum values ??replacement values). |
| 58 | +/// Each entry generates: UPDATE "table" SET "column" = 'replacement' WHERE "column" = '`removed_value`' |
| 59 | +/// |
| 60 | +/// Iteration follows the `BTreeMap` key order, so the emitted statements are |
| 61 | +/// deterministic across runs and platforms. |
| 62 | +fn build_fill_with_updates( |
| 63 | + table: &str, |
| 64 | + column: &str, |
| 65 | + fill_with: &BTreeMap<String, String>, |
| 66 | +) -> Vec<BuiltQuery> { |
| 67 | + fill_with |
| 68 | + .iter() |
| 69 | + .map(|(removed_value, replacement)| { |
| 70 | + let replacement = strip_legacy_outer_quotes(column, removed_value, replacement); |
| 71 | + let update_stmt = Query::update() |
| 72 | + .table(Alias::new(table)) |
| 73 | + .value(Alias::new(column), Expr::val(replacement)) |
| 74 | + .and_where(Expr::col(Alias::new(column)).eq(removed_value.as_str())) |
| 75 | + .to_owned(); |
| 76 | + BuiltQuery::Update(Box::new(update_stmt)) |
| 77 | + }) |
| 78 | + .collect() |
| 79 | +} |
| 80 | + |
| 81 | +/// Conditionally prepend `fill_with` UPDATEs to `queries`. |
| 82 | +/// |
| 83 | +/// Centralises the byte-identical |
| 84 | +/// `if let Some(fw) = fill_with { queries.extend(build_fill_with_updates(...)); }` |
| 85 | +/// dance that the three `modify_column_type` paths each previously |
| 86 | +/// open-coded (`direct::build_postgres_enum_migration`, |
| 87 | +/// `direct::build_standard_type_modification`, and |
| 88 | +/// `sqlite_rebuild::build_modify_column_type_sqlite_temp_table`). Each |
| 89 | +/// callsite now collapses to a single line whose name reads |
| 90 | +/// "if a fill_with map exists, prepend its UPDATEs". |
| 91 | +pub(super) fn extend_fill_with_updates( |
| 92 | + queries: &mut Vec<BuiltQuery>, |
| 93 | + table: &str, |
| 94 | + column: &str, |
| 95 | + fill_with: Option<&BTreeMap<String, String>>, |
| 96 | +) { |
| 97 | + if let Some(fw) = fill_with { |
| 98 | + queries.extend(build_fill_with_updates(table, column, fw)); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + use crate::sql::DatabaseBackend; |
| 106 | + use crate::test_support::{backend_tag, joined_sql_semicolon}; |
| 107 | + use insta::{assert_snapshot, with_settings}; |
| 108 | + use rstest::rstest; |
| 109 | + |
| 110 | + fn updates(fill_with: &BTreeMap<String, String>, backend: DatabaseBackend) -> String { |
| 111 | + let mut queries = Vec::new(); |
| 112 | + extend_fill_with_updates(&mut queries, "plan", "sheet_policy", Some(fill_with)); |
| 113 | + joined_sql_semicolon(backend, &queries) |
| 114 | + } |
| 115 | + |
| 116 | + fn map(pairs: &[(&str, &str)]) -> BTreeMap<String, String> { |
| 117 | + pairs |
| 118 | + .iter() |
| 119 | + .map(|(k, v)| ((*k).to_string(), (*v).to_string())) |
| 120 | + .collect() |
| 121 | + } |
| 122 | + |
| 123 | + /// A bare enum label gets exactly one layer of SQL quoting from the query |
| 124 | + /// builder ??the enum label reaches the column intact. |
| 125 | + #[rstest] |
| 126 | + #[case::postgres(DatabaseBackend::Postgres)] |
| 127 | + #[case::mysql(DatabaseBackend::MySql)] |
| 128 | + #[case::sqlite(DatabaseBackend::Sqlite)] |
| 129 | + fn bare_replacement_gets_exactly_one_quote_layer(#[case] backend: DatabaseBackend) { |
| 130 | + let sql = updates(&map(&[("OVER_500", "FIXED")]), backend); |
| 131 | + |
| 132 | + assert!( |
| 133 | + sql.contains("= 'FIXED'"), |
| 134 | + "expected a single quote layer around FIXED, got: {sql}" |
| 135 | + ); |
| 136 | + assert!( |
| 137 | + !sql.contains("'''FIXED'''"), |
| 138 | + "replacement must not be double-quoted, got: {sql}" |
| 139 | + ); |
| 140 | + |
| 141 | + with_settings!({ snapshot_path => "../snapshots", snapshot_suffix => format!("fill_with_bare_replacement_{}", backend_tag(backend)) }, { |
| 142 | + assert_snapshot!(sql); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + /// Compatibility: a legacy pre-quoted replacement produces the SAME SQL as |
| 147 | + /// the bare form, so migrations written against the old documentation keep |
| 148 | + /// working. |
| 149 | + #[rstest] |
| 150 | + #[case::postgres(DatabaseBackend::Postgres)] |
| 151 | + #[case::mysql(DatabaseBackend::MySql)] |
| 152 | + #[case::sqlite(DatabaseBackend::Sqlite)] |
| 153 | + fn quoted_replacement_matches_bare_replacement(#[case] backend: DatabaseBackend) { |
| 154 | + let bare = updates(&map(&[("OVER_500", "FIXED")]), backend); |
| 155 | + let quoted = updates(&map(&[("OVER_500", "'FIXED'")]), backend); |
| 156 | + |
| 157 | + assert_eq!(quoted, bare); |
| 158 | + } |
| 159 | + |
| 160 | + /// Only the outer layer is stripped: a doubly-wrapped value keeps its inner |
| 161 | + /// quotes, and a value with a stray quote on one side is left alone. |
| 162 | + #[rstest] |
| 163 | + #[case::double_wrapped("''FIXED''", "'FIXED'")] |
| 164 | + #[case::leading_quote_only("'FIXED", "'FIXED")] |
| 165 | + #[case::trailing_quote_only("FIXED'", "FIXED'")] |
| 166 | + #[case::lone_quote("'", "'")] |
| 167 | + #[case::empty_quotes("''", "")] |
| 168 | + #[case::bare("FIXED", "FIXED")] |
| 169 | + fn strip_legacy_outer_quotes_removes_at_most_one_layer( |
| 170 | + #[case] input: &str, |
| 171 | + #[case] expected: &str, |
| 172 | + ) { |
| 173 | + assert_eq!( |
| 174 | + strip_legacy_outer_quotes("sheet_policy", "OVER_500", input), |
| 175 | + expected |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + /// `fill_with` is a `BTreeMap`, so multiple mappings emit in sorted key |
| 180 | + /// order regardless of insertion order. |
| 181 | + #[rstest] |
| 182 | + #[case::postgres(DatabaseBackend::Postgres)] |
| 183 | + #[case::mysql(DatabaseBackend::MySql)] |
| 184 | + #[case::sqlite(DatabaseBackend::Sqlite)] |
| 185 | + fn multiple_mappings_are_deterministically_ordered(#[case] backend: DatabaseBackend) { |
| 186 | + let ascending = map(&[ |
| 187 | + ("OVER_500", "FIXED"), |
| 188 | + ("PER_SHEET", "NEGOTIATION"), |
| 189 | + ("UNDER_100", "FIXED"), |
| 190 | + ]); |
| 191 | + let descending = map(&[ |
| 192 | + ("UNDER_100", "FIXED"), |
| 193 | + ("PER_SHEET", "NEGOTIATION"), |
| 194 | + ("OVER_500", "FIXED"), |
| 195 | + ]); |
| 196 | + let sql = updates(&ascending, backend); |
| 197 | + |
| 198 | + assert_eq!(updates(&descending, backend), sql); |
| 199 | + |
| 200 | + with_settings!({ snapshot_path => "../snapshots", snapshot_suffix => format!("fill_with_multiple_mappings_{}", backend_tag(backend)) }, { |
| 201 | + assert_snapshot!(sql); |
| 202 | + }); |
| 203 | + } |
| 204 | + |
| 205 | + /// `None` contributes no statements. |
| 206 | + #[test] |
| 207 | + fn absent_fill_with_emits_nothing() { |
| 208 | + let mut queries = Vec::new(); |
| 209 | + extend_fill_with_updates(&mut queries, "plan", "sheet_policy", None); |
| 210 | + assert!(queries.is_empty()); |
| 211 | + } |
| 212 | +} |
0 commit comments