1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Named Rust callbacks run during a migration's apply or unapply
//! walk, for data changes that DDL alone cannot express.
//!
//! Migration files are JSON, so they cannot hold function pointers.
//! Register the callback at startup with
//! [`register_migration_callback!`] and refer to it by name:
//!
//! ```json
//! {
//! "name": "0003_backfill_user_locale",
//! "atomic": false,
//! "forward": [
//! {"schema": ...},
//! {"callback": {"name": "backfill_locale"}}
//! ]
//! }
//! ```
//!
//! `"atomic": false` is **required**; the loader refuses a callback
//! without it. The callback gets a `Pool`, not the migration's
//! transaction, so inside one it waits on that transaction's locks —
//! forever on PostgreSQL (#1626).
//!
//! Non-atomic means a failed callback does not roll back the schema op
//! before it. To keep that rollback, put the schema op in its own
//! (atomic) migration and the callback alone in the next one.
//!
//! ## Quick start
//!
//! ```ignore
//! use rustango::migrate::callbacks::{register_migration_callback, MigrationCallbackFut};
//! use rustango::sql::Pool;
//! use std::pin::Pin;
//!
//! fn backfill_locale(pool: Pool) -> MigrationCallbackFut {
//! Box::pin(async move {
//! rustango::sql::raw_execute_pool(
//! &pool,
//! r#"UPDATE "user" SET "locale" = 'en' WHERE "locale" IS NULL"#,
//! Vec::new(),
//! ).await.map_err(|e| rustango::migrate::MigrateError::Validation(e.to_string()))
//! })
//! }
//!
//! rustango::register_migration_callback!("backfill_locale", backfill_locale);
//! ```
//!
//! Names are collected by `inventory`. Lookup scans the registry, but
//! it only holds the callbacks declared in the binary. An unknown name
//! raises [`MigrateError::Validation`] at apply time.
//!
//! [`register_migration_callback!`]: crate::register_migration_callback
use Future;
use Pin;
use crateMigrateError;
use cratePool;
/// Future returned by a migration callback. `'static` because the
/// callback may run after the caller has returned.
pub type MigrationCallbackFut =
;
/// Signature a migration callback implements. The `Pool` is owned but
/// cheap to clone, so the future can outlive the calling frame.
pub type MigrationCallbackFn = fn ;
/// One callback registration. Submit it with the
/// [`register_migration_callback!`](crate::register_migration_callback)
/// macro.
collect!;
/// Look up a registered callback by name. `None` means nothing
/// registered under that name; the runner turns that into a validation
/// error at apply time.
/// Register a named migration callback. Pair the chosen name with a
/// `{"callback": {"name": "..."}}` entry in your migration JSON's
/// `forward` array.
///
/// ```ignore
/// fn my_backfill(pool: rustango::sql::Pool) -> rustango::migrate::callbacks::MigrationCallbackFut {
/// Box::pin(async move {
/// // do the work
/// Ok(())
/// })
/// }
/// rustango::register_migration_callback!("my_backfill", my_backfill);
/// ```