use std::sync::Arc;
use reinhardt_db::migrations::{Operation, Result};
use reinhardt_testkit::fixtures::{PostgresTableCreator, postgres_table_creator};
use rstest::fixture;
pub struct AdminTableCreator {
postgres_creator: PostgresTableCreator,
admin_db: Arc<reinhardt_admin::core::AdminDatabase>,
}
impl AdminTableCreator {
pub fn new(
postgres_creator: PostgresTableCreator,
admin_db: Arc<reinhardt_admin::core::AdminDatabase>,
) -> Self {
Self {
postgres_creator,
admin_db,
}
}
pub async fn apply(&mut self, schema: Vec<Operation>) -> Result<()> {
self.postgres_creator.apply(schema).await
}
pub fn admin_db(&self) -> &Arc<reinhardt_admin::core::AdminDatabase> {
&self.admin_db
}
pub fn pool(&self) -> &Arc<sqlx::PgPool> {
self.postgres_creator.pool()
}
pub fn url(&self) -> &str {
self.postgres_creator.url()
}
pub fn port(&self) -> u16 {
self.postgres_creator.port()
}
pub fn container(
&self,
) -> &reinhardt_testkit::testcontainers::ContainerAsync<
reinhardt_testkit::testcontainers::GenericImage,
> {
self.postgres_creator.container()
}
pub async fn insert_data(
&self,
table: &str,
columns: Vec<&str>,
values: Vec<Vec<reinhardt_query::prelude::Value>>,
) -> Result<()> {
self.postgres_creator
.insert_data(table, columns, values)
.await
}
pub async fn execute_sql(&self, sql: &str) -> Result<sqlx::postgres::PgQueryResult> {
self.postgres_creator.execute_sql(sql).await
}
pub async fn begin_transaction(&self) -> Result<sqlx::Transaction<'_, sqlx::Postgres>> {
self.postgres_creator.begin_transaction().await
}
}
#[fixture]
pub async fn admin_table_creator(
#[future] postgres_table_creator: PostgresTableCreator,
) -> AdminTableCreator {
use reinhardt_db::DatabaseConnection;
use reinhardt_db::backends::connection::DatabaseConnection as BackendsConnection;
use reinhardt_db::backends::dialect::PostgresBackend;
use std::sync::Arc as StdArc;
let creator = postgres_table_creator.await;
let backend = StdArc::new(PostgresBackend::new((**creator.pool()).clone()));
let backends_conn = BackendsConnection::new(backend);
let connection = DatabaseConnection::new(
reinhardt_db::orm::connection::DatabaseBackend::Postgres,
backends_conn,
);
let admin_db = Arc::new(reinhardt_admin::core::AdminDatabase::new(connection));
AdminTableCreator::new(creator, admin_db)
}