Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Revert changes to prepare_db.sh
Browse files Browse the repository at this point in the history
With patch b899831 we modified the
`prepare_db.sh` script in order to be able to define `sqlx` queries
under `cfg(test)`.

This was convenient, but it turns out that the changes cause the
script to trigger _complete_ recompilation of the project every time
we run it.

There may be an alternative, but for the time being we revert this
change. The effect is very minor: we move some test queries to a
`sqlx_test_utils` annotated with `#[allow(dead_code)]`.
  • Loading branch information
luckysori committed May 20, 2022
1 parent 2182a39 commit 117a360
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 61 deletions.
2 changes: 1 addition & 1 deletion daemon/prepare_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ trap 'rm -f $TEMPDB' EXIT
DATABASE_URL=sqlite:$TEMPDB cargo sqlx migrate run

# prepare the sqlx-data.json rust mappings
DATABASE_URL=sqlite:./$DAEMON_DIR/$TEMPDB SQLX_OFFLINE=true cargo sqlx prepare --merged -- --all-targets
DATABASE_URL=sqlite:./$DAEMON_DIR/$TEMPDB SQLX_OFFLINE=true cargo sqlx prepare
129 changes: 69 additions & 60 deletions daemon/src/db/time_to_first_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,79 @@ impl Connection {
}
}

// We cannot hide this under the `test` compilation flag because it
// makes it much less convenient to call `cargo sqlx prepare`.
#[allow(dead_code)]
mod sqlx_test_utils {
use super::*;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;

pub(crate) async fn load_first_seen_timestamp(
conn: &mut PoolConnection<Sqlite>,
taker_id: Identity,
) -> Result<Option<OffsetDateTime>> {
let row = sqlx::query!(
r#"
SELECT
first_seen_timestamp
FROM
time_to_first_position
WHERE
taker_id = $1
"#,
taker_id
)
.fetch_optional(&mut *conn)
.await?;

let timestamp = row
.map(|row| {
row.first_seen_timestamp
.map(OffsetDateTime::from_unix_timestamp)
})
.unwrap_or(None)
.transpose()?;

Ok(timestamp)
}

pub(crate) async fn load_first_position_timestamp(
conn: &mut PoolConnection<Sqlite>,
taker_id: Identity,
) -> Result<Option<OffsetDateTime>> {
let row = sqlx::query!(
r#"
SELECT
first_position_timestamp
FROM
time_to_first_position
WHERE
taker_id = $1
"#,
taker_id
)
.fetch_optional(&mut *conn)
.await?;

let timestamp = row
.map(|row| {
row.first_position_timestamp
.map(OffsetDateTime::from_unix_timestamp)
})
.unwrap_or(None)
.transpose()?;

Ok(timestamp)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::db::memory;
use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use sqlx_test_utils::load_first_position_timestamp;
use sqlx_test_utils::load_first_seen_timestamp;

#[tokio::test]
async fn given_inserted_first_seen_when_trying_to_insert_second_seen_then_timestamp_does_not_change(
Expand Down Expand Up @@ -141,64 +208,6 @@ mod tests {
assert_ne!(Some(second_inserted_timestamp), second_loaded_timestamp);
}

async fn load_first_seen_timestamp(
conn: &mut PoolConnection<Sqlite>,
taker_id: Identity,
) -> Result<Option<OffsetDateTime>> {
let row = sqlx::query!(
r#"
SELECT
first_seen_timestamp
FROM
time_to_first_position
WHERE
taker_id = $1
"#,
taker_id
)
.fetch_optional(&mut *conn)
.await?;

let timestamp = row
.map(|row| {
row.first_seen_timestamp
.map(OffsetDateTime::from_unix_timestamp)
})
.unwrap_or(None)
.transpose()?;

Ok(timestamp)
}

async fn load_first_position_timestamp(
conn: &mut PoolConnection<Sqlite>,
taker_id: Identity,
) -> Result<Option<OffsetDateTime>> {
let row = sqlx::query!(
r#"
SELECT
first_position_timestamp
FROM
time_to_first_position
WHERE
taker_id = $1
"#,
taker_id
)
.fetch_optional(&mut *conn)
.await?;

let timestamp = row
.map(|row| {
row.first_position_timestamp
.map(OffsetDateTime::from_unix_timestamp)
})
.unwrap_or(None)
.transpose()?;

Ok(timestamp)
}

fn dummy_identity() -> Identity {
Identity::new(x25519_dalek::PublicKey::from(
*b"hello world, oh what a beautiful",
Expand Down

0 comments on commit 117a360

Please sign in to comment.