Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit b4a7c8a

Browse files
author
FreeSynergy
committed
feat(fsn-db): add apply_schema() to DbConnection for idempotent raw SQL schema init
1 parent ae568d2 commit b4a7c8a

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

fsn-db/src/connection.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Database connection management via SeaORM.
22

3-
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
3+
use sea_orm::{ConnectOptions, ConnectionTrait, Database, DatabaseConnection, Statement};
44

55
use fsn_error::FsnError;
66

@@ -69,6 +69,22 @@ impl DbConnection {
6969
&self.backend
7070
}
7171

72+
/// Execute all statements in a schema string, creating tables if they don't exist.
73+
///
74+
/// Safe to call every startup — designed for idempotent `CREATE TABLE IF NOT EXISTS` schemas.
75+
pub async fn apply_schema(&self, schema: &str) -> Result<(), FsnError> {
76+
for stmt in schema.split(';').map(str::trim).filter(|s| !s.is_empty()) {
77+
self.conn
78+
.execute(Statement::from_string(
79+
self.conn.get_database_backend(),
80+
stmt.to_string(),
81+
))
82+
.await
83+
.map_err(|e| FsnError::internal(format!("schema apply: {e}")))?;
84+
}
85+
Ok(())
86+
}
87+
7288
/// Close the connection pool.
7389
pub async fn close(self) -> Result<(), FsnError> {
7490
self.conn

0 commit comments

Comments
 (0)