Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,6 +14,8 @@ insert_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -33,6 +35,8 @@ insert_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -53,6 +57,8 @@ select_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -75,6 +81,8 @@ select_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -95,6 +103,8 @@ select_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -120,6 +130,8 @@ update_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand All@@ -142,6 +154,8 @@ update_permissions:
- documents
- labels
- total_voters_percent
- blank_ballots
- blank_ballots_percent
- name
- created_at
- last_updated_at
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,8 @@ insert_permissions:
_eq: X-Hasura-Tenant-Id
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -24,6 +26,8 @@ insert_permissions:
check: {}
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -40,6 +44,8 @@ select_permissions:
allow_aggregations: true
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -58,6 +64,8 @@ select_permissions:
allow_aggregations: true
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -74,6 +82,8 @@ select_permissions:
allow_aggregations: true
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -95,6 +105,8 @@ update_permissions:
_eq: X-Hasura-Tenant-Id
columns:
- area_id
- blank_ballots
- blank_ballots_percent
- created_at
- documents
- election_event_id
Expand All@@ -117,6 +129,8 @@ update_permissions:
- created_at
- last_updated_at
- area_id
- blank_ballots
- blank_ballots_percent
- election_event_id
- election_id
- id
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
ALTER TABLE "sequent_backend"."results_election_area"
DROP COLUMN "blank_ballots",
DROP COLUMN "blank_ballots_percent";

ALTER TABLE "sequent_backend"."results_election"
DROP COLUMN "blank_ballots",
DROP COLUMN "blank_ballots_percent";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
ALTER TABLE "sequent_backend"."results_election"
ADD COLUMN "blank_ballots" integer,
ADD COLUMN "blank_ballots_percent" numeric;

ALTER TABLE "sequent_backend"."results_election_area"
ADD COLUMN "blank_ballots" integer,
ADD COLUMN "blank_ballots_percent" numeric;
37 changes: 27 additions & 10 deletions packages/windmill/src/postgres/results_election.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,11 +51,14 @@ impl TryFrom<Row> for ResultsElectionWrapper {
.map(NotNan::new)
.transpose()?,
documents,
// The results_election.blank_ballots(_percent) columns don't
// exist yet; populated by a later PR once the hasura migration
// lands.
blank_ballots: None,
blank_ballots_percent: None,
blank_ballots: item
.try_get::<_, Option<i32>>("blank_ballots")?
.map(|v| v as i64),
blank_ballots_percent: item
.try_get::<&str, Option<Decimal>>("blank_ballots_percent")?
.map(|d| d.to_f64().map(NotNan::new).transpose())
.transpose()?
.flatten(),
}))
}
}
Expand DownExpand Up@@ -150,6 +153,8 @@ pub async fn insert_results_elections(
pub elegible_census: Option<i64>,
pub total_voters: Option<i64>,
pub total_voters_percent: Option<f64>,
pub blank_ballots: Option<i64>,
pub blank_ballots_percent: Option<f64>,
}

let tenant_uuid = parse_uuid_v4(tenant_id)?;
Expand All@@ -168,6 +173,8 @@ pub async fn insert_results_elections(
elegible_census: election.elegible_census,
total_voters: election.total_voters,
total_voters_percent: election.total_voters_percent.clone().map(|n| n.into()),
blank_ballots: election.blank_ballots,
blank_ballots_percent: election.blank_ballots_percent.map(|n| n.into()),
})
})
.collect::<Result<Vec<InsertResultsElection>>>()?;
Expand All@@ -184,14 +191,16 @@ pub async fn insert_results_elections(
name TEXT,
elegible_census BIGINT,
total_voters BIGINT,
total_voters_percent FLOAT8
total_voters_percent FLOAT8,
blank_ballots BIGINT,
blank_ballots_percent FLOAT8
)
)
INSERT INTO sequent_backend.results_election (
tenant_id, election_event_id, results_event_id, election_id, name, elegible_census, total_voters, total_voters_percent
tenant_id, election_event_id, results_event_id, election_id, name, elegible_census, total_voters, total_voters_percent, blank_ballots, blank_ballots_percent
)
SELECT
tenant_id, election_event_id, results_event_id, election_id, name, elegible_census, total_voters, total_voters_percent
tenant_id, election_event_id, results_event_id, election_id, name, elegible_census, total_voters, total_voters_percent, blank_ballots, blank_ballots_percent
FROM data
RETURNING *;";

Expand DownExpand Up@@ -371,6 +380,8 @@ struct InsertableResultsElection {
elegible_census: Option<i64>,
total_voters: Option<i64>,
total_voters_percent: Option<f64>,
blank_ballots: Option<i64>,
blank_ballots_percent: Option<f64>,
created_at: Option<DateTime<Local>>,
last_updated_at: Option<DateTime<Local>>,
labels: Option<Value>,
Expand DownExpand Up@@ -402,6 +413,8 @@ pub async fn insert_many_results_elections(
elegible_census: r.elegible_census,
total_voters: r.total_voters,
total_voters_percent: r.total_voters_percent.map(|n| n.into_inner()),
blank_ballots: r.blank_ballots,
blank_ballots_percent: r.blank_ballots_percent.map(|n| n.into_inner()),
created_at: r.created_at,
last_updated_at: r.last_updated_at,
labels: r.labels.clone(),
Expand All@@ -425,6 +438,8 @@ pub async fn insert_many_results_elections(
elegible_census BIGINT,
total_voters BIGINT,
total_voters_percent FLOAT8,
blank_ballots BIGINT,
blank_ballots_percent FLOAT8,
created_at TIMESTAMPTZ,
last_updated_at TIMESTAMPTZ,
labels JSONB,
Expand All@@ -435,13 +450,15 @@ pub async fn insert_many_results_elections(
INSERT INTO sequent_backend.results_election (
id, tenant_id, election_event_id, election_id,
results_event_id, name, elegible_census, total_voters,
total_voters_percent, created_at, last_updated_at,
total_voters_percent, blank_ballots, blank_ballots_percent,
created_at, last_updated_at,
labels, annotations, documents
)
SELECT
id, tenant_id, election_event_id, election_id,
results_event_id, name, elegible_census, total_voters,
total_voters_percent, created_at, last_updated_at,
total_voters_percent, blank_ballots, blank_ballots_percent,
created_at, last_updated_at,
labels, annotations, documents
FROM data
RETURNING *;
Expand Down
53 changes: 38 additions & 15 deletions packages/windmill/src/postgres/results_election_area.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,9 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Local};
use deadpool_postgres::Transaction;
use ordered_float::NotNan;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use sequent_core::serialization::deserialize_with_path::deserialize_value;
use sequent_core::services::uuid_validation::parse_uuid_v4;
use sequent_core::types::results::{ResultDocuments, ResultsElectionArea};
Expand DownExpand Up@@ -36,11 +39,14 @@ impl TryFrom<Row> for ResultsElectionAreaWrapper {
created_at: item.get("created_at"),
last_updated_at: item.get("last_updated_at"),
documents,
// The results_election_area.blank_ballots(_percent) columns
// don't exist yet; populated by a later PR once the hasura
// migration lands.
blank_ballots: None,
blank_ballots_percent: None,
blank_ballots: item
.try_get::<_, Option<i32>>("blank_ballots")?
.map(|v| v as i64),
blank_ballots_percent: item
.try_get::<&str, Option<Decimal>>("blank_ballots_percent")?
.map(|d| d.to_f64().map(NotNan::new).transpose())
.transpose()?
.flatten(),
}))
}
}
Expand All@@ -55,6 +61,8 @@ pub async fn insert_results_election_area_documents(
area_id: &str,
area_name: &str,
documents: &ResultDocuments,
blank_ballots: Option<i32>,
blank_ballots_percent: Option<f64>,
) -> Result<()> {
let documents_value = serde_json::to_value(documents.clone())?;
let tenant_uuid: uuid::Uuid = parse_uuid_v4(&tenant_id)
Expand All@@ -67,20 +75,27 @@ pub async fn insert_results_election_area_documents(
.map_err(|err| anyhow!("Error parsing election_id as UUID: {}", err))?;
let area_uuid: uuid::Uuid =
parse_uuid_v4(&area_id).map_err(|err| anyhow!("Error parsing area_id as UUID: {}", err))?;
// blank_ballots_percent is a Postgres `numeric` column; tokio-postgres
// only maps f64 to `float8`, so bind the value as the same Decimal type
// the read path (ResultsElectionAreaWrapper) already uses.
let blank_ballots_percent_decimal: Option<Decimal> =
blank_ballots_percent.and_then(Decimal::from_f64_retain);

let statement = hasura_transaction
.prepare(
r#"
INSERT INTO sequent_backend.results_election_area (
documents,
tenant_id,
results_event_id,
election_event_id,
election_id,
documents,
tenant_id,
results_event_id,
election_event_id,
election_id,
area_id,
name
name,
blank_ballots,
blank_ballots_percent
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id;
"#,
)
Expand All@@ -96,6 +111,8 @@ pub async fn insert_results_election_area_documents(
&election_uuid,
&area_uuid,
&area_name,
&blank_ballots,
&blank_ballots_percent_decimal,
],
)
.await
Expand DownExpand Up@@ -158,6 +175,8 @@ struct InsertableResultsElectionArea {
last_updated_at: Option<DateTime<Local>>,
documents: Option<Value>,
name: Option<String>,
blank_ballots: Option<i64>,
blank_ballots_percent: Option<f64>,
}

#[instrument(err, skip(hasura_transaction, areas))]
Expand DownExpand Up@@ -185,6 +204,8 @@ pub async fn insert_many_results_elections_areas(
last_updated_at: a.last_updated_at,
documents: documents_json,
name: a.name.clone(),
blank_ballots: a.blank_ballots,
blank_ballots_percent: a.blank_ballots_percent.map(|n| n.into_inner()),
})
})
.collect::<Result<_>>()?;
Expand All@@ -203,18 +224,20 @@ pub async fn insert_many_results_elections_areas(
created_at TIMESTAMPTZ,
last_updated_at TIMESTAMPTZ,
documents JSONB,
name TEXT
name TEXT,
blank_ballots BIGINT,
blank_ballots_percent FLOAT8
)
)
INSERT INTO sequent_backend.results_election_area (
id, tenant_id, election_event_id, election_id,
area_id, results_event_id, created_at, last_updated_at,
documents, name
documents, name, blank_ballots, blank_ballots_percent
)
SELECT
id, tenant_id, election_event_id, election_id,
area_id, results_event_id, created_at, last_updated_at,
documents, name
documents, name, blank_ballots, blank_ballots_percent
FROM data
RETURNING *;
"#;
Expand Down
Loading
Loading