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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/// Macro for vector of [`Box`]
#[macro_export]
macro_rules! vec_box {
    ($elem:expr; $n:expr) => (vec![Box::new($elem); $n]);
    ($($x:expr),*) => (vec![$(Box::new($x)),*]);
    ($($x:expr,)*) => (vec![$(Box::new($x)),*]);
    ($($x:expr,)*) => (sqlx_migrator::vec_box![$($x),*]);
}

/// Macro for implementing the `Migration` trait for the provided database.
///
/// This macro will use current file name as name for migration
///
/// This macro expects the following arguments:
/// - `$db:ty`: the type of database
/// - `$state:ty`: the type of state for migration (optional). If not present
///   this will by default set as `()`
/// - `$op:ty`: The type for which the migration is being implemented
/// - `$app_name:expr`: Name of app to be used for app variable
/// - `$parents:expr`: List of parents migration.
/// - `$operations:expr`: List of operations
#[macro_export]
macro_rules! migration {
    ($db:ty, $state:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        #[async_trait::async_trait]
        impl sqlx_migrator::migration::Migration<$db, $state> for $op {
            fn app(&self) -> &str {
                $app_name
            }

            fn name(&self) -> &str {
                std::path::Path::new(file!())
                    .file_stem()
                    .map(|stem_os_str| stem_os_str.to_str().unwrap_or_default())
                    .unwrap_or_default()
            }

            fn parents(&self) -> Vec<Box<dyn sqlx_migrator::migration::Migration<$db, $state>>> {
                $parents
            }

            fn operations(&self) -> Vec<Box<dyn sqlx_migrator::operation::Operation<$db, $state>>> {
                $operations
            }
        }
    };
    ($db:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::migration!($db, (), $op, $app_name, $parents, $operations);
    };
}

/// Macro for implementing the [`migration`] macro for the `Any`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// `sqlx::Any`
#[macro_export]
#[cfg(all(
    any(feature = "postgres", feature = "mysql", feature = "sqlite"),
    feature = "any"
))]
macro_rules! any_migration {
    ($state:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::migration!(
            sqlx_migrator::sqlx::Any,
            $state,
            $op,
            $app_name,
            $parents,
            $operations
        );
    };
    ($op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::any_migration!((), $op, $app_name, $parents, $operations);
    };
}

/// Macro for implementing the [`migration`] macro for the `MySql`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// `sqlx::MySql`
#[macro_export]
#[cfg(feature = "mysql")]
macro_rules! mysql_migration {
    ($state:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::migration!(
            sqlx_migrator::sqlx::MySql,
            $state,
            $op,
            $app_name,
            $parents,
            $operations
        );
    };
    ($op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::mysql_migration!((), $op, $app_name, $parents, $operations);
    };
}

/// Macro for implementing the [`migration`] macro for the `Postgres`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// `sqlx::Postgres`
#[macro_export]
#[cfg(feature = "postgres")]
macro_rules! postgres_migration {
    ($state:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::migration!(
            sqlx_migrator::sqlx::Postgres,
            $state,
            $op,
            $app_name,
            $parents,
            $operations
        );
    };
    ($op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::postgres_migration!((), $op, $app_name, $parents, $operations);
    };
}

/// Macro for implementing the [`migration`] macro for the `Sqlite`.
///
/// This macro calls [`migration`] macro with db value already set as
/// `sqlx::Sqlite`
#[macro_export]
#[cfg(feature = "sqlite")]
macro_rules! sqlite_migration {
    ($state:ty, $op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::migration!(
            sqlx_migrator::sqlx::Sqlite,
            $state,
            $op,
            $app_name,
            $parents,
            $operations
        );
    };
    ($op:ty, $app_name:expr, $parents:expr, $operations:expr) => {
        sqlx_migrator::sqlite_migration!((), $op, $app_name, $parents, $operations);
    };
}