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

View File

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

View File

@ -100,17 +100,7 @@ impl MigrationTrait for Migration {
.big_unsigned() .big_unsigned()
.not_null(), .not_null(),
) )
.foreign_key( // cannot use NULL columns for any of these because unique indexes allow duplicates on NULL
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))
.index( .index(
sea_query::Index::create() sea_query::Index::create()
.col(RpcAccountingV2::RpcKeyId) .col(RpcAccountingV2::RpcKeyId)
@ -122,6 +112,14 @@ impl MigrationTrait for Migration {
.col(RpcAccountingV2::ErrorResponse) .col(RpcAccountingV2::ErrorResponse)
.unique(), .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(), .to_owned(),
) )
.await?; .await?;

View File

@ -236,11 +236,11 @@ impl BufferedRpcQueryStats {
// this is a lot of variables // this is a lot of variables
let accounting_entry = rpc_accounting_v2::ActiveModel { let accounting_entry = rpc_accounting_v2::ActiveModel {
id: sea_orm::NotSet, id: sea_orm::NotSet,
rpc_key_id: sea_orm::Set(key.rpc_secret_key_id.map(Into::into)), 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())), origin: sea_orm::Set(key.origin.map(|x| x.to_string()).unwrap_or_default()),
chain_id: sea_orm::Set(chain_id), chain_id: sea_orm::Set(chain_id),
period_datetime: sea_orm::Set(period_datetime), 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), archive_needed: sea_orm::Set(key.archive_needed),
error_response: sea_orm::Set(key.error_response), error_response: sea_orm::Set(key.error_response),
frontend_requests: sea_orm::Set(self.frontend_requests), frontend_requests: sea_orm::Set(self.frontend_requests),