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
#![recursion_limit = "128"]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
use quote::quote;
use quote::ToTokens;
use refinery_migrations::{find_migrations_filenames, MigrationType};
use std::fs;
use std::path::Path;
use syn::{parse_macro_input, Ident, LitStr};
fn migration_fn_quoted<T: ToTokens>(_migrations: Vec<T>) -> TokenStream2 {
let result = quote! {
use refinery::{Migration, Runner};
pub fn runner() -> Runner {
let quoted_migrations: Vec<(&str, String)> = vec![#(#_migrations),*];
let mut migrations: Vec<Migration> = Vec::new();
for module in quoted_migrations.into_iter() {
migrations.push(Migration::from_filename(module.0, &module.1).unwrap());
}
Runner::new(&migrations)
}
};
result
}
#[proc_macro]
pub fn include_migration_mods(input: TokenStream) -> TokenStream {
let _location = if input.is_empty() {
None
} else {
let location: LitStr = parse_macro_input!(input);
Some(location.value())
};
let location = _location.as_ref().map(Path::new);
let migration_mod_names = find_migrations_filenames(location, MigrationType::Mod, false)
.expect("error getting migration files");
let mut migrations_mods = Vec::new();
let mut _migrations = Vec::new();
for migration in migration_mod_names.iter() {
log::debug!("including mod {}", migration);
let ident = Ident::new(migration, Span2::call_site());
let mig_mod = quote! {pub mod #ident;};
_migrations.push(quote! {(#migration, #ident::migration())});
migrations_mods.push(mig_mod);
}
let fnq = migration_fn_quoted(_migrations);
let result = quote! {
#(#migrations_mods)*
#fnq
};
result.into()
}
#[proc_macro]
pub fn embed_migrations(input: TokenStream) -> TokenStream {
let _location = if input.is_empty() {
None
} else {
let location: LitStr = parse_macro_input!(input);
Some(location.value())
};
let location = _location.as_ref().map(Path::new);
let migration_file_paths = find_migrations_filenames(location, MigrationType::Sql, true)
.expect("error getting migration files");
let mut _migrations = Vec::new();
for path in migration_file_paths.iter() {
let sql = fs::read_to_string(path)
.unwrap_or_else(|_| panic!("could not read migration {} content", path));
let filename = Path::new(path)
.file_stem()
.and_then(|file| file.to_os_string().into_string().ok())
.unwrap();
_migrations.push(quote! {(#filename, #sql.into())});
}
let fnq = migration_fn_quoted(_migrations);
(quote! {
pub mod migrations {
#fnq
}
})
.into()
}
#[cfg(test)]
mod tests {
use super::{migration_fn_quoted, quote};
#[test]
fn test_quote_fn() {
let migs = vec![quote!("V1__first", "valid_sql_file")];
let expected = concat! {
"use refinery :: { Migration , Runner } ; ",
"pub fn runner ( ) -> Runner { ",
"let quoted_migrations : Vec < ( & str , String ) > = vec ! [ \"V1__first\" , \"valid_sql_file\" ] ; ",
"let mut migrations : Vec < Migration > = Vec :: new ( ) ; ",
"for module in quoted_migrations . into_iter ( ) { ",
"migrations . push ( Migration :: from_filename ( module . 0 , & module . 1 ) . unwrap ( ) ) ; ",
"} ",
"Runner :: new ( & migrations ) }"
};
assert_eq!(expected, migration_fn_quoted(migs).to_string());
}
}