use {
crate::types::Key,
carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct GroupV1Row {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: sqlx::types::Json<Key>,
pub update_authority: Pubkey,
pub name: String,
pub uri: String,
pub collections: Vec<Pubkey>,
pub groups: Vec<Pubkey>,
pub parent_groups: Vec<Pubkey>,
pub assets: Vec<Pubkey>,
}
impl GroupV1Row {
pub fn from_parts(
source: crate::accounts::group_v1::GroupV1,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: sqlx::types::Json(source.key),
update_authority: source.update_authority.into(),
name: source.name,
uri: source.uri,
collections: source
.collections
.into_iter()
.map(|element| element.into())
.collect(),
groups: source
.groups
.into_iter()
.map(|element| element.into())
.collect(),
parent_groups: source
.parent_groups
.into_iter()
.map(|element| element.into())
.collect(),
assets: source
.assets
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<GroupV1Row> for crate::accounts::group_v1::GroupV1 {
type Error = carbon_core::error::Error;
fn try_from(source: GroupV1Row) -> Result<Self, Self::Error> {
Ok(Self {
key: source.key.0,
update_authority: *source.update_authority,
name: source.name,
uri: source.uri,
collections: source
.collections
.into_iter()
.map(|element| *element)
.collect(),
groups: source.groups.into_iter().map(|element| *element).collect(),
parent_groups: source
.parent_groups
.into_iter()
.map(|element| *element)
.collect(),
assets: source.assets.into_iter().map(|element| *element).collect(),
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::group_v1::GroupV1 {
fn table() -> &'static str {
"group_v1_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"key",
"update_authority",
"name",
"uri",
"collections",
"groups",
"parent_groups",
"assets",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for GroupV1Row {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO group_v1_account (
"key",
"update_authority",
"name",
"uri",
"collections",
"groups",
"parent_groups",
"assets",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)"#,
)
.bind(&self.key)
.bind(self.update_authority)
.bind(&self.name)
.bind(&self.uri)
.bind(&self.collections)
.bind(&self.groups)
.bind(&self.parent_groups)
.bind(&self.assets)
.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 GroupV1Row {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO group_v1_account (
"key",
"update_authority",
"name",
"uri",
"collections",
"groups",
"parent_groups",
"assets",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"update_authority" = EXCLUDED."update_authority",
"name" = EXCLUDED."name",
"uri" = EXCLUDED."uri",
"collections" = EXCLUDED."collections",
"groups" = EXCLUDED."groups",
"parent_groups" = EXCLUDED."parent_groups",
"assets" = EXCLUDED."assets",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.key)
.bind(self.update_authority)
.bind(&self.name)
.bind(&self.uri)
.bind(&self.collections)
.bind(&self.groups)
.bind(&self.parent_groups)
.bind(&self.assets)
.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 GroupV1Row {
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 group_v1_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 GroupV1Row {
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 group_v1_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 GroupV1MigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for GroupV1MigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS group_v1_account (
-- Account data
"key" JSONB NOT NULL,
"update_authority" BYTEA NOT NULL,
"name" TEXT NOT NULL,
"uri" TEXT NOT NULL,
"collections" BYTEA[] NOT NULL,
"groups" BYTEA[] NOT NULL,
"parent_groups" BYTEA[] NOT NULL,
"assets" BYTEA[] 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 group_v1_account"#)
.execute(connection)
.await?;
Ok(())
}
}