web3-proxy/migration/src/m20230422_172555_premium_downgrade_logic.rs
Bryan Stitt 8a097dabbe
Bryan devel 2023-05-12 (#67)
* add minor todo

* BadRequest instead of web3_context

* more bad request error codes

* use tokio-uring for the tcp listener

* clear block instead of panic

* clone earlier

* more watch channels instead of rwlocks

* drop uring for now (its single threaded) and combine get/post/put routes

* clean up iter vs into_iter and unnecessary collect

* arcswap instead of rwlock for Web3Rpcs.by_name

* cargo upgrade

* uuid fast-rng and alphabetize

* if protected rpcs, only use protected rpcs

* listenfd

* make connectinfo optional

* try_get_with_by_ref instead of try_get_with

* anyhow ensure. and try_get_with_as_ref isn't actually needed

* fix feature flags

* more refs and less clone

* automatic retry for eth_getTransactionReceipt and eth_getTransactionByHash

thanks for the report Lefteris @ Rotki

* ArcSwap for provider

* set archive_request to true on transaction retrying

* merge durable stats

* Revert "ArcSwap for provider"

This reverts commit 166d77f204cde9fa7722c0cefecbb27008749d47.

* comments

* less clones

* more refs

* fix test

* add optional mimalloc feature

* remove stale dependency

* sort

* cargo upgrade

* lint constants

* add todo

* another todo

* lint

* anyhow::ensure instead of panic

* allow rpc_accounting_v2 entries for requests without an rpc key
2023-05-12 15:15:32 -07:00

125 lines
4.1 KiB
Rust

use crate::sea_orm::ConnectionTrait;
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 column "downgrade_tier_id"
// It is a "foreign key" that references other items in this table
manager
.alter_table(
Table::alter()
.table(UserTier::Table)
.add_column(ColumnDef::new(UserTier::DowngradeTierId).big_unsigned())
.add_foreign_key(
TableForeignKey::new()
.to_tbl(UserTier::Table)
.from_col(UserTier::DowngradeTierId)
.to_col(UserTier::Id),
)
.to_owned(),
)
.await?;
// Insert Premium, and PremiumOutOfFunds
let premium_out_of_funds_tier = Query::insert()
.into_table(UserTier::Table)
.columns([
UserTier::Title,
UserTier::MaxRequestsPerPeriod,
UserTier::MaxConcurrentRequests,
UserTier::DowngradeTierId,
])
.values_panic([
"Premium Out Of Funds".into(),
Some("6000").into(),
Some("5").into(),
None::<i64>.into(),
])
.to_owned();
manager.exec_stmt(premium_out_of_funds_tier).await?;
// Insert Premium Out Of Funds
// get the premium tier ...
let db_conn = manager.get_connection();
let db_backend = manager.get_database_backend();
let select_premium_out_of_funds_tier_id = Query::select()
.column(UserTier::Id)
.from(UserTier::Table)
.cond_where(Expr::col(UserTier::Title).eq("Premium Out Of Funds"))
.to_owned();
let premium_out_of_funds_tier_id: u64 = db_conn
.query_one(db_backend.build(&select_premium_out_of_funds_tier_id))
.await?
.expect("we just created Premium Out Of Funds")
.try_get("", &UserTier::Id.to_string())?;
// Add two tiers for premium: premium, and premium-out-of-funds
let premium_tier = Query::insert()
.into_table(UserTier::Table)
.columns([
UserTier::Title,
UserTier::MaxRequestsPerPeriod,
UserTier::MaxConcurrentRequests,
UserTier::DowngradeTierId,
])
.values_panic([
"Premium".into(),
None::<&str>.into(),
Some("100").into(),
Some(premium_out_of_funds_tier_id).into(),
])
.to_owned();
manager.exec_stmt(premium_tier).await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Remove the two tiers that you just added
// And remove the column you just added
let db_conn = manager.get_connection();
let db_backend = manager.get_database_backend();
let delete_premium = Query::delete()
.from_table(UserTier::Table)
.cond_where(Expr::col(UserTier::Title).eq("Premium"))
.to_owned();
db_conn.execute(db_backend.build(&delete_premium)).await?;
let delete_premium_out_of_funds = Query::delete()
.from_table(UserTier::Table)
.cond_where(Expr::col(UserTier::Title).eq("Premium Out Of Funds"))
.to_owned();
db_conn
.execute(db_backend.build(&delete_premium_out_of_funds))
.await?;
// Finally drop the downgrade column
manager
.alter_table(
Table::alter()
.table(UserTier::Table)
.drop_column(UserTier::DowngradeTierId)
.to_owned(),
)
.await
}
}
#[derive(Iden)]
enum UserTier {
Table,
Id,
Title,
MaxRequestsPerPeriod,
MaxConcurrentRequests,
DowngradeTierId,
}