49 lines
1.6 KiB
Rust
49 lines
1.6 KiB
Rust
use sea_orm::{ConnectionTrait, Database, DatabaseConnection, DbConn, EntityTrait, Schema};
|
|
|
|
mod group;
|
|
mod group_permission;
|
|
mod permission;
|
|
mod user;
|
|
mod user_group;
|
|
mod user_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!("Created {}", 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;
|
|
create_table(db, group::Entity).await;
|
|
create_table(db, user_permission::Entity).await;
|
|
create_table(db, group_permission::Entity).await;
|
|
create_table(db, user_group::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;
|
|
}
|