-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compliance: compliance-server: db: Define migrations and query db
- Loading branch information
Showing
12 changed files
with
193 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# For documentation on how to configure this file, | ||
# see https://diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/schema.rs" | ||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] | ||
|
||
[migrations_directory] | ||
dir = "./migrations" |
Empty file.
6 changes: 6 additions & 0 deletions
6
compliance/compliance-server/migrations/00000000000000_diesel_initial_setup/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
36 changes: 36 additions & 0 deletions
36
compliance/compliance-server/migrations/00000000000000_diesel_initial_setup/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
2 changes: 2 additions & 0 deletions
2
compliance/compliance-server/migrations/2024-06-28-231756_create_compliance_table/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- Drop the wallet compliance table | ||
DROP TABLE IF EXISTS wallet_compliance; |
8 changes: 8 additions & 0 deletions
8
compliance/compliance-server/migrations/2024-06-28-231756_create_compliance_table/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- Create a table for caching wallet compliance information | ||
CREATE TABLE IF NOT EXISTS wallet_compliance ( | ||
address TEXT PRIMARY KEY, | ||
is_compliant BOOLEAN NOT NULL, | ||
reason TEXT NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
expires_at TIMESTAMP NOT NULL DEFAULT NOW() + INTERVAL '1 year' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Database helpers for the server | ||
|
||
use std::time::SystemTime; | ||
|
||
use compliance_api::ComplianceStatus; | ||
use diesel::{ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl}; | ||
use renegade_util::err_str; | ||
|
||
use crate::{ | ||
error::ComplianceServerError, | ||
schema::{ | ||
wallet_compliance, | ||
wallet_compliance::dsl::{address as address_col, wallet_compliance as compliance_table}, | ||
}, | ||
}; | ||
|
||
// ---------- | ||
// | Models | | ||
// ---------- | ||
|
||
/// A compliance entry for a wallet | ||
#[derive(Debug, Clone, Queryable, Insertable)] | ||
#[table_name = "wallet_compliance"] | ||
#[allow(missing_docs)] | ||
pub struct ComplianceEntry { | ||
pub address: String, | ||
pub is_compliant: bool, | ||
pub reason: String, | ||
pub created_at: SystemTime, | ||
pub expires_at: SystemTime, | ||
} | ||
|
||
impl ComplianceEntry { | ||
/// Get the compliance status for an entry | ||
pub fn compliance_status(&self) -> ComplianceStatus { | ||
if self.is_compliant { | ||
ComplianceStatus::Compliant | ||
} else { | ||
ComplianceStatus::NotCompliant { reason: self.reason.clone() } | ||
} | ||
} | ||
} | ||
|
||
// ----------- | ||
// | Queries | | ||
// ----------- | ||
|
||
/// Get a compliance entry by address | ||
pub fn get_compliance_entry( | ||
address: &str, | ||
conn: &mut PgConnection, | ||
) -> Result<Option<ComplianceEntry>, ComplianceServerError> { | ||
let query = compliance_table | ||
.filter(address_col.eq(address)) | ||
.load::<ComplianceEntry>(conn) | ||
.map_err(err_str!(ComplianceServerError::Db))?; | ||
|
||
Ok(query.first().cloned()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
diesel::table! { | ||
wallet_compliance (address) { | ||
address -> Text, | ||
is_compliant -> Bool, | ||
reason -> Text, | ||
created_at -> Timestamp, | ||
expires_at -> Timestamp, | ||
} | ||
} |