| 1 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/::Acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#; |
| 2 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::migraterust-analyzer cargo sqlx-core 0.8.3 migrate/::{AppliedMigrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#, Migraterust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#, Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#, MigrationSourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#}; |
| 3 | 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#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::{HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#, HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#}; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/; |
| 7 | |
| 8 | /// A resolved set of migrations, ready to be run. |
| 9 | /// |
| 10 | /// Can be constructed statically using `migrate!()` or at runtime using [`Migrator::new()`]. |
| 11 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 12 | // Forbids `migrate!()` from constructing this: |
| 13 | // #[non_exhaustive] |
| 14 | pub struct Migratorrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator# { |
| 15 | // NOTE: these fields are semver-exempt and may be changed or removed in any future version. |
| 16 | // These have to be public for `migrate!()` to be able to initialize them in an implicitly |
| 17 | // const-promotable context. A `const fn` constructor isn't implicitly const-promotable. |
| 18 | #[doc(hidden)] |
| 19 | pub migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#migrations.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'static, [Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#]>, |
| 20 | #[doc(hidden)] |
| 21 | pub ignore_missingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#ignore_missing.: bool, |
| 22 | #[doc(hidden)] |
| 23 | pub lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking.: bool, |
| 24 | #[doc(hidden)] |
| 25 | pub no_txrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#no_tx.: bool, |
| 26 | } |
| 27 | |
| 28 | fn validate_applied_migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/validate_applied_migrations().( |
| 29 | applied_migrations: &[AppliedMigrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#], |
| 30 | migrator: &Migratorrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#, |
| 31 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> { |
| 32 | if migrator.ignore_missingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#ignore_missing. { |
| 33 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 34 | } |
| 35 | |
| 36 | let migrations: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<_> = migrator.iterrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|m| m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 37 | |
| 38 | for applied_migration in applied_migrations { |
| 39 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().migrations.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&applied_migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#version.) { |
| 40 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::VersionMissingrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#VersionMissing#(applied_migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#version.)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 45 | } |
| 46 | |
| 47 | impl Migratorrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator# { |
| 48 | #[doc(hidden)] |
| 49 | pub const DEFAULTrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]DEFAULT.: Migratorrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator# = Migratorrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator# { |
| 50 | migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#migrations.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(&[]), |
| 51 | ignore_missingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#ignore_missing.: false, |
| 52 | no_txrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#no_tx.: false, |
| 53 | lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking.: true, |
| 54 | }; |
| 55 | |
| 56 | /// Creates a new instance with the given source. |
| 57 | /// |
| 58 | /// # Examples |
| 59 | /// |
| 60 | /// ```rust,no_run |
| 61 | /// # use sqlx_core::migrate::MigrateError; |
| 62 | /// # fn main() -> Result<(), MigrateError> { |
| 63 | /// # sqlx::__rt::test_block_on(async move { |
| 64 | /// # use sqlx_core::migrate::Migrator; |
| 65 | /// use std::path::Path; |
| 66 | /// |
| 67 | /// // Read migrations from a local folder: ./migrations |
| 68 | /// let m = Migrator::new(Path::new("./migrations")).await?; |
| 69 | /// # Ok(()) |
| 70 | /// # }) |
| 71 | /// # } |
| 72 | /// ``` |
| 73 | /// See [MigrationSource] for details on structure of the `./migrations` directory. |
| 74 | pub async fn newrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]new().<'s, S>(source: S) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator], MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> |
| 75 | where |
| 76 | S: MigrationSourcerust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#<'s>, |
| 77 | { |
| 78 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator] { |
| 79 | migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#migrations.: 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#(source.resolverust-analyzer cargo sqlx-core 0.8.3 migrate/source/MigrationSource#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()..map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::Sourcerust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#Source#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().), |
| 80 | ..Selfrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]::DEFAULTrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]DEFAULT. |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | /// Specify whether applied migrations that are missing from the resolved migrations should be ignored. |
| 85 | pub fn set_ignore_missingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]set_ignore_missing().(&mut self, ignore_missing: bool) -> &Selfrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator] { |
| 86 | self.ignore_missingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#ignore_missing. = ignore_missing; |
| 87 | self |
| 88 | } |
| 89 | |
| 90 | /// Specify whether or not to lock the database during migration. Defaults to `true`. |
| 91 | /// |
| 92 | /// ### Warning |
| 93 | /// Disabling locking can lead to errors or data loss if multiple clients attempt to apply migrations simultaneously |
| 94 | /// without some sort of mutual exclusion. |
| 95 | /// |
| 96 | /// This should only be used if the database does not support locking, e.g. CockroachDB which talks the Postgres |
| 97 | /// protocol but does not support advisory locks used by SQLx's migrations support for Postgres. |
| 98 | pub fn set_lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]set_locking().(&mut self, locking: bool) -> &Selfrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator] { |
| 99 | self.lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking. = locking; |
| 100 | self |
| 101 | } |
| 102 | |
| 103 | /// Get an iterator over all known migrations. |
| 104 | pub fn iterrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]iter().(&self) -> slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::Iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/Iter#<'_, Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#> { |
| 105 | self.migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#migrations..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 106 | } |
| 107 | |
| 108 | /// Check if a migration version exists. |
| 109 | pub fn version_existsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]version_exists().(&self, version: i64) -> bool { |
| 110 | self.iterrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|m| m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialEq<Self>`]eq().version) |
| 111 | } |
| 112 | |
| 113 | /// Run any pending migrations against the database; and, validate previously applied migrations |
| 114 | /// against the current migration source to detect accidental changes in previously-applied migrations. |
| 115 | /// |
| 116 | /// # Examples |
| 117 | /// |
| 118 | /// ```rust,no_run |
| 119 | /// # use sqlx::migrate::MigrateError; |
| 120 | /// # fn main() -> Result<(), MigrateError> { |
| 121 | /// # sqlx::__rt::test_block_on(async move { |
| 122 | /// use sqlx::migrate::Migrator; |
| 123 | /// use sqlx::sqlite::SqlitePoolOptions; |
| 124 | /// |
| 125 | /// let m = Migrator::new(std::path::Path::new("./migrations")).await?; |
| 126 | /// let pool = SqlitePoolOptions::new().connect("sqlite::memory:").await?; |
| 127 | /// m.run(&pool).await |
| 128 | /// # }) |
| 129 | /// # } |
| 130 | /// ``` |
| 131 | pub async fn runrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]run().<'a, A>(&self, migrator: A) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> |
| 132 | where |
| 133 | A: Acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#<'a>, |
| 134 | <A::Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Connection# as Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#>::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target#: Migraterust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#, |
| 135 | { |
| 136 | let mut conn = migrator.acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#acquire().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 137 | self.run_directrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]run_direct().(&mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut#deref_mut().conn).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). |
| 138 | } |
| 139 | |
| 140 | // Getting around the annoying "implementation of `Acquire` is not general enough" error |
| 141 | #[doc(hidden)] |
| 142 | pub async fn run_directrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]run_direct().<C>(&self, conn: &mut C) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> |
| 143 | where |
| 144 | C: Migraterust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#, |
| 145 | { |
| 146 | // lock the database for exclusive access by the migrator |
| 147 | if self.lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking. { |
| 148 | conn.lockrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#lock().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 149 | } |
| 150 | |
| 151 | // creates [_migrations] table only if needed |
| 152 | // eventually this will likely migrate previous versions of the table |
| 153 | conn.ensure_migrations_tablerust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#ensure_migrations_table().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 154 | |
| 155 | let version = conn.dirty_versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#dirty_version().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 156 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version) = version { |
| 157 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::Dirtyrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#Dirty#(version)); |
| 158 | } |
| 159 | |
| 160 | let applied_migrations = conn.list_applied_migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#list_applied_migrations().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 161 | validate_applied_migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/validate_applied_migrations().(&applied_migrations, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 162 | |
| 163 | let applied_migrations: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _> = applied_migrations |
| 164 | .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() |
| 165 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|m| (m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#version., m)) |
| 166 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 167 | |
| 168 | for migration in self.iterrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]iter().() { |
| 169 | if migration.migration_typerust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#migration_type..is_down_migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/impl#[MigrationType]is_down_migration().() { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | match applied_migrations.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) { |
| 174 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(applied_migration) => { |
| 175 | if migration.checksumrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#checksum. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().applied_migration.checksumrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#checksum. { |
| 176 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::VersionMismatchrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#VersionMismatch#(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.)); |
| 177 | } |
| 178 | } |
| 179 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 180 | conn.applyrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#apply().(migration).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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // unlock the migrator to allow other migrators to run |
| 186 | // but do nothing as we already migrated |
| 187 | if self.lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking. { |
| 188 | conn.unlockrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#unlock().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 189 | } |
| 190 | |
| 191 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 192 | } |
| 193 | |
| 194 | /// Run down migrations against the database until a specific version. |
| 195 | /// |
| 196 | /// # Examples |
| 197 | /// |
| 198 | /// ```rust,no_run |
| 199 | /// # use sqlx::migrate::MigrateError; |
| 200 | /// # fn main() -> Result<(), MigrateError> { |
| 201 | /// # sqlx::__rt::test_block_on(async move { |
| 202 | /// use sqlx::migrate::Migrator; |
| 203 | /// use sqlx::sqlite::SqlitePoolOptions; |
| 204 | /// |
| 205 | /// let m = Migrator::new(std::path::Path::new("./migrations")).await?; |
| 206 | /// let pool = SqlitePoolOptions::new().connect("sqlite::memory:").await?; |
| 207 | /// m.undo(&pool, 4).await |
| 208 | /// # }) |
| 209 | /// # } |
| 210 | /// ``` |
| 211 | pub async fn undorust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]undo().<'a, A>(&self, migrator: A, target: i64) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> |
| 212 | where |
| 213 | A: Acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#<'a>, |
| 214 | <A::Connectionrust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#Connection# as Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#>::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target#: Migraterust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#, |
| 215 | { |
| 216 | let mut conn = migrator.acquirerust-analyzer cargo sqlx-core 0.8.3 acquire/Acquire#acquire().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 217 | |
| 218 | // lock the database for exclusive access by the migrator |
| 219 | if self.lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking. { |
| 220 | conn.lockrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#lock().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 221 | } |
| 222 | |
| 223 | // creates [_migrations] table only if needed |
| 224 | // eventually this will likely migrate previous versions of the table |
| 225 | conn.ensure_migrations_tablerust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#ensure_migrations_table().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 226 | |
| 227 | let version = conn.dirty_versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#dirty_version().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 228 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(version) = version { |
| 229 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::Dirtyrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#Dirty#(version)); |
| 230 | } |
| 231 | |
| 232 | let applied_migrations = conn.list_applied_migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#list_applied_migrations().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 233 | validate_applied_migrationsrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/validate_applied_migrations().(&applied_migrations, self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 234 | |
| 235 | let applied_migrations: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _> = applied_migrations |
| 236 | .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() |
| 237 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|m| (m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#version., m)) |
| 238 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 239 | |
| 240 | for migration in self |
| 241 | .iterrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/impl#[Migrator]iter().() |
| 242 | .revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() |
| 243 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|m| m.migration_typerust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#migration_type..is_down_migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration_type/impl#[MigrationType]is_down_migration().()) |
| 244 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|m| applied_migrations.contains_keyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]contains_key().(&m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.)) |
| 245 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|m| m.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().target) |
| 246 | { |
| 247 | conn.revertrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#revert().(migration).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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 248 | } |
| 249 | |
| 250 | // unlock the migrator to allow other migrators to run |
| 251 | // but do nothing as we already migrated |
| 252 | if self.lockingrust-analyzer cargo sqlx-core 0.8.3 migrate/migrator/Migrator#locking. { |
| 253 | conn.unlockrust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate#unlock().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 254 | } |
| 255 | |
| 256 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 257 | } |
| 258 | } |