| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#; |
| 4 | |
| 5 | 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#; |
| 6 | pub(craterust-analyzer cargo sqlx-mysql 0.8.3 crate/) use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::migraterust-analyzer cargo sqlx-core 0.8.3 migrate/::*; |
| 7 | |
| 8 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::connectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/::{ConnectOptionsrust-analyzer cargo sqlx-core 0.8.3 connection/ConnectOptions#, Connectionrust-analyzer cargo sqlx-core 0.8.3 connection/Connection#}; |
| 9 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#; |
| 10 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::executorrust-analyzer cargo sqlx-core 0.8.3 executor/::Executorrust-analyzer cargo sqlx-core 0.8.3 executor/Executor#; |
| 11 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::queryrust-analyzer cargo sqlx-core 0.8.3 query/::queryrust-analyzer cargo sqlx-core 0.8.3 query/query().; |
| 12 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/::query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as().; |
| 13 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/::query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().; |
| 14 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::{MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#, MySqlConnectOptionsrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#, MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection#}; |
| 15 | |
| 16 | fn parse_for_maintenancerust-analyzer cargo sqlx-mysql 0.8.3 migrate/parse_for_maintenance().(url: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(MySqlConnectOptionsrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> { |
| 17 | let mut options = MySqlConnectOptionsrust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#::from_strrust-analyzer cargo sqlx-mysql 0.8.3 options/parse/impl#[MySqlConnectOptions][FromStr]from_str().(url)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 18 | |
| 19 | let database = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(database) = &options.databaserust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#database. { |
| 20 | database.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[T][ToOwned]to_owned().() |
| 21 | } else { |
| 22 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#::Configurationrust-analyzer cargo sqlx-core 0.8.3 error/Error#Configuration#( |
| 23 | "DATABASE_URL does not specify a database".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 24 | )); |
| 25 | }; |
| 26 | |
| 27 | // switch us to <no> database for create/drop commands |
| 28 | options.databaserust-analyzer cargo sqlx-mysql 0.8.3 options/MySqlConnectOptions#database. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 29 | |
| 30 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((options, database)) |
| 31 | } |
| 32 | |
| 33 | impl MigrateDatabaserust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/MigrateDatabase# for MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql# { |
| 34 | fn create_databaserust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySql][MigrateDatabase]create_database().(url: &str) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> { |
| 35 | 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 { |
| 36 | let (options, database) = parse_for_maintenancerust-analyzer cargo sqlx-mysql 0.8.3 migrate/parse_for_maintenance().(url)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 37 | let mut conn = options.connectrust-analyzer cargo sqlx-mysql 0.8.3 options/connect/impl#[MySqlConnectOptions][ConnectOptions]connect().().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().; |
| 38 | |
| 39 | let _ = conn |
| 40 | .executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("CREATE DATABASE `{database}`")) |
| 41 | .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().; |
| 42 | |
| 43 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | fn database_existsrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySql][MigrateDatabase]database_exists().(url: &str) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> { |
| 48 | 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 { |
| 49 | let (options, database) = parse_for_maintenancerust-analyzer cargo sqlx-mysql 0.8.3 migrate/parse_for_maintenance().(url)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 50 | let mut conn = options.connectrust-analyzer cargo sqlx-mysql 0.8.3 options/connect/impl#[MySqlConnectOptions][ConnectOptions]connect().().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().; |
| 51 | |
| 52 | let exists: bool = query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().( |
| 53 | "select exists(SELECT 1 from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?)", |
| 54 | ) |
| 55 | .bindrust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>`]bind().(database) |
| 56 | .fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(&mut conn) |
| 57 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 58 | |
| 59 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(exists) |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | fn drop_databaserust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySql][MigrateDatabase]drop_database().(url: &str) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#>> { |
| 64 | 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 { |
| 65 | let (options, database) = parse_for_maintenancerust-analyzer cargo sqlx-mysql 0.8.3 migrate/parse_for_maintenance().(url)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 66 | let mut conn = options.connectrust-analyzer cargo sqlx-mysql 0.8.3 options/connect/impl#[MySqlConnectOptions][ConnectOptions]connect().().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().; |
| 67 | |
| 68 | let _ = conn |
| 69 | .executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("DROP DATABASE IF EXISTS `{database}`")) |
| 70 | .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().; |
| 71 | |
| 72 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Migraterust-analyzer cargo sqlx-core 0.8.3 migrate/migrate/Migrate# for MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection# { |
| 78 | fn ensure_migrations_tablerust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]ensure_migrations_table().(&mut self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, 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#>> { |
| 79 | 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 { |
| 80 | // language=MySQL |
| 81 | self.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().( |
| 82 | r#" |
| 83 | CREATE TABLE IF NOT EXISTS _sqlx_migrations ( |
| 84 | version BIGINT PRIMARY KEY, |
| 85 | description TEXT NOT NULL, |
| 86 | installed_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 87 | success BOOLEAN NOT NULL, |
| 88 | checksum BLOB NOT NULL, |
| 89 | execution_time BIGINT NOT NULL |
| 90 | ); |
| 91 | "#, |
| 92 | ) |
| 93 | .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().; |
| 94 | |
| 95 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | fn dirty_versionrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]dirty_version().(&mut self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<i64>, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#>> { |
| 100 | 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 { |
| 101 | // language=SQL |
| 102 | let row: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(i64,)> = query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as().( |
| 103 | "SELECT version FROM _sqlx_migrations WHERE success = false ORDER BY version LIMIT 1", |
| 104 | ) |
| 105 | .fetch_optionalrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_optional().(self) |
| 106 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 107 | |
| 108 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(row.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|r| r.0)) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | fn list_applied_migrationsrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]list_applied_migrations().( |
| 113 | &mut self, |
| 114 | ) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, 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#<AppliedMigrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#>, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#>> { |
| 115 | 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 { |
| 116 | // language=SQL |
| 117 | let rows: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(i64, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>)> = |
| 118 | query_asrust-analyzer cargo sqlx-core 0.8.3 query_as/query_as().("SELECT version, checksum FROM _sqlx_migrations ORDER BY version") |
| 119 | .fetch_allrust-analyzer cargo sqlx-core 0.8.3 query_as/impl#[`QueryAs<'q, DB, O, A>`]fetch_all().(self) |
| 120 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 121 | |
| 122 | let migrations = rows |
| 123 | .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() |
| 124 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(version, checksum)| AppliedMigrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration# { |
| 125 | versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#version., |
| 126 | checksumrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/AppliedMigration#checksum.: checksum.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 127 | }) |
| 128 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 129 | |
| 130 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(migrations) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | fn lockrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]lock().(&mut self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, 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#>> { |
| 135 | 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 { |
| 136 | let database_name = current_databaserust-analyzer cargo sqlx-mysql 0.8.3 migrate/current_database().(self).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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 137 | let lock_id = generate_lock_idrust-analyzer cargo sqlx-mysql 0.8.3 migrate/generate_lock_id().(&database_name); |
| 138 | |
| 139 | // create an application lock over the database |
| 140 | // this function will not return until the lock is acquired |
| 141 | |
| 142 | // https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS |
| 143 | // https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS-TABLE |
| 144 | |
| 145 | // language=MySQL |
| 146 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("SELECT GET_LOCK(?, -1)") |
| 147 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(lock_id) |
| 148 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(self) |
| 149 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 150 | |
| 151 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | fn unlockrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]unlock().(&mut self) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'_, 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#>> { |
| 156 | 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 { |
| 157 | let database_name = current_databaserust-analyzer cargo sqlx-mysql 0.8.3 migrate/current_database().(self).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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 158 | let lock_id = generate_lock_idrust-analyzer cargo sqlx-mysql 0.8.3 migrate/generate_lock_id().(&database_name); |
| 159 | |
| 160 | // language=MySQL |
| 161 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().("SELECT RELEASE_LOCK(?)") |
| 162 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(lock_id) |
| 163 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(self) |
| 164 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 165 | |
| 166 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | fn applyrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]apply().<'e: 'm, 'm>( |
| 171 | &'e mut self, |
| 172 | migration: &'m Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#, |
| 173 | ) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'m, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#>> { |
| 174 | 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 { |
| 175 | // Use a single transaction for the actual migration script and the essential bookeeping so we never |
| 176 | // execute migrations twice. See https://github.com/launchbadge/sqlx/issues/1966. |
| 177 | // The `execution_time` however can only be measured for the whole transaction. This value _only_ exists for |
| 178 | // data lineage and debugging reasons, so it is not super important if it is lost. So we initialize it to -1 |
| 179 | // and update it once the actual transaction completed. |
| 180 | let mut tx = self.beginrust-analyzer cargo sqlx-mysql 0.8.3 connection/impl#[MySqlConnection][Connection]begin().().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 | let start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 182 | |
| 183 | // For MySQL we cannot really isolate migrations due to implicit commits caused by table modification, see |
| 184 | // https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html |
| 185 | // |
| 186 | // To somewhat try to detect this, we first insert the migration into the migration table with |
| 187 | // `success=FALSE` and later modify the flag. |
| 188 | // |
| 189 | // language=MySQL |
| 190 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().( |
| 191 | r#" |
| 192 | INSERT INTO _sqlx_migrations ( version, description, success, checksum, execution_time ) |
| 193 | VALUES ( ?, ?, FALSE, ?, -1 ) |
| 194 | "#, |
| 195 | ) |
| 196 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) |
| 197 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().migration.descriptionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#description.) |
| 198 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().migration.checksumrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#checksum.) |
| 199 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`][DerefMut]deref_mut().tx) |
| 200 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 201 | |
| 202 | let _ = tx |
| 203 | .executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().migration.sqlrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#sql.) |
| 204 | .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(). |
| 205 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#::ExecuteMigrationrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#ExecuteMigration#(e, migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 206 | |
| 207 | // language=MySQL |
| 208 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().( |
| 209 | r#" |
| 210 | UPDATE _sqlx_migrations |
| 211 | SET success = TRUE |
| 212 | WHERE version = ? |
| 213 | "#, |
| 214 | ) |
| 215 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) |
| 216 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`][DerefMut]deref_mut().tx) |
| 217 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 218 | |
| 219 | tx.commitrust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`]commit().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 220 | |
| 221 | // Update `elapsed_time`. |
| 222 | // NOTE: The process may disconnect/die at this point, so the elapsed time value might be lost. We accept |
| 223 | // this small risk since this value is not super important. |
| 224 | |
| 225 | let elapsed = start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().(); |
| 226 | |
| 227 | #[allow(clippy::cast_possible_truncation)] |
| 228 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().( |
| 229 | r#" |
| 230 | UPDATE _sqlx_migrations |
| 231 | SET execution_time = ? |
| 232 | WHERE version = ? |
| 233 | "#, |
| 234 | ) |
| 235 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(elapsed.as_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_nanos().() as i64) |
| 236 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) |
| 237 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(self) |
| 238 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 239 | |
| 240 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(elapsed) |
| 241 | }) |
| 242 | } |
| 243 | |
| 244 | fn revertrust-analyzer cargo sqlx-mysql 0.8.3 migrate/impl#[MySqlConnection][Migrate]revert().<'e: 'm, 'm>( |
| 245 | &'e mut self, |
| 246 | migration: &'m Migrationrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#, |
| 247 | ) -> BoxFuturerust-analyzer cargo futures-core 0.3.34 future/BoxFuture#<'m, Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#>> { |
| 248 | 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 { |
| 249 | // Use a single transaction for the actual migration script and the essential bookeeping so we never |
| 250 | // execute migrations twice. See https://github.com/launchbadge/sqlx/issues/1966. |
| 251 | let mut tx = self.beginrust-analyzer cargo sqlx-mysql 0.8.3 connection/impl#[MySqlConnection][Connection]begin().().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().; |
| 252 | let start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 253 | |
| 254 | // For MySQL we cannot really isolate migrations due to implicit commits caused by table modification, see |
| 255 | // https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html |
| 256 | // |
| 257 | // To somewhat try to detect this, we first insert the migration into the migration table with |
| 258 | // `success=FALSE` and later remove the migration altogether. |
| 259 | // |
| 260 | // language=MySQL |
| 261 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().( |
| 262 | r#" |
| 263 | UPDATE _sqlx_migrations |
| 264 | SET success = FALSE |
| 265 | WHERE version = ? |
| 266 | "#, |
| 267 | ) |
| 268 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) |
| 269 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`][DerefMut]deref_mut().tx) |
| 270 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 271 | |
| 272 | tx.executerust-analyzer cargo sqlx-core 0.8.3 executor/Executor#execute().(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().migration.sqlrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#sql.).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().; |
| 273 | |
| 274 | // language=SQL |
| 275 | let _ = queryrust-analyzer cargo sqlx-core 0.8.3 query/query().(r#"DELETE FROM _sqlx_migrations WHERE version = ?"#) |
| 276 | .bindrust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, <DB as Database>::Arguments<'q>>`]bind().(migration.versionrust-analyzer cargo sqlx-core 0.8.3 migrate/migration/Migration#version.) |
| 277 | .executerust-analyzer cargo sqlx-core 0.8.3 query/impl#[`Query<'q, DB, A>`]execute().(&mut *rust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`][DerefMut]deref_mut().tx) |
| 278 | .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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 279 | |
| 280 | tx.commitrust-analyzer cargo sqlx-core 0.8.3 transaction/impl#[`Transaction<'c, DB>`]commit().().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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 281 | |
| 282 | let elapsed = start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().(); |
| 283 | |
| 284 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(elapsed) |
| 285 | }) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | async fn current_databaserust-analyzer cargo sqlx-mysql 0.8.3 migrate/current_database().(conn: &mut MySqlConnectionrust-analyzer cargo sqlx-mysql 0.8.3 connection/MySqlConnection#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, MigrateErrorrust-analyzer cargo sqlx-core 0.8.3 migrate/error/MigrateError#> { |
| 290 | // language=MySQL |
| 291 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(query_scalarrust-analyzer cargo sqlx-core 0.8.3 query_scalar/query_scalar().("SELECT DATABASE()").fetch_onerust-analyzer cargo sqlx-core 0.8.3 query_scalar/impl#[`QueryScalar<'q, DB, O, A>`]fetch_one().(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().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 292 | } |
| 293 | |
| 294 | // inspired from rails: https://github.com/rails/rails/blob/6e49cc77ab3d16c06e12f93158eaf3e507d4120e/activerecord/lib/active_record/migration.rb#L1308 |
| 295 | fn generate_lock_idrust-analyzer cargo sqlx-mysql 0.8.3 migrate/generate_lock_id().(database_name: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 296 | const CRC_IEEErust-analyzer cargo sqlx-mysql 0.8.3 migrate/CRC_IEEE.: crcrust-analyzer cargo crc 3.4.0 crate/::Crcrust-analyzer cargo crc 3.4.0 Crc#<u32> = crcrust-analyzer cargo crc 3.4.0 crate/::Crcrust-analyzer cargo crc 3.4.0 Crc#::<u32>::newrust-analyzer cargo crc 3.4.0 crc32/impl#[`Crc<u32, Table<L>>`]new().(&crcrust-analyzer cargo crc 3.4.0 crate/::CRC_32_ISO_HDLCrust-analyzer cargo crc-catalog 2.5.0 algorithm/CRC_32_ISO_HDLC.); |
| 297 | // 0x3d32ad9e chosen by fair dice roll |
| 298 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 299 | "{:x}", |
| 300 | 0x3d32ad9e *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Mul<Self>`]mul(). (CRC_IEEErust-analyzer cargo sqlx-mysql 0.8.3 migrate/CRC_IEEE..checksumrust-analyzer cargo crc 3.4.0 crc32/impl#[`Crc<u32, Table<L>>`]checksum().(database_name.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) as i64) |
| 301 | ) |
| 302 | } |