web3-proxy/migration/src/m20220101_000001_create_table.rs

187 lines
5.7 KiB
Rust
Raw Normal View History

2022-07-26 07:53:38 +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> {
2022-08-04 01:23:01 +03:00
// users
2022-08-03 03:27:26 +03:00
manager
.create_table(
Table::create()
.table(User::Table)
.col(
ColumnDef::new(User::Id)
.big_integer()
2022-08-03 03:27:26 +03:00
.not_null()
2022-08-06 07:39:33 +03:00
.auto_increment()
.primary_key(),
2022-08-03 03:27:26 +03:00
)
.col(
ColumnDef::new(User::Address)
.binary_len(20)
2022-08-03 03:27:26 +03:00
.not_null()
.unique_key(),
)
.col(ColumnDef::new(User::Description).string())
2022-08-04 02:17:02 +03:00
.col(ColumnDef::new(User::Email).string())
2022-08-03 03:27:26 +03:00
.to_owned(),
)
.await?;
2022-07-26 07:53:38 +03:00
2022-08-04 01:23:01 +03:00
// secondary users
2022-07-26 07:53:38 +03:00
manager
.create_table(
Table::create()
2022-08-03 03:27:26 +03:00
.table(SecondaryUser::Table)
2022-07-26 07:53:38 +03:00
.col(
ColumnDef::new(SecondaryUser::Id)
.big_integer()
2022-07-26 07:53:38 +03:00
.not_null()
2022-08-06 07:39:33 +03:00
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(SecondaryUser::UserId)
.big_integer()
.not_null(),
2022-07-26 07:53:38 +03:00
)
2022-08-04 01:23:01 +03:00
.col(
ColumnDef::new(SecondaryUser::Address)
.binary_len(20)
2022-08-04 01:23:01 +03:00
.not_null(),
)
.col(ColumnDef::new(SecondaryUser::Description).string())
.col(ColumnDef::new(SecondaryUser::Email).string())
2022-08-03 03:27:26 +03:00
.col(
ColumnDef::new(SecondaryUser::Role)
.enumeration("role", ["owner", "admin", "collaborator"])
.not_null(),
)
2022-08-04 20:18:54 +03:00
.index(sea_query::Index::create().col(SecondaryUser::Address))
2022-08-04 01:23:01 +03:00
.foreign_key(
sea_query::ForeignKey::create()
.from(SecondaryUser::Table, SecondaryUser::UserId)
.to(User::Table, User::Id),
2022-08-04 01:23:01 +03:00
)
2022-07-26 07:53:38 +03:00
.to_owned(),
)
2022-08-03 03:27:26 +03:00
.await?;
2022-08-04 01:23:01 +03:00
// api keys
2022-08-03 03:27:26 +03:00
manager
.create_table(
Table::create()
.table(UserKeys::Table)
.col(
ColumnDef::new(UserKeys::Id)
.big_integer()
2022-08-03 03:27:26 +03:00
.not_null()
2022-08-06 07:39:33 +03:00
.auto_increment()
.primary_key(),
2022-08-03 03:27:26 +03:00
)
.col(ColumnDef::new(UserKeys::UserId).big_integer().not_null())
2022-08-04 01:23:01 +03:00
.col(
ColumnDef::new(UserKeys::ApiKey)
2022-08-05 22:22:23 +03:00
.uuid()
2022-08-04 01:23:01 +03:00
.not_null()
.unique_key(),
)
.col(ColumnDef::new(UserKeys::Description).string())
2022-08-03 03:27:26 +03:00
.col(
ColumnDef::new(UserKeys::PrivateTxs)
.boolean()
.default(true)
.not_null(),
)
.col(
ColumnDef::new(UserKeys::Active)
.boolean()
.default(true)
.not_null(),
)
2022-08-07 09:48:57 +03:00
.col(
ColumnDef::new(UserKeys::RequestsPerMinute)
.unsigned()
.default(true)
.not_null(),
)
2022-08-04 20:18:54 +03:00
.index(sea_query::Index::create().col(UserKeys::Active))
2022-08-04 01:23:01 +03:00
.foreign_key(
sea_query::ForeignKey::create()
.from(UserKeys::Table, UserKeys::UserId)
.to(User::Table, User::Id),
2022-08-04 01:23:01 +03:00
)
2022-08-03 03:27:26 +03:00
.to_owned(),
)
.await?;
2022-08-04 01:23:01 +03:00
// it worked!
2022-08-03 03:27:26 +03:00
Ok(())
2022-07-26 07:53:38 +03:00
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
2022-08-03 03:27:26 +03:00
.drop_table(Table::drop().table(User::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(SecondaryUser::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(UserKeys::Table).to_owned())
.await?;
Ok(())
2022-07-26 07:53:38 +03:00
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
2022-08-03 03:27:26 +03:00
enum User {
Table,
Id,
2022-08-03 03:27:26 +03:00
Address,
Description,
Email,
}
/*
-- TODO: foreign keys
-- TODO: how should we store addresses?
-- TODO: creation time?
-- TODO: permissions. likely similar to infura
// TODO: creation time?
*/
#[derive(Iden)]
enum SecondaryUser {
Table,
Id,
2022-08-03 03:27:26 +03:00
UserId,
Address,
Description,
Email,
Role,
}
/*
-- TODO: foreign keys
-- TODO: index on api_key
-- TODO: what size for api_key
-- TODO: track active with a timestamp?
-- TODO: creation time?
2022-08-07 09:48:57 +03:00
-- TODO: requests_per_day? requests_per_second?,
2022-08-03 03:27:26 +03:00
-- TODO: more security features. likely similar to infura
*/
#[derive(Iden)]
enum UserKeys {
2022-07-26 07:53:38 +03:00
Table,
Id,
UserId,
2022-08-03 03:27:26 +03:00
ApiKey,
Description,
PrivateTxs,
Active,
2022-08-07 09:48:57 +03:00
RequestsPerMinute,
2022-07-26 07:53:38 +03:00
}