From 2b30422b84aeb24a4346b4a1302f5ab638a57543 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 5 Apr 2023 14:55:37 -0700 Subject: [PATCH] fixes for NULL and UNIQUE to work together --- entities/src/rpc_accounting_v2.rs | 6 +++--- migration/src/m20230125_204810_stats_v2.rs | 20 +++++++++----------- web3_proxy/src/stats/mod.rs | 6 +++--- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/entities/src/rpc_accounting_v2.rs b/entities/src/rpc_accounting_v2.rs index 8fef415d..9f94c7e7 100644 --- a/entities/src/rpc_accounting_v2.rs +++ b/entities/src/rpc_accounting_v2.rs @@ -8,11 +8,11 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] pub id: u64, - pub rpc_key_id: Option, + pub rpc_key_id: u64, pub chain_id: u64, pub period_datetime: DateTimeUtc, - pub method: Option, - pub origin: Option, + pub method: String, + pub origin: String, pub archive_needed: bool, pub error_response: bool, pub frontend_requests: u64, diff --git a/migration/src/m20230125_204810_stats_v2.rs b/migration/src/m20230125_204810_stats_v2.rs index 6e162ad4..223acabe 100644 --- a/migration/src/m20230125_204810_stats_v2.rs +++ b/migration/src/m20230125_204810_stats_v2.rs @@ -100,17 +100,7 @@ impl MigrationTrait for Migration { .big_unsigned() .not_null(), ) - .foreign_key( - sea_query::ForeignKey::create() - .from(RpcAccountingV2::Table, RpcAccountingV2::RpcKeyId) - .to(RpcKey::Table, RpcKey::Id), - ) - .index(sea_query::Index::create().col(RpcAccountingV2::ChainId)) - .index(sea_query::Index::create().col(RpcAccountingV2::Origin)) - .index(sea_query::Index::create().col(RpcAccountingV2::PeriodDatetime)) - .index(sea_query::Index::create().col(RpcAccountingV2::Method)) - .index(sea_query::Index::create().col(RpcAccountingV2::ArchiveNeeded)) - .index(sea_query::Index::create().col(RpcAccountingV2::ErrorResponse)) + // cannot use NULL columns for any of these because unique indexes allow duplicates on NULL .index( sea_query::Index::create() .col(RpcAccountingV2::RpcKeyId) @@ -122,6 +112,14 @@ impl MigrationTrait for Migration { .col(RpcAccountingV2::ErrorResponse) .unique(), ) + // cannot use a foreign key for RpcKeyId because the UNIQUE index uses 0 instead of NULL + .index(sea_query::Index::create().col(RpcAccountingV2::RpcKeyId)) + .index(sea_query::Index::create().col(RpcAccountingV2::ChainId)) + .index(sea_query::Index::create().col(RpcAccountingV2::Origin)) + .index(sea_query::Index::create().col(RpcAccountingV2::PeriodDatetime)) + .index(sea_query::Index::create().col(RpcAccountingV2::Method)) + .index(sea_query::Index::create().col(RpcAccountingV2::ArchiveNeeded)) + .index(sea_query::Index::create().col(RpcAccountingV2::ErrorResponse)) .to_owned(), ) .await?; diff --git a/web3_proxy/src/stats/mod.rs b/web3_proxy/src/stats/mod.rs index 5a13ad01..5ecae74d 100644 --- a/web3_proxy/src/stats/mod.rs +++ b/web3_proxy/src/stats/mod.rs @@ -236,11 +236,11 @@ impl BufferedRpcQueryStats { // this is a lot of variables let accounting_entry = rpc_accounting_v2::ActiveModel { id: sea_orm::NotSet, - rpc_key_id: sea_orm::Set(key.rpc_secret_key_id.map(Into::into)), - origin: sea_orm::Set(key.origin.map(|x| x.to_string())), + rpc_key_id: sea_orm::Set(key.rpc_secret_key_id.map(Into::into).unwrap_or_default()), + origin: sea_orm::Set(key.origin.map(|x| x.to_string()).unwrap_or_default()), chain_id: sea_orm::Set(chain_id), period_datetime: sea_orm::Set(period_datetime), - method: sea_orm::Set(key.method), + method: sea_orm::Set(key.method.unwrap_or_default()), archive_needed: sea_orm::Set(key.archive_needed), error_response: sea_orm::Set(key.error_response), frontend_requests: sea_orm::Set(self.frontend_requests),