#migration #postgresql #repo #ui #progress

promad

Migration tool for SQLx

2 releases

0.1.1 Jun 9, 2023
0.1.0 Jun 6, 2023
Download history 27/week @ 2023-06-02 62/week @ 2023-06-09 48/week @ 2023-06-16 60/week @ 2023-06-23 50/week @ 2023-06-30 112/week @ 2023-07-07 56/week @ 2023-07-14 44/week @ 2023-07-21 59/week @ 2023-07-28 35/week @ 2023-08-04 71/week @ 2023-08-11 70/week @ 2023-08-18 61/week @ 2023-08-25 38/week @ 2023-09-01 42/week @ 2023-09-08 53/week @ 2023-09-15

209 downloads per month

Apache-2.0

1.5MB
729 lines

Promad

Promad logo

Migration tool for SQLx that allows for arbitrary SQL/Rust execution within a transaction on the write connection.

Features

  • Rust code mixed with SQL in migrations.
  • Scan migrations across a table with blob data using Rust.
  • Embeddable CLI.

Example

use std::borrow::Cow;
use async_trait::async_trait;
use promad::{file_basename, Migration, Migrator, error::{Error, Result}};
use sqlx::{postgres::PgPoolOptions, PgPool, Postgres, Executor, Row};
use testcontainers::{clients, Container};

pub struct FirstMigration;

#[async_trait]
impl Migration<Postgres> for FirstMigration {
    fn name(&self) -> &'static str {
        file_basename!()
    }

    async fn up(
        &self,
        _read: &mut sqlx::PgConnection,
        write: &mut sqlx::PgConnection,
    ) -> Result<()> {
        sqlx::query("CREATE TABLE test (id INT PRIMARY KEY)")
            .execute(write)
            .await?;
        Ok(())
    }

    async fn down(
        &self,
        _read: &mut sqlx::PgConnection,
        write: &mut sqlx::PgConnection,
    ) -> Result<()> {
        sqlx::query("DROP TABLE test")
            .execute(write)
            .await?;
        Ok(())
    }
}

let docker = clients::Cli::default();
let pgsql = docker.run(testcontainers::images::postgres::Postgres::default());
let port = pgsql.get_host_port_ipv4(5432);
let pool = PgPoolOptions::new()
    .connect(&format!(
        "postgres://postgres:postgres@localhost:{}/postgres",
        port
    ))
    .await.unwrap();

let mut migrator = Migrator::create(pool.clone());
migrator.add_migration(Box::new(FirstMigration));
migrator.apply_all().await.unwrap();

// Check that the table exists.
let mut conn = pool.acquire().await.unwrap();

let row = sqlx::query("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'test')")
    .fetch_one(&mut conn)
    .await.unwrap();

assert!(row.get::<bool, _>(0));

migrator.revert_all().await.unwrap();

let row = sqlx::query("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'test')")
    .fetch_one(&mut conn)
    .await.unwrap();

assert!(!row.get::<bool, _>(0));

Dependencies

~17–57MB
~1M SLoC