You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9 KiB

use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(Iden)]
enum Link {
Table,
Id,
Target,
Source,
UserId,
}
#[derive(Iden)]
enum Click {
Table,
Id,
LinkId,
Time,
}
#[derive(Iden)]
enum User {
Table,
Id,
Name,
Password,
IsAdmin,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
manager
.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(User::Name).string().unique_key().not_null())
.col(ColumnDef::new(User::Password).string())
.col(
ColumnDef::new(User::IsAdmin)
.boolean()
.default(false)
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Link::Table)
.if_not_exists()
.col(
ColumnDef::new(Link::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Link::Target).string().not_null())
.col(ColumnDef::new(Link::Source).string().not_null())
.col(ColumnDef::new(Link::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_user_link")
.from(Link::Table, Link::UserId)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Click::Table)
.if_not_exists()
.col(
ColumnDef::new(Click::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Click::Time).timestamp())
.col(ColumnDef::new(Click::LinkId).integer())
.foreign_key(
ForeignKey::create()
.name("FK_link_click")
.from(Click::Table, Click::LinkId)
.to(Link::Table, Link::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Click::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Link::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await?;
Ok(())
}
}