@@ -24,7 +24,7 @@ struct QuotedMigration {
24
24
version : i64 ,
25
25
description : String ,
26
26
migration_type : QuotedMigrationType ,
27
- sql : String ,
27
+ path : String ,
28
28
checksum : Vec < u8 > ,
29
29
}
30
30
@@ -34,7 +34,7 @@ impl ToTokens for QuotedMigration {
34
34
version,
35
35
description,
36
36
migration_type,
37
- sql ,
37
+ path ,
38
38
checksum,
39
39
} = & self ;
40
40
@@ -43,7 +43,8 @@ impl ToTokens for QuotedMigration {
43
43
version: #version,
44
44
description: :: std:: borrow:: Cow :: Borrowed ( #description) ,
45
45
migration_type: #migration_type,
46
- sql: :: std:: borrow:: Cow :: Borrowed ( #sql) ,
46
+ // this tells the compiler to watch this path for changes
47
+ sql: :: std:: borrow:: Cow :: Borrowed ( include_str!( #path) ) ,
47
48
checksum: :: std:: borrow:: Cow :: Borrowed ( & [
48
49
#( #checksum) , *
49
50
] ) ,
@@ -59,7 +60,7 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<TokenStream
59
60
let path = crate :: common:: resolve_path ( & dir. value ( ) , dir. span ( ) ) ?;
60
61
let mut migrations = Vec :: new ( ) ;
61
62
62
- for entry in fs:: read_dir ( path) ? {
63
+ for entry in fs:: read_dir ( & path) ? {
63
64
let entry = entry?;
64
65
if !fs:: metadata ( entry. path ( ) ) ?. is_file ( ) {
65
66
// not a file; ignore
@@ -89,18 +90,43 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<TokenStream
89
90
90
91
let checksum = Vec :: from ( Sha384 :: digest ( sql. as_bytes ( ) ) . as_slice ( ) ) ;
91
92
93
+ // canonicalize the path so we can pass it to `include_str!()`
94
+ let path = entry. path ( ) . canonicalize ( ) ?;
95
+ let path = path
96
+ . to_str ( )
97
+ . ok_or_else ( || {
98
+ format ! (
99
+ "migration path cannot be represented as a string: {:?}" ,
100
+ path
101
+ )
102
+ } ) ?
103
+ . to_owned ( ) ;
104
+
92
105
migrations. push ( QuotedMigration {
93
106
version,
94
107
description,
95
108
migration_type : QuotedMigrationType ( migration_type) ,
96
- sql ,
109
+ path ,
97
110
checksum,
98
111
} )
99
112
}
100
113
101
114
// ensure that we are sorted by `VERSION ASC`
102
115
migrations. sort_by_key ( |m| m. version ) ;
103
116
117
+ #[ cfg( any( sqlx_macros_unstable, procmacro2_semver_exempt) ) ]
118
+ {
119
+ let path = path. canonicalize ( ) ?;
120
+ let path = path. to_str ( ) . ok_or_else ( || {
121
+ format ! (
122
+ "migration directory path cannot be represented as a string: {:?}" ,
123
+ path
124
+ )
125
+ } ) ?;
126
+
127
+ proc_macro:: tracked_path:: path ( path) ;
128
+ }
129
+
104
130
Ok ( quote ! {
105
131
:: sqlx:: migrate:: Migrator {
106
132
migrations: :: std:: borrow:: Cow :: Borrowed ( & [
0 commit comments