From 60c3ece6710549f48a32f8b2760614ed823df317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Th=C3=A9riault?= Date: Thu, 23 Jul 2020 12:57:48 -0400 Subject: [PATCH] Edit Migration and Migrator to make embedded migrations possible --- sqlx-core/src/migrate/migration.rs | 18 ++++-------------- sqlx-core/src/migrate/migrator.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/sqlx-core/src/migrate/migration.rs b/sqlx-core/src/migrate/migration.rs index 52396a11..0aec8ffb 100644 --- a/sqlx-core/src/migrate/migration.rs +++ b/sqlx-core/src/migrate/migration.rs @@ -2,18 +2,8 @@ use std::borrow::Cow; #[derive(Debug, Clone)] pub struct Migration { - pub(crate) version: i64, - pub(crate) description: Cow<'static, str>, - pub(crate) sql: Cow<'static, str>, - pub(crate) checksum: Cow<'static, [u8]>, -} - -impl Migration { - pub fn version(&self) -> i64 { - self.version - } - - pub fn description(&self) -> &str { - &*self.description - } + pub version: i64, + pub description: Cow<'static, str>, + pub sql: Cow<'static, str>, + pub checksum: Cow<'static, [u8]>, } diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index ba602a38..33491e4e 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -1,11 +1,12 @@ use crate::acquire::Acquire; use crate::migrate::{Migrate, MigrateError, Migration, MigrationSource}; +use std::borrow::Cow; use std::ops::Deref; use std::slice; #[derive(Debug)] pub struct Migrator { - migrations: Vec, + migrations: Cow<'static, [Migration]>, } impl Migrator { @@ -31,10 +32,17 @@ impl Migrator { S: MigrationSource<'s>, { Ok(Self { - migrations: source.resolve().await.map_err(MigrateError::Source)?, + migrations: Cow::Owned(source.resolve().await.map_err(MigrateError::Source)?), }) } + /// Creates a new instance from a static slice of migrations. + pub async fn from_static(migrations: &'static [Migration]) -> Self { + Self { + migrations: Cow::Borrowed(migrations), + } + } + /// Get an iterator over all known migrations. pub fn iter(&self) -> slice::Iter<'_, Migration> { self.migrations.iter()