| 1 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::errorrust-analyzer cargo sqlx-core 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#; |
| 2 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::migraterust-analyzer cargo sqlx-core 0.8.3 migrate/::{Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#, MigrationTyperust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/MigrationType#}; |
| 3 | use futures_corerust-analyzer cargo futures-core 0.3.34 crate/::futurerust-analyzer cargo futures-core 0.3.34 future/::BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#; |
| 4 | |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/; |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 9 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 10 | |
| 11 | /// In the default implementation, a MigrationSource is a directory which |
| 12 | /// contains the migration SQL scripts. All these scripts must be stored in |
| 13 | /// files with names using the format `<VERSION>_<DESCRIPTION>.sql`, where |
| 14 | /// `<VERSION>` is a string that can be parsed into `i64` and its value is |
| 15 | /// greater than zero, and `<DESCRIPTION>` is a string. |
| 16 | /// |
| 17 | /// Files that don't match this format are silently ignored. |
| 18 | /// |
| 19 | /// You can create a new empty migration script using sqlx-cli: |
| 20 | /// `sqlx migrate add <DESCRIPTION>`. |
| 21 | /// |
| 22 | /// Note that migrations for each database are tracked using the |
| 23 | /// `_sqlx_migrations` table (stored in the database). If a migration's hash |
| 24 | /// changes and it has already been run, this will cause an error. |
| 25 | pub trait MigrationSourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#<'s>: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# { |
| 26 | fn resolverust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#resolve().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'s, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#>, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#>>; |
| 27 | } |
| 28 | |
| 29 | impl<'s> MigrationSourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#<'s> for &'s Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path# { |
| 30 | fn resolverust-analyzer cargo sqlx-core 0.8.3 migrate/source/impl#[`&'s Path`][`MigrationSource<'s>`]resolve().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'s, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#>, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#>> { |
| 31 | Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move { |
| 32 | let canonical = self.canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 33 | let migrations_with_paths = |
| 34 | craterust-analyzer cargo sqlx-core 0.8.3 crate/::rtrust-analyzer cargo sqlx-core 0.8.3 rt/::spawn_blockingrust-analyzer cargo sqlx-core 0.8.3 rt/spawn_blocking().(move || resolve_blockingrust-analyzer cargo sqlx-core 0.8.3 migrate/source/resolve_blocking().(&canonical)).rust-analyzer cargo sqlx-core 0.8.3 rt/impl#[`JoinHandle<T>`][Future]poll().awaitrust-analyzer cargo sqlx-core 0.8.3 rt/impl#[`JoinHandle<T>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 35 | |
| 36 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(migrations_with_paths.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(m, _p)| m).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()) |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl MigrationSourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#<'static> for PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# { |
| 42 | fn resolverust-analyzer cargo sqlx-core 0.8.3 migrate/source/impl#[PathBuf][`MigrationSource<'static>`]resolve().(self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'static, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#>, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#>> { |
| 43 | Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::pinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]pin().(async move { self.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().().resolverust-analyzer cargo sqlx-core 0.8.3 migrate/source/impl#[`&'s Path`][`MigrationSource<'s>`]resolve().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/impl#[`Pin<P>`][Future]poll(). }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(thiserrorrust-analyzer cargo thiserror 2.0.20 crate/::Errorrust-analyzer cargo thiserror-impl 2.0.20 Error:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 48 | #[error("{message}")] |
| 49 | pub struct ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 50 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 51 | #[sourcerust-analyzer cargo thiserror-impl 2.0.20 source:] |
| 52 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>, |
| 53 | } |
| 54 | |
| 55 | // FIXME: paths should just be part of `Migration` but we can't add a field backwards compatibly |
| 56 | // since it's `#[non_exhaustive]`. |
| 57 | pub fn resolve_blockingrust-analyzer cargo sqlx-core 0.8.3 migrate/source/resolve_blocking().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#)>, ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#> { |
| 58 | let s = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 59 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("error reading migration directory {}: {e}", path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()), |
| 60 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 61 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 62 | |
| 63 | let mut migrations = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 64 | |
| 65 | for res in s { |
| 66 | let entry = res.map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 67 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 68 | "error reading contents of migration directory {}: {e}", |
| 69 | path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().() |
| 70 | ), |
| 71 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 72 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 73 | |
| 74 | let entry_path = entry.pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().(); |
| 75 | |
| 76 | let metadata = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(&entry_path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 77 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 78 | "error getting metadata of migration path {}", |
| 79 | entry_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().() |
| 80 | ), |
| 81 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 82 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | |
| 84 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().metadata.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]is_file().() { |
| 85 | // not a file; ignore |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | let file_name = entry.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]file_name().(); |
| 90 | // This is arguably the wrong choice, |
| 91 | // but it really only matters for parsing the version and description. |
| 92 | // |
| 93 | // Using `.to_str()` and returning an error if the filename is not UTF-8 |
| 94 | // would be a breaking change. |
| 95 | let file_name = file_name.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(); |
| 96 | |
| 97 | let parts = file_name.splitnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]splitn().(2, '_').collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 98 | |
| 99 | if parts.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().2 || !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(".sql") { |
| 100 | // not of the format: <VERSION>_<DESCRIPTION>.<REVERSIBLE_DIRECTION>.sql; ignore |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | let version: i64 = parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() |
| 105 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_e| ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 106 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("error parsing migration filename {file_name:?}; expected integer version prefix (e.g. `01_foo.sql`)"), |
| 107 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 108 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 109 | |
| 110 | let migration_type = MigrationTyperust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/MigrationType#::from_filenamerust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/impl#[MigrationType]from_filename().(parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 111 | |
| 112 | // remove the `.sql` and replace `_` with ` ` |
| 113 | let description = parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 114 | .trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(migration_type.suffixrust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/impl#[MigrationType]suffix().()) |
| 115 | .replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('_', " ") |
| 116 | .to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[T][ToOwned]to_owned().(); |
| 117 | |
| 118 | let sql = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(&entry_path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| ResolveErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError# { |
| 119 | messagerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#message.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 120 | "error reading contents of migration {}: {e}", |
| 121 | entry_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().() |
| 122 | ), |
| 123 | sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/ResolveError#source.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 124 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 125 | |
| 126 | // opt-out of migration transaction |
| 127 | let no_tx = sql.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("-- no-transaction"); |
| 128 | |
| 129 | migrations.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(( |
| 130 | Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#::newrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/impl#[Migration]new().( |
| 131 | version, |
| 132 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(description), |
| 133 | migration_type, |
| 134 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(sql), |
| 135 | no_tx, |
| 136 | ), |
| 137 | entry_path, |
| 138 | )); |
| 139 | } |
| 140 | |
| 141 | // Ensure that we are sorted by version in ascending order. |
| 142 | migrations.sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|(m, _)| m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.); |
| 143 | |
| 144 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(migrations) |
| 145 | } |