2022-09-22 02:50:55 +03:00
|
|
|
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 some fields to the UserKeys table
|
|
|
|
manager
|
|
|
|
.alter_table(
|
|
|
|
sea_query::Table::alter()
|
|
|
|
.table(UserKeys::Table)
|
|
|
|
.to_owned()
|
|
|
|
// change requests per minute to a big_unsigned
|
|
|
|
.modify_column(
|
|
|
|
ColumnDef::new(UserKeys::RequestsPerMinute)
|
|
|
|
.big_unsigned()
|
2022-09-23 08:22:33 +03:00
|
|
|
.null(),
|
2022-09-22 02:50:55 +03:00
|
|
|
)
|
|
|
|
// add a column for logging reverts in the RevertLogs table
|
|
|
|
.add_column(
|
2022-09-24 05:47:44 +03:00
|
|
|
ColumnDef::new(UserKeys::LogRevertChance)
|
2022-09-23 08:22:33 +03:00
|
|
|
.decimal_len(5, 4)
|
2022-09-22 02:50:55 +03:00
|
|
|
.not_null()
|
2022-09-23 08:22:33 +03:00
|
|
|
.default("0.0"),
|
2022-09-22 02:50:55 +03:00
|
|
|
)
|
2022-09-23 08:22:33 +03:00
|
|
|
// add columns for more advanced authorization
|
|
|
|
.add_column(ColumnDef::new(UserKeys::AllowedIps).text().null())
|
|
|
|
.add_column(ColumnDef::new(UserKeys::AllowedOrigins).text().null())
|
|
|
|
.add_column(ColumnDef::new(UserKeys::AllowedReferers).text().null())
|
|
|
|
.add_column(ColumnDef::new(UserKeys::AllowedUserAgents).text().null())
|
2022-09-22 02:50:55 +03:00
|
|
|
.to_owned(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// create a table for logging reverting eth_call and eth_estimateGas
|
|
|
|
manager
|
|
|
|
.create_table(
|
|
|
|
Table::create()
|
|
|
|
.table(RevertLogs::Table)
|
|
|
|
.col(
|
|
|
|
ColumnDef::new(RevertLogs::Id)
|
|
|
|
.big_unsigned()
|
|
|
|
.not_null()
|
|
|
|
.auto_increment()
|
|
|
|
.primary_key(),
|
|
|
|
)
|
|
|
|
.col(
|
|
|
|
ColumnDef::new(RevertLogs::UserKeyId)
|
|
|
|
.big_unsigned()
|
|
|
|
.not_null(),
|
|
|
|
)
|
|
|
|
.col(ColumnDef::new(RevertLogs::Timestamp).timestamp().not_null())
|
|
|
|
.col(
|
|
|
|
ColumnDef::new(RevertLogs::Method)
|
|
|
|
.enumeration(
|
|
|
|
"method",
|
|
|
|
["eth_call", "eth_estimateGas", "eth_sendRawTransaction"],
|
|
|
|
)
|
|
|
|
.not_null(),
|
|
|
|
)
|
|
|
|
.col(ColumnDef::new(RevertLogs::To).binary_len(20).not_null())
|
2022-09-24 10:03:29 +03:00
|
|
|
.col(ColumnDef::new(RevertLogs::CallData).text().null())
|
2022-09-22 02:50:55 +03:00
|
|
|
.index(sea_query::Index::create().col(RevertLogs::To))
|
|
|
|
.foreign_key(
|
|
|
|
sea_query::ForeignKey::create()
|
|
|
|
.from(RevertLogs::Table, RevertLogs::UserKeyId)
|
|
|
|
.to(UserKeys::Table, UserKeys::Id),
|
|
|
|
)
|
|
|
|
.to_owned(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
|
|
// drop the new table
|
|
|
|
manager
|
|
|
|
.drop_table(Table::drop().table(RevertLogs::Table).to_owned())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// put the UserKeys back to how it was before our migrations
|
|
|
|
manager
|
|
|
|
.alter_table(
|
|
|
|
sea_query::Table::alter()
|
|
|
|
.table(UserKeys::Table)
|
|
|
|
.to_owned()
|
|
|
|
.modify_column(
|
|
|
|
ColumnDef::new(UserKeys::RequestsPerMinute)
|
|
|
|
.unsigned()
|
|
|
|
.not_null(),
|
|
|
|
)
|
2022-09-24 05:47:44 +03:00
|
|
|
.drop_column(UserKeys::LogRevertChance)
|
2022-09-22 02:50:55 +03:00
|
|
|
.to_owned(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from create_table.rs, but added
|
|
|
|
#[derive(Iden)]
|
|
|
|
pub enum UserKeys {
|
|
|
|
Table,
|
|
|
|
Id,
|
2022-09-23 08:22:33 +03:00
|
|
|
// we don't touch some of the columns
|
2022-09-22 02:50:55 +03:00
|
|
|
// UserId,
|
|
|
|
// ApiKey,
|
|
|
|
// Description,
|
|
|
|
// PrivateTxs,
|
|
|
|
// Active,
|
|
|
|
RequestsPerMinute,
|
2022-09-24 05:47:44 +03:00
|
|
|
LogRevertChance,
|
2022-09-23 08:22:33 +03:00
|
|
|
AllowedIps,
|
|
|
|
AllowedOrigins,
|
|
|
|
AllowedReferers,
|
|
|
|
AllowedUserAgents,
|
2022-09-22 02:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Iden)]
|
|
|
|
enum RevertLogs {
|
|
|
|
Table,
|
|
|
|
Id,
|
|
|
|
UserKeyId,
|
|
|
|
Method,
|
|
|
|
CallData,
|
|
|
|
To,
|
|
|
|
Timestamp,
|
|
|
|
}
|