Skip to content

Commit

Permalink
Fix check db schema version
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeRosa committed Mar 17, 2023
1 parent ce80da7 commit d5e0a8b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/event-db/src/lib.rs
Expand Up @@ -20,7 +20,7 @@ const DATABASE_URL_ENVVAR: &str = "EVENT_DB_URL";

/// Database version this crate matches.
/// Must equal the last Migrations Version Number.
pub const DATABASE_SCHEMA_VERSION: u32 = 10;
pub const DATABASE_SCHEMA_VERSION: i32 = 9;

#[allow(unused)]
/// Connection to the Election Database
Expand Down Expand Up @@ -52,12 +52,6 @@ pub struct EventDB {
///
/// The env var "`DATABASE_URL`" can be set directly as an anv var, or in a
/// `.env` file.
///
/// # Examples
///
/// ```
/// let db = election_db::establish_connection(None)?;
/// ```
pub async fn establish_connection(
url: Option<&str>,
) -> Result<EventDB, Box<dyn Error + Send + Sync + 'static>> {
Expand Down
10 changes: 5 additions & 5 deletions src/event-db/src/schema_check.rs
Expand Up @@ -10,9 +10,9 @@ use crate::{EventDB, DATABASE_SCHEMA_VERSION};
#[derive(Debug)]
struct MismatchedSchema {
/// The current schema version.
was: u32,
was: i32,
/// The schema version we expected.
expected: u32,
expected: i32,
}

impl Error for MismatchedSchema {}
Expand All @@ -35,19 +35,19 @@ pub trait SchemaVersion {
/// Check the schema version.
/// return the current schema version if its current.
/// Otherwise return an error.
async fn schema_version_check(&self) -> Result<u32, Box<dyn Error + Send + Sync + 'static>>;
async fn schema_version_check(&self) -> Result<i32, Box<dyn Error + Send + Sync + 'static>>;
}

#[async_trait]
impl SchemaVersion for EventDB {
async fn schema_version_check(&self) -> Result<u32, Box<dyn Error + Send + Sync + 'static>> {
async fn schema_version_check(&self) -> Result<i32, Box<dyn Error + Send + Sync + 'static>> {
let conn = self.pool.get().await?;

let schema_check = conn
.query_one("SELECT MAX(version) from refinery_schema_history;", &[])
.await?;

let current_ver = schema_check.try_get::<usize, u32>(0)?;
let current_ver = schema_check.try_get::<usize, i32>(0)?;

if current_ver == DATABASE_SCHEMA_VERSION {
Ok(current_ver)
Expand Down

0 comments on commit d5e0a8b

Please sign in to comment.