1- use proc_macro2;
2- use syn;
3-
4- use migrations:: migration_directory_from_given_path;
1+ use crate :: migrations:: migration_directory_from_given_path;
52use migrations_internals:: { migration_paths_in_directory, version_from_path} ;
3+ use quote:: quote;
64use std:: error:: Error ;
75use std:: fs:: DirEntry ;
86use std:: path:: Path ;
97
10- use util:: { get_option, get_options_from_input} ;
11-
12- pub fn derive_embed_migrations ( input : & syn:: DeriveInput ) -> proc_macro2:: TokenStream {
13- fn bug ( ) -> ! {
14- panic ! (
15- "This is a bug. Please open a Github issue \
16- with your invocation of `embed_migrations!"
17- ) ;
18- }
19-
20- let options =
21- get_options_from_input ( & parse_quote ! ( embed_migrations_options) , & input. attrs , bug) ;
22- let migrations_path_opt = options
23- . as_ref ( )
24- . map ( |o| get_option ( o, "migrations_path" , bug) ) ;
8+ pub fn expand ( path : String ) -> proc_macro2:: TokenStream {
9+ dbg ! ( & path) ;
10+ let migrations_path_opt = if path. is_empty ( ) {
11+ None
12+ } else {
13+ Some ( path. replace ( "\" " , "" ) )
14+ } ;
2515 let migrations_expr =
2616 migration_directory_from_given_path ( migrations_path_opt. as_ref ( ) . map ( String :: as_str) )
2717 . and_then ( |path| migration_literals_from_path ( & path) ) ;
@@ -30,54 +20,48 @@ pub fn derive_embed_migrations(input: &syn::DeriveInput) -> proc_macro2::TokenSt
3020 Err ( e) => panic ! ( "Error reading migrations: {}" , e) ,
3121 } ;
3222
33- // These are split into multiple `quote!` calls to avoid recursion limit
34- let embedded_migration_def = quote ! (
35- struct EmbeddedMigration {
36- version: & ' static str ,
37- up_sql: & ' static str ,
38- }
23+ quote ! {
24+ #[ allow( dead_code) ]
25+ mod embedded_migrations {
26+ extern crate diesel;
27+ extern crate diesel_migrations;
3928
40- impl Migration for EmbeddedMigration {
41- fn version( & self ) -> & str {
42- self . version
43- }
29+ use self :: diesel_migrations:: * ;
30+ use self :: diesel:: connection:: SimpleConnection ;
31+ use std:: io;
4432
45- fn run( & self , conn: & SimpleConnection ) -> Result <( ) , RunMigrationsError > {
46- conn. batch_execute( self . up_sql) . map_err( Into :: into)
47- }
33+ const ALL_MIGRATIONS : & [ & Migration ] = & [ #( #migrations_expr) , * ] ;
4834
49- fn revert( & self , _conn: & SimpleConnection ) -> Result <( ) , RunMigrationsError > {
50- unreachable!( )
35+ struct EmbeddedMigration {
36+ version: & ' static str ,
37+ up_sql: & ' static str ,
5138 }
52- }
53- ) ;
5439
55- let run_fns = quote ! (
56- pub fn run<C : MigrationConnection >( conn: & C ) -> Result <( ) , RunMigrationsError > {
57- run_with_output( conn, & mut io:: sink( ) )
58- }
59-
60- pub fn run_with_output<C : MigrationConnection >(
61- conn: & C ,
62- out: & mut io:: Write ,
63- ) -> Result <( ) , RunMigrationsError > {
64- run_migrations( conn, ALL_MIGRATIONS . iter( ) . map( |v| * v) , out)
65- }
66- ) ;
67-
68- quote ! {
69- extern crate diesel;
70- extern crate diesel_migrations;
40+ impl Migration for EmbeddedMigration {
41+ fn version( & self ) -> & str {
42+ self . version
43+ }
7144
72- use self :: diesel_migrations :: * ;
73- use self :: diesel :: connection :: SimpleConnection ;
74- use std :: io ;
45+ fn run ( & self , conn : & SimpleConnection ) -> Result < ( ) , RunMigrationsError > {
46+ conn . batch_execute ( self . up_sql ) . map_err ( Into :: into )
47+ }
7548
76- const ALL_MIGRATIONS : & [ & Migration ] = & [ #( #migrations_expr) , * ] ;
49+ fn revert( & self , _conn: & SimpleConnection ) -> Result <( ) , RunMigrationsError > {
50+ unreachable!( )
51+ }
52+ }
7753
78- #embedded_migration_def
54+ pub fn run<C : MigrationConnection >( conn: & C ) -> Result <( ) , RunMigrationsError > {
55+ run_with_output( conn, & mut io:: sink( ) )
56+ }
7957
80- #run_fns
58+ pub fn run_with_output<C : MigrationConnection >(
59+ conn: & C ,
60+ out: & mut io:: Write ,
61+ ) -> Result <( ) , RunMigrationsError > {
62+ run_migrations( conn, ALL_MIGRATIONS . iter( ) . map( |v| * v) , out)
63+ }
64+ }
8165 }
8266}
8367
0 commit comments