code: entity, migration, quick migration
This commit is contained in:
20
entity/Cargo.toml
Normal file
20
entity/Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "entity"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "entity"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
tokio = { version = "1.32.0", features = ["full"] }
|
||||
|
||||
[dependencies.sea-orm]
|
||||
version = "0.12.10" # sea-orm version
|
||||
features = [
|
||||
"runtime-tokio-native-tls",
|
||||
"sqlx-postgres",
|
||||
]
|
||||
1
entity/src/lib.rs
Normal file
1
entity/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod user;
|
||||
37
entity/src/main.rs
Normal file
37
entity/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use sea_orm::{DbConn, EntityTrait, Schema, DatabaseConnection, Database, ConnectionTrait};
|
||||
|
||||
mod user;
|
||||
mod permission;
|
||||
|
||||
async fn create_table<E>(db: &DbConn, entity: E)
|
||||
where
|
||||
E: EntityTrait,
|
||||
{
|
||||
let backend = db.get_database_backend();
|
||||
let schema = Schema::new(backend);
|
||||
|
||||
let mut table_create_statement = schema.create_table_from_entity(entity);
|
||||
// we need to shadow the mutable instance X, because if_not_exists() returns &mut X
|
||||
let table_create_statement = table_create_statement.if_not_exists();
|
||||
// we need to reborrow after dereferencing, which transforms our &mut X into &X
|
||||
let stmt = backend.build(&*table_create_statement);
|
||||
|
||||
match db.execute(stmt).await {
|
||||
Ok(_) => println!("Migrated {}", entity.table_name()),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_tables(db: &DbConn) {
|
||||
create_table(db, user::Entity).await;
|
||||
create_table(db, permission::Entity).await;
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Running Entities manually creates the tables from the entities in their latest incarnation.
|
||||
println!("Connecting to database...");
|
||||
let db: DatabaseConnection = Database::connect("postgresql://miniweb:miniweb@localhost:54321/miniweb").await.unwrap();
|
||||
println!("Creating tables for entities...");
|
||||
create_tables(&db).await;
|
||||
}
|
||||
18
entity/src/permission.rs
Normal file
18
entity/src/permission.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "permissions")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i32,
|
||||
#[sea_orm(index = "permission_names")]
|
||||
pub name: String,
|
||||
pub level: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
18
entity/src/user.rs
Normal file
18
entity/src/user.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "users")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i32,
|
||||
pub username: String,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
Reference in New Issue
Block a user