This commit is contained in:
2025-08-20 00:42:01 +08:00
commit 06bb5439b4
73 changed files with 30196 additions and 0 deletions

26
migration/src/lib.rs Normal file
View File

@ -0,0 +1,26 @@
pub use sea_orm_migration::prelude::*;
mod m20240101_000001_create_users_table;
mod m20240101_000002_create_roles_table;
mod m20240101_000003_create_permissions_table;
mod m20240101_000004_create_menus_table;
mod m20240101_000005_create_user_roles_table;
mod m20240101_000006_create_role_permissions_table;
mod m20240101_000007_insert_admin_user;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20240101_000001_create_users_table::Migration),
Box::new(m20240101_000002_create_roles_table::Migration),
Box::new(m20240101_000003_create_permissions_table::Migration),
Box::new(m20240101_000004_create_menus_table::Migration),
Box::new(m20240101_000005_create_user_roles_table::Migration),
Box::new(m20240101_000006_create_role_permissions_table::Migration),
Box::new(m20240101_000007_insert_admin_user::Migration),
]
}
}

View File

@ -0,0 +1,84 @@
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> {
manager
.create_table(
Table::create()
.table(Users::Table)
.if_not_exists()
.col(
ColumnDef::new(Users::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Users::Username)
.string()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Users::Email)
.string()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Users::PasswordHash)
.string()
.not_null(),
)
.col(ColumnDef::new(Users::Nickname).string())
.col(ColumnDef::new(Users::Avatar).string())
.col(ColumnDef::new(Users::Phone).string())
.col(
ColumnDef::new(Users::Status)
.tiny_integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(Users::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Users::UpdatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Users::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
Username,
Email,
PasswordHash,
Nickname,
Avatar,
Phone,
Status,
CreatedAt,
UpdatedAt,
}

View File

@ -0,0 +1,67 @@
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> {
manager
.create_table(
Table::create()
.table(Roles::Table)
.if_not_exists()
.col(
ColumnDef::new(Roles::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Roles::Name)
.string()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(Roles::Description).text())
.col(
ColumnDef::new(Roles::Status)
.tiny_integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(Roles::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Roles::UpdatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Roles::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Roles {
Table,
Id,
Name,
Description,
Status,
CreatedAt,
UpdatedAt,
}

View File

@ -0,0 +1,86 @@
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> {
manager
.create_table(
Table::create()
.table(Permissions::Table)
.if_not_exists()
.col(
ColumnDef::new(Permissions::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Permissions::Name)
.string()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Permissions::Key)
.string()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(Permissions::Description).text())
.col(
ColumnDef::new(Permissions::Resource)
.string()
.not_null(),
)
.col(
ColumnDef::new(Permissions::Action)
.string()
.not_null(),
)
.col(
ColumnDef::new(Permissions::Status)
.tiny_integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(Permissions::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Permissions::UpdatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Permissions::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Permissions {
Table,
Id,
Name,
Key,
Description,
Resource,
Action,
Status,
CreatedAt,
UpdatedAt,
}

View File

@ -0,0 +1,103 @@
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> {
manager
.create_table(
Table::create()
.table(Menus::Table)
.if_not_exists()
.col(
ColumnDef::new(Menus::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Menus::ParentId).integer())
.col(
ColumnDef::new(Menus::Name)
.string()
.not_null(),
)
.col(ColumnDef::new(Menus::Path).string())
.col(ColumnDef::new(Menus::Component).string())
.col(ColumnDef::new(Menus::Icon).string())
.col(
ColumnDef::new(Menus::SortOrder)
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Menus::MenuType)
.tiny_integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(Menus::Visible)
.tiny_integer()
.not_null()
.default(1),
)
.col(
ColumnDef::new(Menus::Status)
.tiny_integer()
.not_null()
.default(1),
)
.col(ColumnDef::new(Menus::PermissionKey).string())
.col(
ColumnDef::new(Menus::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Menus::UpdatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_menus_parent_id")
.from(Menus::Table, Menus::ParentId)
.to(Menus::Table, Menus::Id)
.on_delete(ForeignKeyAction::SetNull)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Menus::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Menus {
Table,
Id,
ParentId,
Name,
Path,
Component,
Icon,
SortOrder,
MenuType,
Visible,
Status,
PermissionKey,
CreatedAt,
UpdatedAt,
}

View File

@ -0,0 +1,91 @@
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> {
manager
.create_table(
Table::create()
.table(UserRoles::Table)
.if_not_exists()
.col(
ColumnDef::new(UserRoles::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(UserRoles::UserId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(UserRoles::RoleId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(UserRoles::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_user_roles_user_id")
.from(UserRoles::Table, UserRoles::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_user_roles_role_id")
.from(UserRoles::Table, UserRoles::RoleId)
.to(Roles::Table, Roles::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("idx_user_roles_unique")
.col(UserRoles::UserId)
.col(UserRoles::RoleId)
.unique(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(UserRoles::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum UserRoles {
Table,
Id,
UserId,
RoleId,
CreatedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[derive(DeriveIden)]
enum Roles {
Table,
Id,
}

View File

@ -0,0 +1,91 @@
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> {
manager
.create_table(
Table::create()
.table(RolePermissions::Table)
.if_not_exists()
.col(
ColumnDef::new(RolePermissions::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(RolePermissions::RoleId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(RolePermissions::PermissionId)
.integer()
.not_null(),
)
.col(
ColumnDef::new(RolePermissions::CreatedAt)
.timestamp()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_role_permissions_role_id")
.from(RolePermissions::Table, RolePermissions::RoleId)
.to(Roles::Table, Roles::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_role_permissions_permission_id")
.from(RolePermissions::Table, RolePermissions::PermissionId)
.to(Permissions::Table, Permissions::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("idx_role_permissions_unique")
.col(RolePermissions::RoleId)
.col(RolePermissions::PermissionId)
.unique(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(RolePermissions::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum RolePermissions {
Table,
Id,
RoleId,
PermissionId,
CreatedAt,
}
#[derive(DeriveIden)]
enum Roles {
Table,
Id,
}
#[derive(DeriveIden)]
enum Permissions {
Table,
Id,
}

View File

@ -0,0 +1,63 @@
use sea_orm_migration::prelude::*;
use bcrypt::{hash, DEFAULT_COST};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 对密码进行哈希加密
let password_hash = hash("123456", DEFAULT_COST)
.map_err(|e| DbErr::Custom(format!("Failed to hash password: {}", e)))?;
// 插入admin用户
let insert_stmt = Query::insert()
.into_table(Users::Table)
.columns([
Users::Username,
Users::Email,
Users::PasswordHash,
Users::Status,
Users::CreatedAt,
Users::UpdatedAt,
])
.values_panic([
"admin".into(),
"admin@example.com".into(),
password_hash.into(),
1.into(),
Expr::current_timestamp().into(),
Expr::current_timestamp().into(),
])
.to_owned();
manager.exec_stmt(insert_stmt).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 删除admin用户
let delete_stmt = Query::delete()
.from_table(Users::Table)
.and_where(Expr::col(Users::Username).eq("admin"))
.to_owned();
manager.exec_stmt(delete_stmt).await?;
Ok(())
}
}
#[derive(Iden)]
enum Users {
Table,
Id,
Username,
Email,
PasswordHash,
Status,
CreatedAt,
UpdatedAt,
}

6
migration/src/main.rs Normal file
View File

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}