razeri_plugin_contract/evolution.rs
1// Copyright (C) 2026 Gabriel Shea
2// Licensed under either of the Apache License, Version 2.0 or the MIT
3// license, at your option.
4
5//! **Changing a table that already holds rows** — what this engine keeps
6//! through such a change, and the statement it takes to make one.
7//!
8//! A first deploy writes `CREATE`. Every deploy after it has rows to keep, and
9//! the two questions a dialect is the only one able to answer are:
10//!
11//! - **does the engine keep every value through this change?** A column
12//! declared to hold ten characters and now declared to hold forty holds
13//! everything it held on one engine and is rebuilt by a cast on another.
14//! [`EvolutionAnswers`] is where a dialect says which it is.
15//! - **what statement does the engine take?** Most write
16//! `ALTER TABLE … ADD COLUMN …`, some write `ALTER TABLE … ADD …`, and a
17//! change some engines make with a statement is not a statement other engines
18//! have at all. [`ColumnEvolution`] is the change, handed over to be spelled.
19//!
20//! # Both default to the careful answer
21//!
22//! An engine nobody has measured keeps nothing it has not been said to keep,
23//! and is given no statement Razeri cannot show it accepts. A dialect that
24//! answers nothing here still gets `ADD COLUMN`, which is the one spelling
25//! every engine Razeri targets takes, and gets no other statement at all.
26
27use crate::data_type::DataType;
28
29/// **What this engine keeps through a change to a table that already holds
30/// rows.**
31///
32/// Every answer defaults to `false`, which means *nothing has been measured*
33/// rather than *this engine loses data*. Razeri's classifier treats a change it
34/// cannot prove keeps every value as one that does not, so a silence here costs
35/// an operator a warning they can read and never costs them a row.
36///
37/// An answer only ever makes a verdict gentler. Razeri proves what it can from
38/// the types themselves first; these answers are asked afterwards, about the
39/// changes the engine's own spelling left unprovable.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
41#[non_exhaustive]
42pub struct EvolutionAnswers {
43 /// **This engine widens a string's declared length in place, keeping every
44 /// value.**
45 ///
46 /// A column declared `varchar(10)` and now declared `varchar(40)` holds a
47 /// strict superset of what it held, as a type. Whether the engine gets it
48 /// there without rewriting the column is the engine's own business, and
49 /// this is where it says so.
50 ///
51 /// The question is asked at all because a length is spelled differently on
52 /// every engine — `VARCHAR(40)` on one, `STRING(40)` on another,
53 /// `NVARCHAR(40)` on a third — and the length in a spelling Razeri does not
54 /// write is a length Razeri cannot read back. A `true` here is the dialect
55 /// saying *"that pair is a widening on my engine, and nothing is lost"*.
56 pub parameterized_string_widening_is_lossless: bool,
57
58 /// **This engine adds a field to a record a table already holds, keeping
59 /// every value.**
60 ///
61 /// The change is a record column that gained a field and changed in no
62 /// other way. Some engines take it as a change to the column's declared
63 /// shape and keep every row; others have no way to make it at all, and the
64 /// column has to be rebuilt.
65 ///
66 /// `false` is the careful answer, and it is the right one for every engine
67 /// that has no records to begin with.
68 pub nested_field_addition_is_lossless: bool,
69}
70
71impl EvolutionAnswers {
72 /// **Nothing measured** — the answer for an engine nobody has asked.
73 #[must_use]
74 pub const fn none() -> Self {
75 Self {
76 parameterized_string_widening_is_lossless: false,
77 nested_field_addition_is_lossless: false,
78 }
79 }
80
81 /// **This engine widens a string's declared length in place.** See
82 /// [`EvolutionAnswers::parameterized_string_widening_is_lossless`].
83 #[must_use]
84 pub const fn widening_a_string_in_place() -> Self {
85 Self {
86 parameterized_string_widening_is_lossless: true,
87 nested_field_addition_is_lossless: false,
88 }
89 }
90
91 /// The same answers with the nested-field one added, so an engine that
92 /// keeps both states both without a constructor per pair.
93 #[must_use]
94 pub const fn and_a_nested_field(self) -> Self {
95 Self { nested_field_addition_is_lossless: true, ..self }
96 }
97}
98
99/// **One change to a table that already holds rows**, handed to the dialect to
100/// spell.
101///
102/// The schema and table arrive unqualified and unquoted, because the dialect
103/// knows how it qualifies and quotes them. The type arrives as a
104/// [`DataType`] — Razeri's own, so the dialect renders it the way it renders
105/// every other column of that type, and the statement cannot disagree with the
106/// `CREATE` that would have built the same column.
107#[derive(Debug, Clone, Copy)]
108#[non_exhaustive]
109pub enum ColumnEvolution<'a> {
110 /// A column the table does not have yet, which accepts no value today.
111 AddColumn {
112 /// The table's schema, absent on engines that have none.
113 schema: Option<&'a str>,
114 /// The table.
115 table: &'a str,
116 /// The new column.
117 column: &'a str,
118 /// The type it is declared with.
119 data_type: &'a DataType,
120 },
121
122 /// A column whose declared type now holds more than it held.
123 WidenColumn {
124 /// The table's schema, absent on engines that have none.
125 schema: Option<&'a str>,
126 /// The table.
127 table: &'a str,
128 /// The column.
129 column: &'a str,
130 /// The type it is declared with now.
131 data_type: &'a DataType,
132 },
133
134 /// A record column that gained a field, and gained nothing else.
135 AddNestedField {
136 /// The table's schema, absent on engines that have none.
137 schema: Option<&'a str>,
138 /// The table.
139 table: &'a str,
140 /// The column whose record grew.
141 column: &'a str,
142 /// The path from the column to the new field, dotted through the
143 /// records that contain it — for a dialect whose engine names the field
144 /// rather than restating the column.
145 field: &'a str,
146 /// **The column's whole type, with the new field in it.** An engine
147 /// that makes this change by restating the column's shape needs all of
148 /// it; an engine that names the field alone reads `field` instead.
149 data_type: &'a DataType,
150 },
151}
152
153impl ColumnEvolution<'_> {
154 /// The table's schema, whichever change this is.
155 #[must_use]
156 pub const fn schema(&self) -> Option<&str> {
157 match self {
158 Self::AddColumn { schema, .. }
159 | Self::WidenColumn { schema, .. }
160 | Self::AddNestedField { schema, .. } => *schema,
161 }
162 }
163
164 /// The table, whichever change this is.
165 #[must_use]
166 pub const fn table(&self) -> &str {
167 match self {
168 Self::AddColumn { table, .. }
169 | Self::WidenColumn { table, .. }
170 | Self::AddNestedField { table, .. } => table,
171 }
172 }
173
174 /// The column, whichever change this is.
175 #[must_use]
176 pub const fn column(&self) -> &str {
177 match self {
178 Self::AddColumn { column, .. }
179 | Self::WidenColumn { column, .. }
180 | Self::AddNestedField { column, .. } => column,
181 }
182 }
183
184 /// The type the column is declared with after the change.
185 #[must_use]
186 pub const fn data_type(&self) -> &DataType {
187 match self {
188 Self::AddColumn { data_type, .. }
189 | Self::WidenColumn { data_type, .. }
190 | Self::AddNestedField { data_type, .. } => data_type,
191 }
192 }
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 #[test]
200 fn nothing_is_claimed_until_a_dialect_claims_it() {
201 let silent = EvolutionAnswers::default();
202 assert!(!silent.parameterized_string_widening_is_lossless);
203 assert!(!silent.nested_field_addition_is_lossless);
204 assert_eq!(silent, EvolutionAnswers::none());
205 }
206
207 #[test]
208 fn an_engine_that_keeps_both_states_both() {
209 let both = EvolutionAnswers::widening_a_string_in_place().and_a_nested_field();
210 assert!(both.parameterized_string_widening_is_lossless);
211 assert!(both.nested_field_addition_is_lossless);
212 }
213
214 #[test]
215 fn a_change_names_its_table_and_column_whichever_change_it_is() {
216 let data_type = DataType::boolean();
217 let change = ColumnEvolution::AddColumn {
218 schema: Some("telemetry_core"),
219 table: "core_clients_daily",
220 column: "is_default_browser",
221 data_type: &data_type,
222 };
223 assert_eq!(change.schema(), Some("telemetry_core"));
224 assert_eq!(change.table(), "core_clients_daily");
225 assert_eq!(change.column(), "is_default_browser");
226 }
227}