|
1 | | -//! Data migration declaration and representation utilities |
2 | | -
|
3 | 1 | use diesel::SqliteConnection; |
4 | | -use diesel::migration::MigrationVersion; |
5 | 2 |
|
6 | 3 | use crate::DatabaseError; |
7 | 4 |
|
8 | | -/// A data migration to apply post schema migration. |
9 | | -pub(crate) struct DataMigration { |
10 | | - /// Identifier of the migration which matches the directory name containing the up.sql/down.sql |
11 | | - /// files |
| 5 | +pub(crate) struct Migration { |
12 | 6 | pub(crate) id: &'static str, |
13 | | - /// Closure to execute given a connection |
14 | | - pub(crate) run: fn( |
15 | | - &mut SqliteConnection, |
16 | | - &MigrationVersion<'static>, |
17 | | - ) -> std::result::Result<(), DatabaseError>, |
| 7 | + pub(crate) sql: &'static str, |
| 8 | + pub(crate) data_migration: fn(&mut SqliteConnection) -> Result<(), DatabaseError>, |
18 | 9 | } |
19 | 10 |
|
20 | | -impl std::fmt::Debug for DataMigration { |
| 11 | +impl std::fmt::Debug for Migration { |
21 | 12 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
22 | 13 | std::fmt::Debug::fmt(self.id, f) |
23 | 14 | } |
24 | 15 | } |
25 | | - |
26 | | -// Helper macro to resolve an optional explicit run identifier or default to `<name>::run`. |
27 | | -#[macro_export] |
28 | | -macro_rules! __data_migrations_resolve_run { |
29 | | - ($name:ident, $run:ident) => { |
30 | | - $name::$run |
31 | | - }; |
32 | | - ($name:ident) => { |
33 | | - $name::run |
34 | | - }; |
35 | | -} |
36 | | - |
37 | | -#[macro_export] |
38 | | -macro_rules! __data_migrations_mod { |
39 | | - ($module:ident, $path:literal) => { |
40 | | - // It'd be great if we could use `$(concat!($path, "/mod.rs"))` |
41 | | - // but it's already required at module traversal, so it _must be_ |
42 | | - // a true literal string `"somewhere/mod.rs"`. |
43 | | - #[path = $path] |
44 | | - mod $module; |
45 | | - }; |
46 | | -} |
47 | | - |
48 | | -/// Helper macro to both declare the `fn run` (default), declare the module, and keeps it in a slice |
49 | | -/// for later usage |
50 | | -/// |
51 | | -/// Example: |
52 | | -/// |
53 | | -/// ```ignore |
54 | | -/// post_migrations! { |
55 | | -/// const DATA_MIGRATION_CODE: &[DataMigration] = { |
56 | | -/// "20260223160000_add_block_header_commitment/mod.rs" => alt_name_add_commitment, |
57 | | -/// "20260223160001_bar/mod.rs" => bar::fun, |
58 | | -/// }; |
59 | | -/// } |
60 | | -/// ``` |
61 | | -/// |
62 | | -/// expands to |
63 | | -/// |
64 | | -///```ignore |
65 | | -/// mod alt_name_add_commitment { include!{ "20260223160000_add_block_header_commitment/mod.rs" } } |
66 | | -/// mod bar { include!{ "20260223160001_bar/mod.rs" } } |
67 | | -/// |
68 | | -/// const DATA_MIGRATION_CODE: &[DataMigration] = { |
69 | | -/// PostMigration { id: "20260223160000_add_block_header_commitment", add_block_header::run }, |
70 | | -/// PostMigration { id: "20260223160001_bar", bar::fun }, |
71 | | -/// }; |
72 | | -/// ``` |
73 | | -#[macro_export] |
74 | | -macro_rules! data_migrations { |
75 | | - (const $binding:ident : &[$what:ty] = {$($path:literal => $module:ident $(:: $run:ident)?),* $(,)?};) => { |
76 | | - $( |
77 | | - $crate::__data_migrations_mod!{$module, $path} |
78 | | - )* |
79 | | - static $binding: ::std::sync::LazyLock<Box<[$what]>> = |
80 | | - ::std::sync::LazyLock::new(|| { |
81 | | - Box::new([ |
82 | | - $( |
83 | | - $crate::db::migrations::data::DataMigration { |
84 | | - id: $path.split_once('/').expect("Has one path segment at least").0, |
85 | | - run: $crate::__data_migrations_resolve_run!($module $(, $run)?), |
86 | | - }, |
87 | | - )* |
88 | | - ]) |
89 | | - }); |
90 | | - }; |
91 | | -} |
0 commit comments