use welds::errors::Result;
use welds::migrations::prelude::*;
#[async_std::main]
async fn main() -> Result<()> {
pretty_env_logger::init();
let client = welds::connections::connect("sqlite::memory:").await?;
up(
&client,
&[
create_peoples_table,
create_addresses_table,
add_address_to_people,
rename_and_make_nullable,
],
)
.await?;
println!("Migrate Up Complete");
let downed = down_last(&client).await?;
println!("Migrate Down Complete");
println!("Rollback: {}", downed.unwrap());
Ok(())
}
fn create_peoples_table(_: &TableState) -> Result<MigrationStep> {
let m = create_table("people")
.id(|c| c("id", Type::Int))
.column(|c| c("name", Type::String).create_unique_index());
Ok(MigrationStep::new("create_peoples_table", m))
}
fn create_addresses_table(_: &TableState) -> Result<MigrationStep> {
let m = create_table("addresses")
.id(|c| c("id", Type::Int))
.column(|c| c("name", Type::String).create_unique_index())
.column(|c| c("finger_count", Type::IntSmall));
Ok(MigrationStep::new("create_addresses_table", m))
}
fn add_address_to_people(state: &TableState) -> Result<MigrationStep> {
let alter = change_table(state, "people")?;
let m = alter.add_column("aaddress_id", Type::Int);
Ok(MigrationStep::new("add_address_to_people", m))
}
fn rename_and_make_nullable(state: &TableState) -> Result<MigrationStep> {
let alter = change_table(state, "people")?;
let m = alter.change("aaddress_id").null().rename("address_id");
Ok(MigrationStep::new("rename_and_make_nullable", m))
}