use crate::db::{tables::IndexerSchema, IndexerSchemaDbResult};
use fuel_indexer_database::{queries, IndexerConnection, IndexerConnectionPool};
use fuel_indexer_lib::graphql::GraphQLSchema;
use tracing::info;
pub struct SchemaManager {
pool: IndexerConnectionPool,
}
impl SchemaManager {
pub fn new(pool: IndexerConnectionPool) -> SchemaManager {
SchemaManager { pool }
}
pub async fn new_schema(
&self,
namespace: &str,
identifier: &str,
schema: GraphQLSchema,
conn: &mut IndexerConnection,
) -> IndexerSchemaDbResult<()> {
let version = schema.version();
if !queries::schema_exists(conn, namespace, identifier, version).await? {
info!("SchemaManager creating schema for Indexer({namespace}.{identifier}) with Version({version}).");
let _ = IndexerSchema::new(
namespace,
identifier,
&schema,
self.pool.database_type(),
)?
.commit(&schema, conn)
.await?;
}
Ok(())
}
pub async fn load_schema(
&self,
namespace: &str,
identifier: &str,
) -> IndexerSchemaDbResult<IndexerSchema> {
info!("SchemaManager loading schema for Indexer({namespace}.{identifier}).");
IndexerSchema::load(&self.pool, namespace, identifier).await
}
}