48 lines
1.0 KiB
Rust
48 lines
1.0 KiB
Rust
pub use sea_orm_migration::prelude::*;
|
|
|
|
pub async fn execute_sql(manager: &SchemaManager<'_>, sql: &str) -> Result<(), DbErr> {
|
|
for stmt in split_sql_statements(sql) {
|
|
if stmt.is_empty() {
|
|
continue;
|
|
}
|
|
manager
|
|
.get_connection()
|
|
.execute_raw(sea_orm::Statement::from_string(
|
|
sea_orm::DbBackend::Postgres,
|
|
stmt,
|
|
))
|
|
.await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn split_sql_statements(sql: &str) -> Vec<&str> {
|
|
sql.split(';')
|
|
.map(|s| s.trim())
|
|
.filter(|s| !s.is_empty())
|
|
.collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_split_simple() {
|
|
let sql = "SELECT 1; SELECT 2; SELECT 3";
|
|
let stmts = split_sql_statements(sql);
|
|
assert_eq!(stmts, &["SELECT 1", "SELECT 2", "SELECT 3"]);
|
|
}
|
|
}
|
|
|
|
pub struct Migrator;
|
|
|
|
#[async_trait::async_trait]
|
|
impl MigratorTrait for Migrator {
|
|
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
|
vec![Box::new(init::Migration)]
|
|
}
|
|
}
|
|
|
|
pub mod init;
|