Struct trek::migration_index::MigrationIndex [] [src]

pub struct MigrationIndex { /* fields omitted */ }

Tracks and manages database migrations for this system.

Methods

impl MigrationIndex
[src]

[src]

Wrap the given Migrations list into a new MigrationIndex.

[src]

Runs all database migrations that haven't yet been applied to the database.

Failures

Returns an error if a problem occurred when communicating with the database.

Examples

let connection = Connection::connect("server url", TlsMode::None).unwrap();
let transaction = connection.transaction().unwrap();

let migrations = MigrationIndex::new(migration_list);
match migrations.run(&transaction) {
    Ok(_) => {
        try!(transaction.commit());
        println!("All outstanding database migrations have been applied.");
    },
    Err(error) => {
        // note: no need to manually roll back the transaction, it'll automatically roll
        // back when the transaction variable goes out of scope

        println!("Error updating database structure: {}", error);
    }
}

[src]

Rolls back the last database migration that was successfully applied to the database.

Failures

Returns an error if a problem occurred when communicating with the database.

Examples

let connection = Connection::connect("server url", TlsMode::None).unwrap();
let transaction = connection.transaction().unwrap();

let migrations = MigrationIndex::new(migration_list);
match migrations.rollback(&transaction) {
    Ok(_) => {
        try!(transaction.commit());
        println!("Rollback of latest migration complete.");
    },
    Err(error) => {
        // note: no need to manually roll back the transaction, it'll automatically roll
        // back when the transaction variable goes out of scope

        println!("Error error rolling back last applied migration: {}", error);
    }
}

[src]

Takes a queryable connection object and returns the current version of the database's schema. No changes are made to the database.

Panics

If the schema_version table that Trek uses to save a schema's current version has multiple columns then this method will panic. It's expected that the schema_version table has only a single column named after the last applied migration.

Examples

let connection = Connection::connect("server url", TlsMode::None).unwrap();

match MigrationIndex::schema_version(&connection) {
    Ok(result_option) => {
        match result_option {
            Some(name) => println!("Current database version is: {}", name),
            None => println!("Database is empty, no migrations applied yet.")
        };
    },
    Err(error) => {
        println!("Error fetching schema version: {}", error);
    }
};