use {
crate::types::ProgramAuthority,
carbon_core::{account::AccountMetadata, postgres::metadata::AccountRowMetadata},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct AuthorityConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authorities: sqlx::types::Json<Vec<ProgramAuthority>>,
}
impl AuthorityConfigRow {
pub fn from_parts(
source: crate::accounts::authority_config::AuthorityConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authorities: sqlx::types::Json(source.authorities.to_vec()),
}
}
}
impl TryFrom<AuthorityConfigRow> for crate::accounts::authority_config::AuthorityConfig {
type Error = carbon_core::error::Error;
fn try_from(source: AuthorityConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
authorities: source
.authorities
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::authority_config::AuthorityConfig
{
fn table() -> &'static str {
"authority_config_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "authorities"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for AuthorityConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO authority_config_account (
"authorities",
__pubkey, __slot
) VALUES (
$1, $2, $3
)"#,
)
.bind(&self.authorities)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for AuthorityConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO authority_config_account (
"authorities",
__pubkey, __slot
) VALUES (
$1, $2, $3
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authorities" = EXCLUDED."authorities",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.authorities)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for AuthorityConfigRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM authority_config_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for AuthorityConfigRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM authority_config_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct AuthorityConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for AuthorityConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS authority_config_account (
-- Account data
"authorities" JSONB NOT NULL,
-- Account metadata
__pubkey BYTEA NOT NULL,
__slot NUMERIC(20),
PRIMARY KEY (__pubkey)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS authority_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}