By Guest
Unnamed Paste
Auto Detect
3.98 KiB
22
6 days ago
use sea_orm_migration::{prelude::*, sea_query::extension::postgres::Type};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_type(
Type::create()
.as_enum(Role::Type)
.values(vec![Role::User, Role::Admin])
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Users::Table)
.col(
ColumnDef::new(Users::Id)
.string()
.not_null()
.unique_key()
.primary_key(),
)
.col(
ColumnDef::new(Users::Email)
.string_len(320)
.unique_key()
.not_null(),
)
.col(
ColumnDef::new(Users::Username)
.string_len(32)
.unique_key()
.not_null(),
)
.col(ColumnDef::new(Users::Password).string_len(128).not_null())
.col(
ColumnDef::new(Users::Verified)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Users::Role)
.custom(Role::Type)
.default("user")
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Applications::Table)
.col(
ColumnDef::new(Applications::Id)
.string()
.not_null()
.unique_key()
.primary_key(),
)
.col(ColumnDef::new(Applications::UserId).string().not_null())
.col(ColumnDef::new(Applications::Name).string_len(16).not_null())
.col(
ColumnDef::new(Applications::LastAccessed)
.timestamp_with_time_zone()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.from(Applications::Table, Applications::UserId)
.to(Users::Table, Users::Id),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(Applications::Table)
.col(Applications::UserId)
.col(Applications::Name)
.unique()
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Users::Table).to_owned())
.await?;
manager
.drop_type(Type::drop().name(Role::Type).to_owned())
.await
}
}
#[derive(Iden)]
enum Role {
#[iden = "role"]
Type,
User,
Admin,
}
#[derive(Iden)]
enum Users {
Table,
Id,
Email,
Username,
Password,
Verified,
Role,
}
#[derive(Iden)]
enum Applications {
Table,
Id,
UserId,
Name,
LastAccessed,
}