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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#[cfg(any(
feature = "mysql",
feature = "postgres",
feature = "tokio-postgres",
feature = "mysql_async"
))]
use crate::config::build_db_url;
use crate::config::{Config, ConfigDbType};
use crate::error::WrapMigrationError;
use crate::traits::r#async::{AsyncQuery, AsyncTransaction};
use crate::traits::sync::{Query, Transaction};
use crate::traits::{GET_APPLIED_MIGRATIONS_QUERY, GET_LAST_APPLIED_MIGRATION_QUERY};
use crate::{Error, Migration, Report, Target};
use async_trait::async_trait;
use std::convert::Infallible;
impl Transaction for Config {
type Error = Infallible;
fn execute(&mut self, _queries: &[&str]) -> Result<usize, Self::Error> {
Ok(0)
}
}
impl Query<Vec<Migration>> for Config {
fn query(&mut self, _query: &str) -> Result<Vec<Migration>, Self::Error> {
Ok(Vec::new())
}
}
#[async_trait]
impl AsyncTransaction for Config {
type Error = Infallible;
async fn execute(&mut self, _queries: &[&str]) -> Result<usize, Self::Error> {
Ok(0)
}
}
#[async_trait]
impl AsyncQuery<Vec<Migration>> for Config {
async fn query(
&mut self,
_query: &str,
) -> Result<Vec<Migration>, <Self as AsyncTransaction>::Error> {
Ok(Vec::new())
}
}
#[cfg(any(feature = "mysql", feature = "postgres", feature = "rusqlite"))]
macro_rules! with_connection {
($config:ident, $op: expr) => {
match $config.db_type() {
ConfigDbType::Mysql => {
cfg_if::cfg_if! {
if #[cfg(feature = "mysql")] {
let url = build_db_url("mysql", &$config);
let conn = mysql::Conn::new(&url).migration_err("could not connect to database", None)?;
$op(conn)
} else {
panic!("tried to migrate from config for a mysql database, but feature mysql not enabled!");
}
}
}
ConfigDbType::Sqlite => {
cfg_if::cfg_if! {
if #[cfg(feature = "rusqlite")] {
let path = $config.db_path().map(|p| p.to_path_buf()).unwrap_or_default();
let conn = rusqlite::Connection::open_with_flags(path, rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE).migration_err("could not open database", None)?;
$op(conn)
} else {
panic!("tried to migrate from config for a sqlite database, but feature rusqlite not enabled!");
}
}
}
ConfigDbType::Postgres => {
cfg_if::cfg_if! {
if #[cfg(feature = "postgres")] {
let path = build_db_url("postgresql", &$config);
let conn = postgres::Client::connect(path.as_str(), postgres::NoTls).migration_err("could not connect to database", None)?;
$op(conn)
} else {
panic!("tried to migrate from config for a postgresql database, but feature postgres not enabled!");
}
}
}
};
}
}
#[cfg(any(feature = "tokio-postgres", feature = "mysql_async"))]
macro_rules! with_connection_async {
($config: ident, $op: expr) => {
match $config.db_type() {
ConfigDbType::Mysql => {
cfg_if::cfg_if! {
if #[cfg(feature = "mysql_async")] {
let url = build_db_url("mysql", $config);
let pool = mysql_async::Pool::from_url(&url).migration_err("could not connect to the database", None)?;
$op(pool).await
} else {
panic!("tried to migrate async from config for a mysql database, but feature mysql_async not enabled!");
}
}
}
ConfigDbType::Sqlite => {
panic!("tried to migrate async from config for a sqlite database, but this feature is not implemented yet");
}
ConfigDbType::Postgres => {
cfg_if::cfg_if! {
if #[cfg(all(feature = "tokio-postgres", feature = "tokio"))] {
let path = build_db_url("postgresql", $config);
let (client, connection ) = tokio_postgres::connect(path.as_str(), tokio_postgres::NoTls).await.migration_err("could not connect to database", None)?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
$op(client).await
} else {
panic!("tried to migrate async from config for a postgresql database, but either tokio or tokio-postgres was not enabled!");
}
}
}
}
}
}
#[cfg(any(feature = "mysql", feature = "postgres", feature = "rusqlite"))]
impl crate::Migrate for Config {
fn get_last_applied_migration(&mut self) -> Result<Option<Migration>, Error> {
with_connection!(self, |mut conn| {
let mut migrations: Vec<Migration> =
Query::query(&mut conn, GET_LAST_APPLIED_MIGRATION_QUERY)
.migration_err("error getting last applied migration", None)?;
Ok(migrations.pop())
})
}
fn get_applied_migrations(&mut self) -> Result<Vec<Migration>, Error> {
with_connection!(self, |mut conn| {
let migrations: Vec<Migration> = Query::query(&mut conn, GET_APPLIED_MIGRATIONS_QUERY)
.migration_err("error getting applied migrations", None)?;
Ok(migrations)
})
}
fn migrate(
&mut self,
migrations: &[Migration],
abort_divergent: bool,
abort_missing: bool,
grouped: bool,
target: Target,
) -> Result<Report, Error> {
with_connection!(self, |mut conn| {
crate::Migrate::migrate(
&mut conn,
migrations,
abort_divergent,
abort_missing,
grouped,
target,
)
})
}
}
#[cfg(any(feature = "mysql_async", feature = "tokio-postgres",))]
#[async_trait]
impl crate::AsyncMigrate for Config {
async fn get_last_applied_migration(&mut self) -> Result<Option<Migration>, Error> {
with_connection_async!(self, move |mut conn| async move {
let mut migrations: Vec<Migration> =
AsyncQuery::query(&mut conn, GET_LAST_APPLIED_MIGRATION_QUERY)
.await
.migration_err("error getting last applied migration", None)?;
Ok(migrations.pop())
})
}
async fn get_applied_migrations(&mut self) -> Result<Vec<Migration>, Error> {
with_connection_async!(self, move |mut conn| async move {
let migrations: Vec<Migration> =
AsyncQuery::query(&mut conn, GET_APPLIED_MIGRATIONS_QUERY)
.await
.migration_err("error getting last applied migration", None)?;
Ok(migrations)
})
}
async fn migrate(
&mut self,
migrations: &[Migration],
abort_divergent: bool,
abort_missing: bool,
grouped: bool,
target: Target,
) -> Result<Report, Error> {
with_connection_async!(self, move |mut conn| async move {
crate::AsyncMigrate::migrate(
&mut conn,
migrations,
abort_divergent,
abort_missing,
grouped,
target,
)
.await
})
}
}