Linted code.
This commit is contained in:
1 parent
894f969d0f
commit
174d7e855a
17 files changed
+304
-206
No files matched your search
+15
-10
@@ -1,7 +1,7 @@
|
||||
use std::time::Instant;
|
||||
use resource::resource_str;
|
||||
use rusqlite::{Connection, Transaction};
|
||||
use sha2::{Digest, Sha256};
|
||||
use resource::{resource_str};
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum IntegrityError {
|
||||
@@ -33,10 +33,10 @@ fn get_id_of_new_migration(tx: &Transaction) -> Result<usize, rusqlite::Error> {
|
||||
return tx.query_row(q, (), |row| row.get::<usize, usize>(0));
|
||||
}
|
||||
|
||||
|
||||
pub fn migrate(conn: &mut Connection, migrations: &Vec<String>) -> Result<(), Error> {
|
||||
let tx = conn.transaction().map_err(|e| Error::Sql(e))?;
|
||||
tx.execute(INITIAL_MIGRATION, ()).map_err(|e| Error::Sql(e))?;
|
||||
tx.execute(INITIAL_MIGRATION, ())
|
||||
.map_err(|e| Error::Sql(e))?;
|
||||
let id = get_id_of_new_migration(&tx).map_err(|e| Error::Sql(e))?;
|
||||
|
||||
for (i, migration) in migrations.iter().enumerate() {
|
||||
@@ -56,13 +56,13 @@ fn apply_migration(tx: &Transaction, id: usize, migration: &String) -> Result<()
|
||||
let q = "INSERT INTO shitty_migrations(id, sha256) VALUES (?1, ?2)";
|
||||
tx.execute(q, (&id, &hash)).map_err(|e| Error::Sql(e))?;
|
||||
|
||||
|
||||
let now = Instant::now();
|
||||
tx.execute_batch(migration).map_err(|e| Error::Sql(e))?;
|
||||
let elapsed_time = now.elapsed();
|
||||
|
||||
let q = "UPDATE shitty_migrations SET duration = ?1 WHERE id = ?2";
|
||||
tx.execute(q, (elapsed_time.as_secs_f32(), &id)).map_err(|e| Error::Sql(e))?;
|
||||
tx.execute(q, (elapsed_time.as_secs_f32(), &id))
|
||||
.map_err(|e| Error::Sql(e))?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
@@ -76,7 +76,9 @@ fn compute_migration_sha256(migration: &String) -> String {
|
||||
|
||||
fn check_migration(tx: &Transaction, id: usize, migration: &String) -> Result<(), Error> {
|
||||
let q = "SELECT sha256 FROM shitty_migrations WHERE id = ?1";
|
||||
let res = tx.query_row(q, (&id, ), |row| row.get::<usize, String>(0)).map_err(|e| Error::Sql(e))?;
|
||||
let res = tx
|
||||
.query_row(q, (&id,), |row| row.get::<usize, String>(0))
|
||||
.map_err(|e| Error::Sql(e))?;
|
||||
let expected = compute_migration_sha256(migration);
|
||||
if !res.eq(&expected) {
|
||||
return Err(Error::Integrity(IntegrityError::MigrationCheckFailed {
|
||||
@@ -91,11 +93,14 @@ fn check_migration(tx: &Transaction, id: usize, migration: &String) -> Result<()
|
||||
|
||||
fn get_migrations() -> Vec<String> {
|
||||
let mut ret = Vec::new();
|
||||
ret.push(resource_str!("migrations/sql/create_tables_001.sql").as_ref().to_string());
|
||||
ret.push(
|
||||
resource_str!("migrations/sql/create_tables_001.sql")
|
||||
.as_ref()
|
||||
.to_string(),
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
pub fn apply_migrations(conn: &mut Connection) -> Result<(), Error> {
|
||||
let migrations = get_migrations();
|
||||
return migrate(conn, &migrations);
|
||||
@@ -155,4 +160,4 @@ mod test_migration {
|
||||
assert!(ret.is_err());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user