chore: add basic example

This commit is contained in:
Dmitriy Pleshevskiy 2021-06-13 01:25:45 +03:00
parent 9552ece678
commit c766042262
3 changed files with 70 additions and 2 deletions

View File

@ -32,9 +32,40 @@ authors = ["Me <user@rust-lang.org>"]
migra = { version = "1.0", features = ["postgres"] }
```
### Usage
## Basic usage
For more information about the crate, please read doc.
**Note:** This example requires to enable `sqlite` feature.
```rust
use migra::clients::{OpenDatabaseConnection, SqliteClient};
use migra::managers::{ManageTransaction, ManageMigrations};
fn main() -> migra::Result<()> {
let mut client = SqliteClient::new("./tasks.db")?;
client.create_migrations_table()?;
let mut migrations = client.get_applied_migrations()?;
client
.begin_transaction()
.and_then(|_| {
migrations.should_run_upgrade_migration(
&mut client,
"20210615_initial_migration",
r#"CREATE TABLE IF NOT EXISTS tasks (
title TEXT NOT NULL
);"#,
)?;
Ok(())
})
.and_then(|res| client.commit_transaction().and(Ok(res)))
.or_else(|err| client.rollback_transaction().and(Err(err)));
Ok(())
}
```
### Supported databases

View File

@ -26,6 +26,41 @@
//! migra = { version = "1.0", features = ["postgres"] }
//! ```
//!
//! ## Basic usage
//!
//! **Note:** This example requires to enable `sqlite` feature.
//!
//! ```rust
//! use migra::clients::{OpenDatabaseConnection, SqliteClient};
//! use migra::managers::{ManageTransaction, ManageMigrations};
//!
//! fn main() -> migra::Result<()> {
//! let mut client = SqliteClient::new("./tasks.db")?;
//!
//! client.create_migrations_table()?;
//!
//! let mut migrations = client.get_applied_migrations()?;
//!
//! client
//! .begin_transaction()
//! .and_then(|_| {
//! migrations.should_run_upgrade_migration(
//! &mut client,
//! "20210615_initial_migration",
//! r#"CREATE TABLE IF NOT EXISTS tasks (
//! title TEXT NOT NULL
//! );"#,
//! )?;
//!
//! Ok(())
//! })
//! .and_then(|res| client.commit_transaction().and(Ok(res)))
//! .or_else(|err| client.rollback_transaction().and(Err(err)));
//!
//! Ok(())
//! }
//! ```
//!
//! ### Supported databases
//!
//! | Database Client | Feature |

View File

@ -109,6 +109,7 @@ impl List {
/// # Example
///
/// ```rust
/// # use migra::migration::List;
/// # let mut list = List::new();
/// list.push_name("name");
/// # assert_eq!(list, List::from(vec!["name"]));
@ -116,6 +117,7 @@ impl List {
///
/// Is identical to the following
/// ```rust
/// # use migra::migration::{List, Migration};
/// # let mut list = List::new();
/// list.push(Migration::new("name"));
/// # assert_eq!(list, List::from(vec!["name"]));