wip: login, auth working, still need refactoring
This commit is contained in:
@@ -3,18 +3,17 @@ name = "entity"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
default-run = "entity"
|
||||
|
||||
[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",
|
||||
]
|
||||
version = "0.12.10" # sea-orm version
|
||||
features = ["runtime-tokio-native-tls", "sqlx-postgres"]
|
||||
|
||||
43
entity/src/bin/drop_tables.rs
Normal file
43
entity/src/bin/drop_tables.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use sea_orm::{
|
||||
ConnectionTrait, Database, DatabaseConnection, DbConn, EntityTrait, Schema, Statement,
|
||||
};
|
||||
|
||||
use entity::{group, group_permission, permission, user, user_group, user_permission};
|
||||
|
||||
async fn drop_table<E>(db: &DatabaseConnection, entity: E)
|
||||
where
|
||||
E: EntityTrait,
|
||||
{
|
||||
let table_name = entity.table_name();
|
||||
let sql = format!("DROP TABLE IF EXISTS {} CASCADE;", table_name.to_string());
|
||||
|
||||
println!("{}", sql);
|
||||
|
||||
let stmt = Statement::from_string(db.get_database_backend(), sql);
|
||||
|
||||
match db.execute(stmt).await {
|
||||
Ok(_) => println!("Dropped table {}", table_name),
|
||||
Err(e) => println!("Error dropping table {}: {}", table_name, e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn drop_tables(db: &DbConn) {
|
||||
drop_table(db, user::Entity).await;
|
||||
drop_table(db, permission::Entity).await;
|
||||
drop_table(db, group::Entity).await;
|
||||
drop_table(db, user_permission::Entity).await;
|
||||
drop_table(db, group_permission::Entity).await;
|
||||
drop_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!("Dropping tables for entities...");
|
||||
drop_tables(&db).await;
|
||||
}
|
||||
@@ -21,7 +21,7 @@ where
|
||||
let stmt = backend.build(&*table_create_statement);
|
||||
|
||||
match db.execute(stmt).await {
|
||||
Ok(_) => println!("Migrated {}", entity.table_name()),
|
||||
Ok(_) => println!("Created {}", entity.table_name()),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::ActiveValue::{NotSet, Set};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::group::Entity as Group;
|
||||
@@ -12,7 +13,7 @@ pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i64,
|
||||
#[sea_orm(index = "user_usernames")]
|
||||
#[sea_orm(index = "user_usernames", unique)]
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
|
||||
@@ -32,7 +33,16 @@ pub struct Model {
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
impl ActiveModelBehavior for ActiveModel {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
is_active: Set(true),
|
||||
is_staff: Set(false),
|
||||
is_superuser: Set(false),
|
||||
..<Self as ActiveModelTrait>::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<Group> for Entity {
|
||||
// The final relation is User -> UserGroup -> Group
|
||||
|
||||
Reference in New Issue
Block a user