pub fn read_migration_files(path: &Path) -> Result<Migrations> {
    let mut btreemap: Migrations = BTreeMap::new();

    for entry in fs::read_dir(path).chain_err(|| format!("Failed to open {:?}", path))? {
        let entry = entry.unwrap();
        // Will panic on invalid unicode in filename, unlikely (heh)
        let info = match parse_filename(entry.file_name().to_str().unwrap()) {
            Ok(info) => info,
            Err(_) => continue,
        };
        let mut file = File::open(entry.path())
            .chain_err(|| format!("Failed to open {:?}", entry.path()))?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;

        let migration_file = MigrationFile { content: Some(content), ..info };
        let migration_number = migration_file.number;
        let mut migration = match btreemap.remove(&migration_number) {
            None => Migration { up: None, down: None },
            Some(m) => m
        };
        if migration_file.direction == Direction::Up {
            migration.up = Some(migration_file);
        } else {
            migration.down = Some(migration_file);
        }
        btreemap.insert(migration_number, migration);
    }

    // Let's check the all the files we need now
    let mut index = 1;
    for (number, migration) in &btreemap {
        if index != *number {
            bail!("Files for migration {} are missing", index);
        }
        if migration.up.is_none() || migration.down.is_none() {
            bail!("Migration {} is missing its up or down file", index);
        }
        index += 1;
    }
    Ok(btreemap)
}
/// Read the path given and read all the migration files, pairing them by migration
/// number and checking for errors along the way
Read the path given and read all the migration files, pairing them by migration
number and checking for errors along the way
Read the path given and read all the migration files pairing them by migration number and checking for errors along the way
pub fn read_migration_files(path: &Path) -> Result<Migrations> {
    let mut btreemap: Migrations = BTreeMap::new();
    for entry in fs::read_dir(path).chain_err(|| format!("Failed to open {:?}", path))? {
        let entry = entry.unwrap();
        // Will panic on invalid unicode in filename, unlikely (heh)
        let info = match parse_filename(entry.file_name().to_str().unwrap()) {
            Ok(info) => info,
            Err(_) => continue,
        };
        let mut file = File::open(entry.path())
            .chain_err(|| format!("Failed to open {:?}", entry.path()))?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        let migration_file = MigrationFile { content: Some(content), ..info };
        let migration_number = migration_file.number;
        let mut migration = match btreemap.remove(&migration_number) {
            None => Migration { up: None, down: None },
            Some(m) => m
        };
        if migration_file.direction == Direction::Up {
            migration.up = Some(migration_file);
        } else {
            migration.down = Some(migration_file);
        }
        btreemap.insert(migration_number, migration);
    }
    // Let's check the all the files we need now
    let mut index = 1;
    for (number, migration) in &btreemap {
        if index != *number {
            bail!("Files for migration {} are missing", index);
        }
        if migration.up.is_none() || migration.down.is_none() {
            bail!("Migration {} is missing its up or down file", index);
        }
        index += 1;
    }
    Ok(btreemap)
}
pub fn read_migration_files ( path : & Path ) -> Result < Migrations > { let mut btreemap : Migrations = BTreeMap :: new ( ) ; for entry in fs :: read_dir ( path ) . chain_err ( | | format ! ( " " path ) ) ? { let entry = entry . unwrap ( ) ; // Will panic on invalid unicode in filename, unlikely (heh) let info = match parse_filename ( entry . file_name ( ) . to_str ( ) . unwrap ( ) ) { Ok ( info ) => info , Err ( _ ) => continue , } ; let mut file = File :: open ( entry . path ( ) ) . chain_err ( | | format ! ( " " entry path ( ) ) ) ? ; let mut content = String :: new ( ) ; file . read_to_string ( & mut content ) ? ; let migration_file = MigrationFile { content : Some ( content ) , .. info } ; let migration_number = migration_file . number ; let mut migration = match btreemap . remove ( & migration_number ) { None => Migration { up : None , down : None } , Some ( m ) => m } ; if migration_file . direction == Direction :: Up { migration . up = Some ( migration_file ) ; } else { migration . down = Some ( migration_file ) ; } btreemap . insert ( migration_number , migration ) ; } // Let's check the all the files we need now let mut index = 1 ; for ( number , migration ) in & btreemap { if index != * number { bail ! ( " " index ) ; } if migration . up . is_none ( ) || migration . down . is_none ( ) { bail ! ( " " index ) ; } index += 1 ; } Ok ( btreemap ) }
Read the path given and read all the migration files, pairing them by migration
number and checking for errors along the way
Read the path given and read all the migration files pairing them by migration number and checking for errors along the way
path
&Path