Skip to content

Commit ad689a3

Browse files
committed
add migration on init of Sentry, fix all queries & impl ToSql for Rule
1 parent 3484bf1 commit ad689a3

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

primitives/src/targeting/eval.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,26 @@ fn math_operator(lhs: Number, rhs: Number, ops: MathOperator) -> Result<Number,
587587
}
588588
}
589589

590+
#[cfg(feature = "postgres")]
591+
pub mod postgres {
592+
use bytes::BytesMut;
593+
use postgres_types::{accepts, to_sql_checked, IsNull, Json, ToSql, Type};
594+
use std::error::Error;
595+
use super::*;
596+
597+
impl ToSql for Rule {
598+
fn to_sql(
599+
&self,
600+
ty: &Type,
601+
w: &mut BytesMut,
602+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
603+
Json(self).to_sql(ty, w)
604+
}
605+
606+
accepts!(JSONB);
607+
to_sql_checked!();
608+
}
609+
}
590610
#[cfg(test)]
591611
mod test {
592612
use super::*;

sentry/src/db.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ pub async fn setup_migrations(environment: &str) {
8484
};
8585
}
8686

87-
let mut migrations = vec![make_migration!("20190806011140_initial-tables")];
87+
let mut migrations = vec![
88+
make_migration!("20190806011140_initial-tables"),
89+
make_migration!("20200625092729_channel-targeting-rules"),
90+
];
8891

8992
if environment == "development" {
9093
// seeds database tables for testing

sentry/src/db/channel.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub async fn get_channel_by_id(
1414
pool
1515
.run(move |connection| {
1616
async move {
17-
match connection.prepare("SELECT id, creator, deposit_asset, deposit_amount, valid_until, spec FROM channels WHERE id = $1 LIMIT 1").await {
17+
match connection.prepare("SELECT id, creator, deposit_asset, deposit_amount, valid_until, targeting_rules, spec FROM channels WHERE id = $1 LIMIT 1").await {
1818
Ok(select) => match connection.query(&select, &[&id]).await {
1919
Ok(results) => Ok((results.get(0).map(Channel::from), connection)),
2020
Err(e) => Err((e, connection)),
@@ -35,7 +35,7 @@ pub async fn get_channel_by_id_and_validator(
3535
.run(move |connection| {
3636
async move {
3737
let validator = serde_json::Value::from_str(&format!(r#"[{{"id": "{}"}}]"#, validator_id)).expect("Not a valid json");
38-
let query = "SELECT id, creator, deposit_asset, deposit_amount, valid_until, spec FROM channels WHERE id = $1 AND spec->'validators' @> $2 LIMIT 1";
38+
let query = "SELECT id, creator, deposit_asset, deposit_amount, valid_until, targeting_rules, spec FROM channels WHERE id = $1 AND spec->'validators' @> $2 LIMIT 1";
3939
match connection.prepare(query).await {
4040
Ok(select) => {
4141
match connection.query(&select, &[&id, &validator]).await {
@@ -57,8 +57,8 @@ pub async fn insert_channel(
5757
pool
5858
.run(move |connection| {
5959
async move {
60-
match connection.prepare("INSERT INTO channels (id, creator, deposit_asset, deposit_amount, valid_until, spec) values ($1, $2, $3, $4, $5, $6)").await {
61-
Ok(stmt) => match connection.execute(&stmt, &[&channel.id, &channel.creator, &channel.deposit_asset, &channel.deposit_amount, &channel.valid_until, &channel.spec]).await {
60+
match connection.prepare("INSERT INTO channels (id, creator, deposit_asset, deposit_amount, valid_until, targeting_rules, spec) values ($1, $2, $3, $4, $5, $6, $7)").await {
61+
Ok(stmt) => match connection.execute(&stmt, &[&channel.id, &channel.creator, &channel.deposit_asset, &channel.deposit_amount, &channel.valid_until, &channel.targeting_rules, &channel.spec]).await {
6262
Ok(row) => {
6363
let inserted = row == 1;
6464
Ok((inserted, connection))
@@ -138,7 +138,7 @@ mod list_channels {
138138
.run(move |connection| {
139139
async move {
140140
// To understand why we use Order by, see Postgres Documentation: https://www.postgresql.org/docs/8.1/queries-limit.html
141-
let statement = format!("SELECT id, creator, deposit_asset, deposit_amount, valid_until, spec FROM channels WHERE {} ORDER BY spec->>'created' DESC LIMIT {} OFFSET {}", where_clauses.join(" AND "), limit, skip);
141+
let statement = format!("SELECT id, creator, deposit_asset, deposit_amount, valid_until, targeting_rules, spec FROM channels WHERE {} ORDER BY spec->>'created' DESC LIMIT {} OFFSET {}", where_clauses.join(" AND "), limit, skip);
142142
match connection.prepare(&statement).await {
143143
Ok(stmt) => {
144144
match connection.query(&stmt, params.as_slice()).await {

0 commit comments

Comments
 (0)