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
use crate::DatabaseType;
use proc_macro2::{Ident, Span};
use quote::quote;
use std::{fs, path::Path};
use walkdir::WalkDir;
pub fn generate(
migrations_dir: impl AsRef<Path>,
module_path: impl AsRef<Path>,
db_type: DatabaseType,
) {
cargo_rerun(migrations_dir.as_ref());
let modules = super::migration_modules(migrations_dir.as_ref());
let migrations = super::migrations(db_type, migrations_dir.as_ref());
if let Some(p) = module_path.as_ref().parent() {
fs::create_dir_all(p).unwrap();
}
let db_ident = Ident::new(db_type.sqlx_type(), Span::call_site());
fs::write(
module_path,
quote! {
pub use sqlx_migrate::prelude::*;
#modules
pub fn migrations() -> impl IntoIterator<Item = Migration<sqlx::#db_ident>> {
#migrations
}
}
.to_string(),
)
.unwrap();
}
fn cargo_rerun(dir: &Path) {
for entry in WalkDir::new(dir) {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
println!("cargo:rerun-if-changed={}", entry.path().to_string_lossy());
}
}