use {
crate::types::UserClass,
carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct AuthorizationListRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub auth_users: Vec<Pubkey>,
pub guardians: Vec<Pubkey>,
pub user_classes: sqlx::types::Json<Vec<UserClass>>,
}
impl AuthorizationListRow {
pub fn from_parts(
source: crate::accounts::authorization_list::AuthorizationList,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
auth_users: source
.auth_users
.into_iter()
.map(|element| element.into())
.collect(),
guardians: source
.guardians
.into_iter()
.map(|element| element.into())
.collect(),
user_classes: sqlx::types::Json(source.user_classes.to_vec()),
}
}
}
impl TryFrom<AuthorizationListRow> for crate::accounts::authorization_list::AuthorizationList {
type Error = carbon_core::error::Error;
fn try_from(source: AuthorizationListRow) -> Result<Self, Self::Error> {
Ok(Self {
auth_users: source
.auth_users
.into_iter()
.map(|element| *element)
.collect(),
guardians: source
.guardians
.into_iter()
.map(|element| *element)
.collect(),
user_classes: source.user_classes.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::authorization_list::AuthorizationList
{
fn table() -> &'static str {
"authorization_list_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"auth_users",
"guardians",
"user_classes",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for AuthorizationListRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO authorization_list_account (
"auth_users",
"guardians",
"user_classes",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(&self.auth_users)
.bind(&self.guardians)
.bind(&self.user_classes)
.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 AuthorizationListRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO authorization_list_account (
"auth_users",
"guardians",
"user_classes",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"auth_users" = EXCLUDED."auth_users",
"guardians" = EXCLUDED."guardians",
"user_classes" = EXCLUDED."user_classes",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.auth_users)
.bind(&self.guardians)
.bind(&self.user_classes)
.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 AuthorizationListRow {
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 authorization_list_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 AuthorizationListRow {
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 authorization_list_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 AuthorizationListMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for AuthorizationListMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS authorization_list_account (
-- Account data
"auth_users" BYTEA[] NOT NULL,
"guardians" BYTEA[] NOT NULL,
"user_classes" 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 authorization_list_account"#)
.execute(connection)
.await?;
Ok(())
}
}