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
//! Django-shape `RunPython` — named Rust callbacks invoked during a
//! migration's apply / unapply walk. Issue #347.
//!
//! Where Django's `RunPython(forward_func, reverse_func)` takes Python
//! function references inside the migration file, rustango migration
//! files are JSON — they can't carry function pointers. Instead, the
//! callback is registered at startup via [`register_migration_callback!`]
//! and referenced by name in the JSON:
//!
//! ```json
//! {
//! "name": "0003_backfill_user_locale",
//! "forward": [
//! {"callback": {"name": "backfill_locale"}},
//! {"schema": ...}
//! ]
//! }
//! ```
//!
//! ## 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 inventory-collected; the lookup is `O(N)` over the global
//! registry but `N` is small (bounded by the number of declared
//! callbacks across the binary). Unknown names surface
//! [`MigrateError::Validation`] at apply time.
use Future;
use Pin;
use crateMigrateError;
use cratePool;
/// Future returned by a migration callback. `'static` because the
/// callback is stored as a `fn` pointer and may run after the caller
/// has returned.
pub type MigrationCallbackFut =
;
/// Function signature a migration callback implements. Takes an owned
/// `Pool` (cheap — internally `Arc<...>`) so the future can outlive
/// the surrounding stack frame.
pub type MigrationCallbackFn = fn ;
/// One callback registration. Inventory-collected; submit via the
/// [`register_migration_callback!`] macro.
collect!;
/// Look up a registered callback by name. Returns `None` when no
/// callback with that name was submitted to the inventory — the runner
/// surfaces this as 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);
/// ```