rust_query/migrate/
migration.rs

1use std::{
2    collections::{HashMap, HashSet},
3    convert::Infallible,
4    marker::PhantomData,
5    ops::Deref,
6};
7
8use sea_query::{Alias, IntoTableRef, TableDropStatement};
9
10use crate::{
11    IntoExpr, Lazy, Table, TableRow, Transaction,
12    alias::{Scope, TmpTable},
13    migrate::new_table_inner,
14    transaction::try_insert_private,
15};
16
17pub trait Migration {
18    type FromSchema: 'static;
19    type From: Table<Schema = Self::FromSchema>;
20    type To: Table<MigrateFrom = Self::From>;
21    type Conflict;
22
23    #[doc(hidden)]
24    fn prepare(
25        val: Self,
26        prev: crate::Expr<'static, Self::FromSchema, Self::From>,
27    ) -> <Self::To as Table>::Insert;
28    #[doc(hidden)]
29    fn map_conflict(val: TableRow<Self::From>) -> Self::Conflict;
30}
31
32/// Transaction type for use in migrations.
33pub struct TransactionMigrate<FromSchema> {
34    pub(super) inner: Transaction<FromSchema>,
35    pub(super) scope: Scope,
36    pub(super) rename_map: HashMap<&'static str, TmpTable>,
37    // creating non unique indices is delayed so that they don't need to be renamed
38    pub(super) extra_index: Vec<String>,
39}
40
41impl<FromSchema> Deref for TransactionMigrate<FromSchema> {
42    type Target = Transaction<FromSchema>;
43
44    fn deref(&self) -> &Self::Target {
45        &self.inner
46    }
47}
48
49impl<FromSchema: 'static> TransactionMigrate<FromSchema> {
50    fn new_table_name<T: Table>(&mut self) -> TmpTable {
51        *self.rename_map.entry(T::NAME).or_insert_with(|| {
52            let new_table_name = self.scope.tmp_table();
53            let table = crate::schema::from_macro::Table::new::<T>();
54            self.inner.execute(&new_table_inner(&table, new_table_name));
55            self.extra_index.extend(table.delayed_indices(T::NAME));
56            new_table_name
57        })
58    }
59
60    fn unmigrated<M: Migration<FromSchema = FromSchema>>(
61        &self,
62        new_name: TmpTable,
63    ) -> impl Iterator<Item = TableRow<M::From>> {
64        let data = self.inner.query(|rows| {
65            let old = rows.join_private::<M::From>();
66            rows.into_vec(old)
67        });
68
69        let migrated = Transaction::new().query(|rows| {
70            let new = rows.join_tmp::<M::To>(new_name);
71            rows.into_vec(new)
72        });
73        let migrated: HashSet<_> = migrated.into_iter().map(|x| x.inner.idx).collect();
74
75        data.into_iter()
76            .filter(move |row| !migrated.contains(&row.inner.idx))
77    }
78
79    /// Migrate some rows to the new schema.
80    ///
81    /// This will return an error when there is a conflict.
82    /// The error type depends on the number of unique constraints that the
83    /// migration can violate:
84    /// - 0 => [Infallible]
85    /// - 1.. => `TableRow<T::From>` (row in the old table that could not be migrated)
86    pub fn migrate_optional<'t, M: Migration<FromSchema = FromSchema>>(
87        &'t mut self,
88        mut f: impl FnMut(Lazy<'t, M::From>) -> Option<M>,
89    ) -> Result<(), M::Conflict> {
90        let new_name = self.new_table_name::<M::To>();
91
92        for row in self.unmigrated::<M>(new_name) {
93            if let Some(new) = f(self.lazy(row)) {
94                try_insert_private::<M::To>(
95                    new_name.into_table_ref(),
96                    Some(row.inner.idx),
97                    M::prepare(new, row.into_expr()),
98                )
99                .map_err(|_| M::map_conflict(row))?;
100            };
101        }
102        Ok(())
103    }
104
105    /// Migrate all rows to the new schema.
106    ///
107    /// Conflict errors work the same as in [Self::migrate_optional].
108    ///
109    /// However, this method will return [Migrated] when all rows are migrated.
110    /// This can then be used as proof that there will be no foreign key violations.
111    pub fn migrate<'t, M: Migration<FromSchema = FromSchema>>(
112        &'t mut self,
113        mut f: impl FnMut(Lazy<'t, M::From>) -> M,
114    ) -> Result<Migrated<'static, FromSchema, M::To>, M::Conflict> {
115        self.migrate_optional::<M>(|x| Some(f(x)))?;
116
117        Ok(Migrated {
118            _p: PhantomData,
119            f: Box::new(|_| {}),
120            _local: PhantomData,
121        })
122    }
123
124    /// Helper method for [Self::migrate].
125    ///
126    /// It can only be used when the migration is known to never cause unique constraint conflicts.
127    pub fn migrate_ok<'t, M: Migration<FromSchema = FromSchema, Conflict = Infallible>>(
128        &'t mut self,
129        f: impl FnMut(Lazy<'t, M::From>) -> M,
130    ) -> Migrated<'static, FromSchema, M::To> {
131        let Ok(res) = self.migrate(f);
132        res
133    }
134}
135
136/// [Migrated] provides a proof of migration.
137///
138/// This only needs to be provided for tables that are migrated from a previous table.
139pub struct Migrated<'t, FromSchema, T> {
140    _p: PhantomData<T>,
141    f: Box<dyn 't + FnOnce(&mut SchemaBuilder<'t, FromSchema>)>,
142    _local: PhantomData<*const ()>,
143}
144
145impl<'t, FromSchema: 'static, T: Table> Migrated<'t, FromSchema, T> {
146    /// Don't migrate the remaining rows.
147    ///
148    /// This can cause foreign key constraint violations, which is why an error callback needs to be provided.
149    pub fn map_fk_err(err: impl 't + FnOnce() -> Infallible) -> Self {
150        Self {
151            _p: PhantomData,
152            f: Box::new(|x| x.foreign_key::<T>(err)),
153            _local: PhantomData,
154        }
155    }
156
157    #[doc(hidden)]
158    pub fn apply(self, b: &mut SchemaBuilder<'t, FromSchema>) {
159        (self.f)(b)
160    }
161}
162
163pub struct SchemaBuilder<'t, FromSchema> {
164    pub(super) inner: TransactionMigrate<FromSchema>,
165    pub(super) drop: Vec<TableDropStatement>,
166    pub(super) foreign_key: HashMap<&'static str, Box<dyn 't + FnOnce() -> Infallible>>,
167}
168
169impl<'t, FromSchema: 'static> SchemaBuilder<'t, FromSchema> {
170    pub fn foreign_key<To: Table>(&mut self, err: impl 't + FnOnce() -> Infallible) {
171        self.inner.new_table_name::<To>();
172
173        self.foreign_key.insert(To::NAME, Box::new(err));
174    }
175
176    pub fn create_empty<To: Table>(&mut self) {
177        self.inner.new_table_name::<To>();
178    }
179
180    pub fn drop_table<T: Table>(&mut self) {
181        let name = Alias::new(T::NAME);
182        let step = sea_query::Table::drop().table(name).take();
183        self.drop.push(step);
184    }
185}