more indexes

This commit is contained in:
Bryan Stitt 2022-08-03 22:23:01 +00:00
parent 4d89b0e8b7
commit 3f063efce8

View File

@ -6,11 +6,11 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// users
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(User::Table) .table(User::Table)
.if_not_exists()
.col( .col(
ColumnDef::new(User::Id) ColumnDef::new(User::Id)
.big_integer() .big_integer()
@ -30,11 +30,11 @@ impl MigrationTrait for Migration {
) )
.await?; .await?;
// secondary users
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(SecondaryUser::Table) .table(SecondaryUser::Table)
.if_not_exists()
.col( .col(
ColumnDef::new(SecondaryUser::Id) ColumnDef::new(SecondaryUser::Id)
.big_integer() .big_integer()
@ -47,7 +47,11 @@ impl MigrationTrait for Migration {
.big_integer() .big_integer()
.not_null(), .not_null(),
) )
.col(ColumnDef::new(SecondaryUser::Address).string().not_null()) .col(
ColumnDef::new(SecondaryUser::Address)
.string_len(42)
.not_null(),
)
.col( .col(
ColumnDef::new(SecondaryUser::Description) ColumnDef::new(SecondaryUser::Description)
.string() .string()
@ -59,25 +63,25 @@ impl MigrationTrait for Migration {
.enumeration("role", ["owner", "admin", "collaborator"]) .enumeration("role", ["owner", "admin", "collaborator"])
.not_null(), .not_null(),
) )
.index(
sea_query::Index::create()
.name("idx-secondary_user-address")
.col(SecondaryUser::Address),
)
.foreign_key(
sea_query::ForeignKey::create()
.from(SecondaryUser::Table, SecondaryUser::UserId)
.to(User::Table, User::Id),
)
.to_owned(), .to_owned(),
) )
.await?; .await?;
// TODO: make sure from and to aren't backwards // block list for the transaction firewall
manager
.create_foreign_key(
sea_query::ForeignKey::create()
.from(SecondaryUser::Table, SecondaryUser::UserId)
.to(User::Table, User::Id)
.to_owned(),
)
.await?;
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(BlockList::Table) .table(BlockList::Table)
.if_not_exists()
.col( .col(
ColumnDef::new(BlockList::Id) ColumnDef::new(BlockList::Id)
.big_integer() .big_integer()
@ -85,18 +89,22 @@ impl MigrationTrait for Migration {
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(BlockList::Address).string().not_null()) .col(
.col(ColumnDef::new(BlockList::Chain).integer().not_null()) ColumnDef::new(BlockList::Address)
.string()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(BlockList::Description).string().not_null()) .col(ColumnDef::new(BlockList::Description).string().not_null())
.to_owned(), .to_owned(),
) )
.await?; .await?;
// api keys
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(UserKeys::Table) .table(UserKeys::Table)
.if_not_exists()
.col( .col(
ColumnDef::new(UserKeys::Id) ColumnDef::new(UserKeys::Id)
.big_integer() .big_integer()
@ -105,7 +113,12 @@ impl MigrationTrait for Migration {
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(UserKeys::UserId).big_integer().not_null()) .col(ColumnDef::new(UserKeys::UserId).big_integer().not_null())
.col(ColumnDef::new(UserKeys::ApiKey).string_len(32).not_null()) .col(
ColumnDef::new(UserKeys::ApiKey)
.string_len(32)
.not_null()
.unique_key(),
)
.col(ColumnDef::new(UserKeys::Description).string().not_null()) .col(ColumnDef::new(UserKeys::Description).string().not_null())
.col( .col(
ColumnDef::new(UserKeys::PrivateTxs) ColumnDef::new(UserKeys::PrivateTxs)
@ -119,20 +132,21 @@ impl MigrationTrait for Migration {
.default(true) .default(true)
.not_null(), .not_null(),
) )
.index(
sea_query::Index::create()
.name("idx-user_keys-active")
.col(UserKeys::Active),
)
.foreign_key(
sea_query::ForeignKey::create()
.from(UserKeys::Table, UserKeys::UserId)
.to(User::Table, User::Id),
)
.to_owned(), .to_owned(),
) )
.await?; .await?;
// TODO: make sure from and to aren't backwards // it worked!
manager
.create_foreign_key(
sea_query::ForeignKey::create()
.from(UserKeys::Table, UserKeys::UserId)
.to(User::Table, User::Id)
.to_owned(),
)
.await?;
Ok(()) Ok(())
} }
@ -188,7 +202,6 @@ enum BlockList {
Table, Table,
Id, Id,
Address, Address,
Chain,
Description, Description,
} }