Added agent

This commit is contained in:
Ultradesu
2025-08-26 13:08:43 +03:00
parent 98ba8846c3
commit a2eabe2c69
13 changed files with 1089 additions and 105 deletions

View File

@@ -47,17 +47,30 @@ pub async fn migrate_database(db: &DatabaseConnection) -> Result<(), DbErr> {
let schema = Schema::new(backend);
// Create nodes table if it doesn't exist
let mut create_table_stmt = schema.create_table_from_entity(crate::database::entities::node::Entity);
let mut create_nodes_stmt = schema.create_table_from_entity(crate::database::entities::node::Entity);
// Convert to SQL
let sql = match backend {
DbBackend::Sqlite => create_table_stmt.if_not_exists().to_string(SqliteQueryBuilder),
DbBackend::Postgres => create_table_stmt.if_not_exists().to_string(PostgresQueryBuilder),
DbBackend::MySql => create_table_stmt.if_not_exists().to_string(MysqlQueryBuilder),
let nodes_sql = match backend {
DbBackend::Sqlite => create_nodes_stmt.if_not_exists().to_string(SqliteQueryBuilder),
DbBackend::Postgres => create_nodes_stmt.if_not_exists().to_string(PostgresQueryBuilder),
DbBackend::MySql => create_nodes_stmt.if_not_exists().to_string(MysqlQueryBuilder),
};
// Execute the statement
db.execute(Statement::from_string(backend, sql)).await?;
db.execute(Statement::from_string(backend, nodes_sql)).await?;
// Create settings table if it doesn't exist
let mut create_settings_stmt = schema.create_table_from_entity(crate::database::entities::settings::Entity);
// Convert to SQL
let settings_sql = match backend {
DbBackend::Sqlite => create_settings_stmt.if_not_exists().to_string(SqliteQueryBuilder),
DbBackend::Postgres => create_settings_stmt.if_not_exists().to_string(PostgresQueryBuilder),
DbBackend::MySql => create_settings_stmt.if_not_exists().to_string(MysqlQueryBuilder),
};
// Execute the statement
db.execute(Statement::from_string(backend, settings_sql)).await?;
tracing::info!("Database migration completed");
Ok(())

View File

@@ -1 +1,2 @@
pub mod node;
pub mod node;
pub mod settings;

View File

@@ -0,0 +1,47 @@
use sea_orm::entity::prelude::*;
use sea_orm::Set;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "settings")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub key: String,
pub value: String,
pub created_at: DateTime,
pub updated_at: DateTime,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl Model {
pub fn parse_json_value<T>(&self) -> Result<T, serde_json::Error>
where
T: for<'de> serde::Deserialize<'de>
{
serde_json::from_str(&self.value)
}
}
impl ActiveModel {
pub fn new(key: String, value: &impl serde::Serialize) -> Result<Self, serde_json::Error> {
let value_json = serde_json::to_string(value)?;
let now = chrono::Utc::now().naive_utc();
Ok(Self {
key: Set(key),
value: Set(value_json),
created_at: Set(now),
updated_at: Set(now),
})
}
pub fn update_value(&mut self, value: &impl serde::Serialize) -> Result<(), serde_json::Error> {
let value_json = serde_json::to_string(value)?;
self.value = Set(value_json);
self.updated_at = Set(chrono::Utc::now().naive_utc());
Ok(())
}
}