mod.rs 2.13 KiB
//! Using api-level migration api, not SQL-level (like migration_refinery)
//! For following reasons:
//! - Global breaking-changes are not possible from SQL level, because schema in POD is dynamic,
//! different users has different tables, SQL is not Turing-complete, cannot do queries loops, etc
//! - Creating nodes and edges via API ensures all heavy lifting (like creating triggers) is done always
//! and done correctly
//! - Code is cleaner
//!
mod pod_schema;
use async_trait::async_trait;
use tracing::info;
use crate::{
    async_db_connection::{AsyncConnection, AsyncTx},
    error::Result,
    v5::database_api::graph_schema,
use super::schema::SchemaVersion;
/// POD service is responsible for maintenance of POD specific types and CentralSchema
pub async fn update_schema(conn: &mut AsyncConnection) -> Result<()> {
    info!("Checking POD schema definitions for update...");
    conn.in_write_transaction(|tx: AsyncTx| async move {
        let mut schema_info = graph_schema::get_schema_info(&tx).await.unwrap_or_default();
        pod_schema::update_schema(&tx, &mut schema_info).await?;
        graph_schema::store_schema_info(&tx, schema_info).await
    .await
#[async_trait(?Send)]
pub trait Migration {
    async fn migrate(&self, tx: &AsyncTx) -> Result<()> {
        info!("Applying {}...", self.version());
        self.do_migrate(tx).await
    async fn do_migrate(&self, tx: &AsyncTx) -> Result<()>;
    fn version(&self) -> SchemaVersion;
#[cfg(test)]
mod tests {
    use crate::{
        test_helpers::get_memory_connection, v5::database_api::common::tests::get_schema_with_conn,
    use super::*;
    #[tokio::test]
    async fn clean_db_has_pod_types_defined() {
        let mut conn = get_memory_connection().await.unwrap();
        let schema = get_schema_with_conn(&mut conn).await.unwrap();
        assert_eq!(
            schema.info.get("pod").unwrap().version,
            SchemaVersion { major: 0, minor: 2 }
        assert!(schema.nodes().get("PodUserAccount").is_some());
        assert!(schema.nodes().get("PluginRun").is_some());
        assert!(schema.nodes().get("Trigger").is_some());