Crates.io | migrations |
lib.rs | migrations |
version | 0.2.2 |
source | src |
created_at | 2024-06-25 23:59:53.678381 |
updated_at | 2024-06-26 17:58:41.940241 |
description | A small library to handle migrations, with built in support for libsql. |
homepage | |
repository | https://gitlab.com/magicfoodhand/migrations |
max_upload_size | |
id | 1283979 |
size | 28,693 |
cargo add migrations
files
featureChangesetStores::files
ChangesetStores::sql
Migrations::libsql
, MigrationStores::libsql
use migrations::Migrations;
fn main() {
let db = async_std::task::block_on(Builder::new_local(&db_identifier.as_str()).build()).unwrap();
let connection = db.connect().unwrap();
/**
* The migrations folder should contain a set of folders that match: YYYYMMDDHHMMSS_<\w+>
* Each migration is stored in its own folder, with two files up.sql and down.sql.
**/
let migrations = Migrations::libsql(connection, std::env::current_dir().unwrap().join("migrations"));
migrations.setup().unwrap();
migrations.migrate().unwrap();
// rollback only the last migration
migrations.rollback().unwrap();
// rollback all migrations
migrations.reset().unwrap();
}
Migrations are based on the following internal concepts:
Contains the changesets to be run, based on two traits Changeset
and ChangesetStore
.
pub trait ChangesetStore {
fn create_changeset(&self, name: String) -> anyhow::Result<Box<dyn Changeset + '_>>;
fn get_changesets(&self) -> anyhow::Result<Vec<Box<dyn Changeset>>>;
fn clone(&self) -> Box<dyn ChangesetStore + '_>;
}
pub trait Changeset {
fn identifier(&self) -> Identifier;
fn apply(&self, store: &dyn MigrationStore) -> anyhow::Result<()>;
fn rollback(&self, store: &dyn MigrationStore) -> anyhow::Result<()>;
fn duplicate(&self) -> Box<dyn Changeset>;
}
Allows a file to be used by a MigrationStore, such as a database running a sql file.
pub trait FileActions {
fn run(&self, file: &PathBuf) -> anyhow::Result<()>;
}
Runs the changesets, based on the MigrationStore
traits.
pub trait MigrationStore {
fn is_ready(&self) -> anyhow::Result<bool>;
fn setup(&self) -> anyhow::Result<()>;
fn is_applied(&self, identifier: &Identifier) -> bool;
fn apply(&mut self, changeset: &Box<dyn Changeset>) -> anyhow::Result<()>;
fn rollback(&mut self, changeset: &Box<dyn Changeset>) -> anyhow::Result<()>;
fn clone(&self) -> Box<dyn MigrationStore + '_>;
fn file_actions(&self) -> Option<&dyn FileActions> {
None
}
}