diff --git a/Cargo.lock b/Cargo.lock index f5592e65..5969f100 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2938,7 +2938,7 @@ dependencies = [ [[package]] name = "migration" -version = "0.17.0" +version = "0.19.0" dependencies = [ "sea-orm-migration", "tokio", diff --git a/migration/Cargo.toml b/migration/Cargo.toml index cea86b6b..06cc2134 100644 --- a/migration/Cargo.toml +++ b/migration/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "migration" -version = "0.17.0" +version = "0.19.0" edition = "2021" publish = false diff --git a/migration/src/m20230125_204810_stats_v2.rs b/migration/src/m20230125_204810_stats_v2.rs new file mode 100644 index 00000000..7082fec0 --- /dev/null +++ b/migration/src/m20230125_204810_stats_v2.rs @@ -0,0 +1,159 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(RpcAccountingV2::Table) + .col( + ColumnDef::new(RpcAccountingV2::Id) + .big_unsigned() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(RpcAccountingV2::RpcKeyId) + .big_unsigned() + .not_null() + .default(0), + ) + .col( + ColumnDef::new(RpcAccountingV2::ChainId) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::Origin) + .string() + .not_null() + .default(""), + ) + .col( + ColumnDef::new(RpcAccountingV2::PeriodDatetime) + .timestamp() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::Method) + .string() + .not_null() + .default(""), + ) + .col( + ColumnDef::new(RpcAccountingV2::ArchiveNeeded) + .boolean() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::ErrorResponse) + .boolean() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::FrontendRequests) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::BackendRequests) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::BackendRetries) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::NoServers) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::CacheMisses) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::CacheHits) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::SumRequestBytes) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::SumResponseMillis) + .big_unsigned() + .not_null(), + ) + .col( + ColumnDef::new(RpcAccountingV2::SumResponseBytes) + .big_unsigned() + .not_null(), + ) + // cannot use NULL columns for any of these because unique indexes allow duplicates on NULL + .index( + sea_query::Index::create() + .col(RpcAccountingV2::RpcKeyId) + .col(RpcAccountingV2::ChainId) + .col(RpcAccountingV2::Origin) + .col(RpcAccountingV2::PeriodDatetime) + .col(RpcAccountingV2::Method) + .col(RpcAccountingV2::ArchiveNeeded) + .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?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(RpcAccountingV2::Table).to_owned()) + .await?; + + Ok(()) + } +} + +#[derive(Iden)] +enum RpcAccountingV2 { + Table, + Id, + RpcKeyId, + ChainId, + Origin, + PeriodDatetime, + Method, + ArchiveNeeded, + ErrorResponse, + FrontendRequests, + BackendRequests, + BackendRetries, + NoServers, + CacheMisses, + CacheHits, + SumRequestBytes, + SumResponseMillis, + SumResponseBytes, +} diff --git a/migration/src/m20230307_002623_migrate_rpc_accounting_to_rpc_accounting_v2.rs b/migration/src/m20230307_002623_migrate_rpc_accounting_to_rpc_accounting_v2.rs new file mode 100644 index 00000000..46a82c93 --- /dev/null +++ b/migration/src/m20230307_002623_migrate_rpc_accounting_to_rpc_accounting_v2.rs @@ -0,0 +1,37 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Add a nullable timestamp column to check if things were migrated in the rpc_accounting table + manager + .alter_table( + Table::alter() + .table(RpcAccounting::Table) + .add_column(ColumnDef::new(RpcAccounting::Migrated).timestamp()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(RpcAccounting::Table) + .drop_column(RpcAccounting::Migrated) + .to_owned(), + ) + .await + } +} + +/// partial table for RpcAccounting +#[derive(Iden)] +enum RpcAccounting { + Table, + Migrated, +}