pub struct PgMigrationRunner { /* private fields */ }Expand description
Runs schema migrations against a PostgreSQL database.
Tracks which migrations have been applied in a schema_versions table and
only executes those that have not yet been applied. Migrations are executed
in version order inside transactions.
§Concurrency safety
Uses LOCK TABLE schema_versions IN EXCLUSIVE MODE within transactions to
prevent concurrent migration runners from applying the same migration twice.
Implementations§
Source§impl PgMigrationRunner
impl PgMigrationRunner
Sourcepub fn new(pool: Pool<Postgres>) -> PgMigrationRunner
pub fn new(pool: Pool<Postgres>) -> PgMigrationRunner
Creates a new runner with the built-in migrations.
Sourcepub const fn with_migrations(
pool: Pool<Postgres>,
migrations: &'static [PgMigration],
) -> PgMigrationRunner
pub const fn with_migrations( pool: Pool<Postgres>, migrations: &'static [PgMigration], ) -> PgMigrationRunner
Creates a new runner with a custom set of migrations.
Sourcepub async fn current_version(&self) -> Result<u32, Error>
pub async fn current_version(&self) -> Result<u32, Error>
Returns the highest migration version that has been applied, or 0 if
no migrations have been applied yet.
§Errors
Returns an error if the database cannot be queried.
Sourcepub async fn pending_migrations(&self) -> Result<Vec<&PgMigration>, Error>
pub async fn pending_migrations(&self) -> Result<Vec<&PgMigration>, Error>
Returns the list of migrations that have not yet been applied.
§Errors
Returns an error if the current version cannot be determined.
Sourcepub async fn run_pending(&self) -> Result<Vec<u32>, Error>
pub async fn run_pending(&self) -> Result<Vec<u32>, Error>
Applies all pending migrations in version order.
Each migration runs inside its own transaction with an exclusive lock on
the schema_versions table to prevent concurrent application. If a
migration fails, the transaction is rolled back and the error is returned.
Returns the list of version numbers that were applied.
§Errors
Returns an error if any migration fails to apply.
Trait Implementations§
Source§impl Clone for PgMigrationRunner
impl Clone for PgMigrationRunner
Source§fn clone(&self) -> PgMigrationRunner
fn clone(&self) -> PgMigrationRunner
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PgMigrationRunner
impl !RefUnwindSafe for PgMigrationRunner
impl Send for PgMigrationRunner
impl Sync for PgMigrationRunner
impl Unpin for PgMigrationRunner
impl UnsafeUnpin for PgMigrationRunner
impl !UnwindSafe for PgMigrationRunner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request