ockam icon indicating copy to clipboard operation
ockam copied to clipboard

Improve database migrations to pair sql and rust migration code

Open adrianbenavides opened this issue 1 year ago • 0 comments

Current behavior

The current implementation runs all the sql migrations and then the rust migrations for post-migration code. We should refactor this function so that we can run a pair of sql+rust migrations at a time.

Desired behavior

The sqlx::migrate!(); macro returns a Migrator instance that returns the migration names (name+version given in the file name). The Migrator struct is pretty limited and doesn't allow running migrations one by one.

We should consider writing a wrapper around this struct to have more control over migrations and add the functionality to run arbitrary rust code given a migration name.

// within a function like 
// https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/migrate/migrator.rs#L118
let rust_migrator = RustMigrator::new(..);
for migration in self.iter() {
    // ..
    conn.apply(migration).await?;
    rust_migrator.apply(migration).await?;
}

adrianbenavides avatar Jan 02 '24 13:01 adrianbenavides