1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
//! # SQLx Migrate
//!
//! An opinionated migration micro-framework that uses [SQLx](https://github.com/launchbadge/sqlx).
//!
//! All migrations are written in Rust, and are designed to be embedded in existing applications.
//!
#![cfg_attr(feature = "_docs", feature(doc_cfg))]
#![warn(clippy::pedantic)]
#![allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::unreadable_literal,
clippy::doc_markdown,
clippy::module_name_repetitions
)]
use db::{AppliedMigration, Migrations};
use futures_core::future::LocalBoxFuture;
use itertools::{EitherOrBoth, Itertools};
use sha2::{Digest, Sha256};
use sqlx::{ConnectOptions, Connection, Database, Executor, Pool};
use state::TypeMap;
use std::{
borrow::Cow,
str::FromStr,
sync::Arc,
time::{Duration, Instant},
};
pub mod context;
pub mod db;
pub mod error;
pub use context::MigrationContext;
pub use error::Error;
#[cfg(feature = "cli")]
#[cfg_attr(feature = "_docs", doc(cfg(feature = "cli")))]
pub mod cli;
#[cfg(feature = "generate")]
#[cfg_attr(feature = "_docs", doc(cfg(feature = "generate")))]
mod gen;
#[cfg(feature = "generate")]
#[cfg_attr(feature = "_docs", doc(cfg(feature = "generate")))]
pub use gen::generate;
type MigrationFn<DB> =
Box<dyn Fn(&mut MigrationContext<DB>) -> LocalBoxFuture<Result<(), MigrationError>>>;
/// The default migrations table used by all migrators.
pub const DEFAULT_MIGRATIONS_TABLE: &str = "_sqlx_migrations";
/// Commonly used types and functions.
pub mod prelude {
pub use super::Migration;
pub use super::MigrationContext;
pub use super::MigrationError;
pub use super::MigrationStatus;
pub use super::MigrationSummary;
pub use super::Migrator;
pub use super::MigratorOptions;
}
/// A single migration that uses a given [`sqlx::Transaction`] to do the up (migrate) and down (revert) migrations.
///
/// # Example
///
/// ```
/// use sqlx_migrate::Migration;
/// use sqlx::{Executor, Postgres};
///
/// let migration = Migration::<Postgres>::new("initial migration", |tx| {
/// Box::pin(async move {
/// tx.execute("CREATE TABLE example ();").await?;
/// Ok(())
/// })
/// })
/// // Low-effort (optional) checksum.
/// .with_checksum(b"CREATE TABLE example ();".as_slice())
/// .reversible(|tx| {
/// Box::pin(async move {
/// tx.execute("DROP TABLE example;");
/// Ok(())
/// })
/// });
/// ```
pub struct Migration<DB: Database> {
name: Cow<'static, str>,
up: MigrationFn<DB>,
down: Option<MigrationFn<DB>>,
}
impl<DB: Database> Migration<DB> {
/// Create a new migration with the given name
/// and migration function.
pub fn new(
name: impl Into<Cow<'static, str>>,
up: impl Fn(&mut MigrationContext<DB>) -> LocalBoxFuture<Result<(), MigrationError>> + 'static,
) -> Self {
Self {
name: name.into(),
up: Box::new(up),
down: None,
}
}
/// Set a down migration function.
#[must_use]
pub fn reversible(
mut self,
down: impl Fn(&mut MigrationContext<DB>) -> LocalBoxFuture<Result<(), MigrationError>> + 'static,
) -> Self {
self.down = Some(Box::new(down));
self
}
/// Same as [`Migration::reversible`]
#[must_use]
pub fn revertible(
self,
down: impl Fn(&mut MigrationContext<DB>) -> LocalBoxFuture<Result<(), MigrationError>> + 'static,
) -> Self {
self.reversible(down)
}
/// Get the migration's name.
#[must_use]
pub fn name(&self) -> &str {
self.name.as_ref()
}
/// Whether the migration is reversible or not.
#[must_use]
pub fn is_reversible(&self) -> bool {
self.down.is_some()
}
/// Whether the migration is reversible or not.
#[must_use]
pub fn is_revertible(&self) -> bool {
self.down.is_some()
}
}
impl<DB: Database> Eq for Migration<DB> {}
impl<DB: Database> PartialEq for Migration<DB> {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
/// A Migrator that is capable of managing migrations for a database.
///
/// # Example
///
/// ```no_run
/// use crate::{Error, Migration, Migrator};
/// use sqlx::{Executor, Postgres};
///
/// async fn migrate() -> Result<(), Error> {
/// let mut migrator: Migrator<Postgres> =
/// Migrator::connect("postgres://postgres:postgres@localhost:5432/postgres").await?;
///
/// let migration = Migration::<Postgres>::new("initial migration", |tx| {
/// Box::pin(async move {
/// tx.execute("CREATE TABLE example ();").await?;
/// Ok(())
/// })
/// })
/// .reversible(|tx| {
/// Box::pin(async move {
/// tx.execute("DROP TABLE example;");
/// Ok(())
/// })
/// });
///
/// migrator.add_migrations([migration]);
///
/// // Make sure all migrations are consistent with the database.
/// migrator.check_migrations().await?;
///
/// // Migrate
/// let summary = migrator.migrate(migrator.local_migrations().len() as _).await?;
///
/// assert_eq!(summary.new_version, Some(1));
///
/// // List all migrations.
/// let status = migrator.status().await?;
///
/// // Verify that all of them are applied.
/// for migration in status {
/// assert!(migration.applied.is_some());
/// }
///
/// Ok(())
/// }
/// ```
#[must_use]
pub struct Migrator<Db>
where
Db: Database,
Db::Connection: db::Migrations,
{
options: MigratorOptions,
conn: Db::Connection,
table: Cow<'static, str>,
migrations: Vec<Migration<Db>>,
extensions: Arc<TypeMap!(Send + Sync)>,
}
impl<Db> Migrator<Db>
where
Db: Database,
Db::Connection: db::Migrations,
for<'a> &'a mut Db::Connection: Executor<'a>,
{
/// Create a new migrator that uses an existing connection.
pub fn new(conn: Db::Connection) -> Self {
Self {
options: MigratorOptions::default(),
conn,
table: Cow::Borrowed(DEFAULT_MIGRATIONS_TABLE),
migrations: Vec::default(),
extensions: Arc::new(<TypeMap![Send + Sync]>::new()),
}
}
/// Connect to a database given in the URL.
///
/// If this method is used, `SQLx` statement logging is explicitly disabled.
/// To customize the connection, use [`Migrator::connect_with`].
///
/// # Errors
///
/// An error is returned on connection failure.
pub async fn connect(url: &str) -> Result<Self, sqlx::Error> {
let mut opts: <<Db as Database>::Connection as Connection>::Options = url.parse()?;
opts = opts.disable_statement_logging();
let mut conn = Db::Connection::connect_with(&opts).await?;
conn.execute(
r#"--sql
SET client_min_messages TO WARNING;
"#,
)
.await?;
Ok(Self {
options: MigratorOptions::default(),
conn,
table: Cow::Borrowed(DEFAULT_MIGRATIONS_TABLE),
migrations: Vec::default(),
extensions: Arc::new(<TypeMap![Send + Sync]>::new()),
})
}
/// Connect to a database with the given connection options.
///
/// # Errors
///
/// An error is returned on connection failure.
pub async fn connect_with(
options: &<Db::Connection as Connection>::Options,
) -> Result<Self, sqlx::Error> {
let mut conn = Db::Connection::connect_with(options).await?;
conn.execute(
r#"--sql
SET client_min_messages TO WARNING;
"#,
)
.await?;
Ok(Self {
options: MigratorOptions::default(),
conn,
table: Cow::Borrowed(DEFAULT_MIGRATIONS_TABLE),
migrations: Vec::default(),
extensions: Arc::new(<TypeMap![Send + Sync]>::new()),
})
}
/// Use a connection from an existing connection pool.
///
/// **note**: A connection will be detached from the pool.
///
/// # Errors
///
/// An error is returned on connection failure.
pub async fn connect_with_pool(pool: &Pool<Db>) -> Result<Self, sqlx::Error> {
let mut conn = pool.acquire().await?;
conn.execute(
r#"--sql
SET client_min_messages TO WARNING;
"#,
)
.await?;
Ok(Self {
options: MigratorOptions::default(),
conn: conn.detach(),
table: Cow::Borrowed(DEFAULT_MIGRATIONS_TABLE),
migrations: Vec::default(),
extensions: Arc::new(<TypeMap![Send + Sync]>::new()),
})
}
/// Set the table name for migration bookkeeping to override the default [`DEFAULT_MIGRATIONS_TABLE`].
///
/// The table name is used as-is in queries, **DO NOT USE UNTRUSTED STRINGS**.
pub fn set_migrations_table(&mut self, name: impl AsRef<str>) {
self.table = Cow::Owned(name.as_ref().to_string());
}
/// Add migrations to the migrator.
pub fn add_migrations(&mut self, migrations: impl IntoIterator<Item = Migration<Db>>) {
self.migrations.extend(migrations);
}
/// Override the migrator's options.
pub fn set_options(&mut self, options: MigratorOptions) {
self.options = options;
}
/// With an extension that is available to the migrations.
pub fn with<T: Send + Sync + 'static>(&mut self, value: T) -> &mut Self {
self.set(value);
self
}
/// Add an extension that is available to the migrations.
pub fn set<T: Send + Sync + 'static>(&mut self, value: T) {
self.extensions.set(value);
}
/// List all local migrations.
///
/// To list all migrations, use [`Migrator::status`].
pub fn local_migrations(&self) -> &[Migration<Db>] {
&self.migrations
}
}
impl<Db> Migrator<Db>
where
Db: Database,
Db::Connection: db::Migrations,
for<'a> &'a mut Db::Connection: Executor<'a>,
{
/// Apply all migrations to the given version.
///
/// Migration versions start at 1 and migrations are ordered
/// the way they were added to the migrator.
///
/// # Errors
///
/// Whenever a migration fails, and error is returned and no database
/// changes will be made.
#[allow(clippy::missing_panics_doc)]
pub async fn migrate(mut self, target_version: u64) -> Result<MigrationSummary, Error> {
self.local_migration(target_version)?;
self.conn.ensure_migrations_table(&self.table).await?;
let db_migrations = self.conn.list_migrations(&self.table).await?;
self.check_migrations(&db_migrations)?;
let to_apply = self.migrations.iter();
let db_version = db_migrations.len() as _;
let mut conn = self.conn;
conn.execute("BEGIN").await?;
for (idx, mig) in to_apply.enumerate() {
let mig_version = idx as u64 + 1;
if mig_version > target_version {
break;
}
if mig_version <= db_version {
continue;
}
let start = Instant::now();
tracing::info!(
version = mig_version,
name = %mig.name,
"applying migration"
);
let hasher = Sha256::new();
// First we execute the migration with dummy queries,
// otherwise the checksum will depend on the data
// inside the database.
//
// This way we miss out on queries that depend on
// the database context.
// FIXME: detect this and warn the user.
let mut ctx = MigrationContext {
hash_only: true,
ext: self.extensions.clone(),
hasher,
conn,
};
(*mig.up)(&mut ctx)
.await
.map_err(|error| Error::Migration {
name: mig.name.clone(),
version: mig_version,
error,
})?;
let checksum = std::mem::take(&mut ctx.hasher).finalize().to_vec();
ctx.hash_only = false;
(*mig.up)(&mut ctx)
.await
.map_err(|error| Error::Migration {
name: mig.name.clone(),
version: mig_version,
error,
})?;
let execution_time = start.elapsed();
if self.options.verify_checksums {
if let Some(db_mig) = db_migrations.get(idx) {
if db_mig.checksum != checksum {
ctx.conn.execute("ROLLBACK").await?;
return Err(Error::ChecksumMismatch {
version: mig_version,
local_checksum: checksum.clone().into(),
db_checksum: db_mig.checksum.clone(),
});
}
}
}
ctx.conn
.add_migration(
&self.table,
AppliedMigration {
version: mig_version,
name: mig.name.clone(),
checksum: checksum.into(),
execution_time,
},
)
.await?;
conn = ctx.conn;
tracing::info!(
version = mig_version,
name = %mig.name,
execution_time = %humantime::Duration::from(execution_time),
"migration applied"
);
}
tracing::info!("committing changes");
conn.execute("COMMIT").await?;
Ok(MigrationSummary {
old_version: if db_migrations.is_empty() {
None
} else {
Some(db_migrations.len() as _)
},
new_version: Some(target_version.max(db_version)),
})
}
/// Apply all local migrations, if there are any.
///
/// # Errors
///
/// Uses [`Migrator::migrate`] internally, errors are propagated.
pub async fn migrate_all(self) -> Result<MigrationSummary, Error> {
if self.migrations.is_empty() {
return Ok(MigrationSummary {
new_version: None,
old_version: None,
});
}
let migrations = self.migrations.len() as _;
self.migrate(migrations).await
}
/// Revert all migrations after and including the given version.
///
/// Any migrations that are "not reversible" and have no revert functions will be ignored.
///
/// # Errors
///
/// Whenever a migration fails, and error is returned and no database
/// changes will be made.
#[allow(clippy::missing_panics_doc)]
pub async fn revert(mut self, target_version: u64) -> Result<MigrationSummary, Error> {
self.local_migration(target_version)?;
self.conn.ensure_migrations_table(&self.table).await?;
let db_migrations = self.conn.list_migrations(&self.table).await?;
self.check_migrations(&db_migrations)?;
let to_revert = self
.migrations
.iter()
.enumerate()
.skip_while(|(idx, _)| idx + 1 < target_version as _)
.take_while(|(idx, _)| *idx < db_migrations.len())
.collect::<Vec<_>>()
.into_iter()
.rev();
let mut conn = self.conn;
conn.execute("BEGIN").await?;
for (idx, mig) in to_revert {
let version = idx as u64 + 1;
let start = Instant::now();
tracing::info!(
version,
name = %mig.name,
"reverting migration"
);
let hasher = Sha256::new();
let mut ctx = MigrationContext {
hash_only: false,
ext: self.extensions.clone(),
hasher,
conn,
};
match &mig.down {
Some(down) => {
down(&mut ctx).await.map_err(|error| Error::Revert {
name: mig.name.clone(),
version,
error,
})?;
}
None => {
tracing::warn!(
version,
name = %mig.name,
"no down migration found"
);
}
}
let execution_time = start.elapsed();
ctx.conn.remove_migration(&self.table, version).await?;
conn = ctx.conn;
tracing::info!(
version,
name = %mig.name,
execution_time = %humantime::Duration::from(execution_time),
"migration reverted"
);
}
tracing::info!("committing changes");
conn.execute("COMMIT").await?;
Ok(MigrationSummary {
old_version: if db_migrations.is_empty() {
None
} else {
Some(db_migrations.len() as _)
},
new_version: if target_version == 1 {
None
} else {
Some(target_version - 1)
},
})
}
/// Revert all applied migrations, if any.
///
/// # Errors
///
/// Uses [`Migrator::revert`], any errors will be propagated.
pub async fn revert_all(self) -> Result<MigrationSummary, Error> {
self.revert(1).await
}
/// Forcibly set a given migration version in the database.
/// No migrations will be applied or reverted.
///
/// This function should be considered (almost) idempotent, and repeatedly calling it
/// should result in the same state. Some database-specific values can change, such as timestamps.
///
/// # Errors
///
/// The forced migration version must exist locally.
///
/// Connection and database errors are returned.
///
/// Truncating the migrations table and applying migrations are done
/// in separate transactions. As a consequence in some occasions
/// the migrations table might be cleared and no migrations will be set.
#[allow(clippy::missing_panics_doc)]
pub async fn force_version(mut self, version: u64) -> Result<MigrationSummary, Error> {
self.conn.ensure_migrations_table(&self.table).await?;
let db_migrations = self.conn.list_migrations(&self.table).await?;
if version == 0 {
self.conn.clear_migrations(&self.table).await?;
return Ok(MigrationSummary {
old_version: if db_migrations.is_empty() {
None
} else {
Some(db_migrations.len() as _)
},
new_version: None,
});
}
self.local_migration(version)?;
let migrations = self
.migrations
.iter()
.enumerate()
.take_while(|(idx, _)| *idx < version as usize);
self.conn.clear_migrations(&self.table).await?;
let mut conn = self.conn;
conn.execute("BEGIN").await?;
for (idx, mig) in migrations {
let mig_version = idx as u64 + 1;
let hasher = Sha256::new();
let mut ctx = MigrationContext {
hash_only: true,
ext: self.extensions.clone(),
hasher,
conn,
};
(*mig.up)(&mut ctx)
.await
.map_err(|error| Error::Migration {
name: mig.name.clone(),
version: mig_version,
error,
})?;
let checksum = std::mem::take(&mut ctx.hasher).finalize().to_vec();
ctx.conn
.add_migration(
&self.table,
AppliedMigration {
version: mig_version,
name: mig.name.clone(),
checksum: checksum.into(),
execution_time: Duration::default(),
},
)
.await?;
conn = ctx.conn;
tracing::info!(
version = idx + 1,
name = %mig.name,
"migration forcibly set as applied"
);
}
tracing::info!("committing changes");
conn.execute("COMMIT").await?;
Ok(MigrationSummary {
old_version: if db_migrations.is_empty() {
None
} else {
Some(db_migrations.len() as _)
},
new_version: Some(version),
})
}
/// Verify all the migrations.
///
/// # Errors
///
/// The following kind of errors can be returned:
///
/// - connection and database errors
/// - mismatch errors
///
/// Mismatch errors can happen if the local migrations'
/// name or checksum does not match the applied migration's.
///
/// Both name and checksum validation can be turned off via [`MigratorOptions`].
#[allow(clippy::missing_panics_doc)]
pub async fn verify(mut self) -> Result<(), Error> {
self.conn.ensure_migrations_table(&self.table).await?;
let migrations = self.conn.list_migrations(&self.table).await?;
self.check_migrations(&migrations)?;
if self.options.verify_checksums {
for res in self.verify_checksums(&migrations).await?.1 {
res?;
}
}
Ok(())
}
/// List all local and applied migrations.
///
/// # Errors
///
/// Errors are returned on connection and database errors.
/// The migrations themselves are not verified.
#[allow(clippy::missing_panics_doc)]
pub async fn status(mut self) -> Result<Vec<MigrationStatus>, Error> {
self.conn.ensure_migrations_table(&self.table).await?;
let migrations = self.conn.list_migrations(&self.table).await?;
let mut status = Vec::with_capacity(self.migrations.len());
let (migrator, checksums) = self.verify_checksums(&migrations).await?;
self = migrator;
for (idx, pair) in self.migrations.iter().zip_longest(migrations).enumerate() {
let version = idx as u64 + 1;
match pair {
EitherOrBoth::Both(local, db) => status.push(MigrationStatus {
version,
name: local.name.clone().into_owned(),
reversible: local.is_reversible(),
applied: Some(db),
missing_local: false,
checksum_ok: checksums.get(idx).map_or(true, Result::is_ok),
}),
EitherOrBoth::Left(local) => status.push(MigrationStatus {
version,
name: local.name.clone().into_owned(),
reversible: local.is_reversible(),
applied: None,
missing_local: false,
checksum_ok: checksums.get(idx).map_or(true, Result::is_ok),
}),
EitherOrBoth::Right(r) => status.push(MigrationStatus {
version: r.version,
name: r.name.clone().into_owned(),
reversible: false,
applied: Some(r),
missing_local: true,
checksum_ok: checksums.get(idx).map_or(true, Result::is_ok),
}),
}
}
Ok(status)
}
}
impl<Db> Migrator<Db>
where
Db: Database,
Db::Connection: db::Migrations,
for<'a> &'a mut Db::Connection: Executor<'a>,
{
fn local_migration(&self, version: u64) -> Result<&Migration<Db>, Error> {
if version == 0 {
return Err(Error::InvalidVersion {
version,
min_version: 1,
max_version: self.migrations.len() as _,
});
}
if self.migrations.is_empty() {
return Err(Error::InvalidVersion {
version,
min_version: 1,
max_version: self.migrations.len() as _,
});
}
let idx = version - 1;
self.migrations
.get(idx as usize)
.ok_or(Error::InvalidVersion {
version,
min_version: 1,
max_version: self.migrations.len() as _,
})
}
fn check_migrations(&mut self, migrations: &[AppliedMigration<'_>]) -> Result<(), Error> {
if self.migrations.len() < migrations.len() {
return Err(Error::MissingMigrations {
local_count: self.migrations.len(),
db_count: migrations.len(),
});
}
for (idx, (db_migration, local_migration)) in
migrations.iter().zip(self.migrations.iter()).enumerate()
{
let version = idx as u64 + 1;
if self.options.verify_names && db_migration.name != local_migration.name {
return Err(Error::NameMismatch {
version,
local_name: local_migration.name.clone(),
db_name: db_migration.name.to_string().into(),
});
}
}
Ok(())
}
async fn verify_checksums(
mut self,
migrations: &[AppliedMigration<'_>],
) -> Result<(Self, Vec<Result<(), Error>>), Error> {
let mut results = Vec::with_capacity(self.migrations.len());
let local_migrations = self.migrations.iter();
let mut conn = self.conn;
for (idx, mig) in local_migrations.enumerate() {
let mig_version = idx as u64 + 1;
let hasher = Sha256::new();
let mut ctx = MigrationContext {
hash_only: true,
ext: self.extensions.clone(),
hasher,
conn,
};
(*mig.up)(&mut ctx)
.await
.map_err(|error| Error::Migration {
name: mig.name.clone(),
version: mig_version,
error,
})?;
let checksum = std::mem::take(&mut ctx.hasher).finalize().to_vec();
conn = ctx.conn;
if let Some(db_mig) = migrations.get(idx) {
if db_mig.checksum == checksum {
results.push(Ok(()));
} else {
results.push(Err(Error::ChecksumMismatch {
version: mig_version,
local_checksum: checksum.clone().into(),
db_checksum: db_mig.checksum.clone().into_owned().into(),
}));
}
}
}
conn.execute("ROLLBACK").await?;
self.conn = conn;
Ok((self, results))
}
}
/// Options for a [`Migrator`].
#[derive(Debug)]
pub struct MigratorOptions {
/// Whether to check applied migration checksums.
pub verify_checksums: bool,
/// Whether to check applied migration names.
pub verify_names: bool,
}
impl Default for MigratorOptions {
fn default() -> Self {
Self {
verify_checksums: true,
verify_names: true,
}
}
}
/// Summary of a migration or revert operation.
#[derive(Debug, Clone)]
pub struct MigrationSummary {
/// The old migration version in the database.
pub old_version: Option<u64>,
/// The new migration version in the database.
pub new_version: Option<u64>,
}
/// Status of a migration.
#[derive(Debug, Clone)]
pub struct MigrationStatus {
/// Migration version determined by migration order.
pub version: u64,
/// The name of the migration.
pub name: String,
/// Whether the migration has a reverse function.
pub reversible: bool,
/// Information about the migration in the database.
pub applied: Option<db::AppliedMigration<'static>>,
/// Whether the migration is found in the database,
/// but missing locally.
pub missing_local: bool,
/// Whether the checksum matches the database checksum.
pub checksum_ok: bool,
}
/// An opaque error type returned by user-provided migration functions.
///
/// Currently [`anyhow::Error`] is used, but it should be considered an implementation detail.
pub type MigrationError = anyhow::Error;
/// An `SQLx` database type, used for code generation purposes.
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum DatabaseType {
Postgres,
Sqlite,
Any,
}
impl DatabaseType {
fn sqlx_type(self) -> &'static str {
match self {
DatabaseType::Postgres => "Postgres",
DatabaseType::Sqlite => "Sqlite",
DatabaseType::Any => "Any",
}
}
}
impl FromStr for DatabaseType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"postgres" => Ok(Self::Postgres),
"sqlite" => Ok(Self::Sqlite),
"any" => Ok(Self::Any),
db => Err(anyhow::anyhow!("invalid database type `{}`", db)),
}
}
}