fixes for NULL and UNIQUE to work together

This commit is contained in:
Bryan Stitt 2023-04-05 14:55:37 -07:00
parent 66471e29df
commit 2b30422b84
3 changed files with 15 additions and 17 deletions

@ -8,11 +8,11 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub id: u64,
pub rpc_key_id: Option<u64>,
pub rpc_key_id: u64,
pub chain_id: u64,
pub period_datetime: DateTimeUtc,
pub method: Option<String>,
pub origin: Option<String>,
pub method: String,
pub origin: String,
pub archive_needed: bool,
pub error_response: bool,
pub frontend_requests: u64,

@ -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?;

@ -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),