use {
crate::types::Key,
carbon_core::{account::AccountMetadata, postgres::metadata::AccountRowMetadata},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct EditionMarkerRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: sqlx::types::Json<Key>,
pub ledger: Vec<u8>,
}
impl EditionMarkerRow {
pub fn from_parts(
source: crate::accounts::edition_marker::EditionMarker,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: sqlx::types::Json(source.key),
ledger: source.ledger.to_vec(),
}
}
}
impl TryFrom<EditionMarkerRow> for crate::accounts::edition_marker::EditionMarker {
type Error = carbon_core::error::Error;
fn try_from(source: EditionMarkerRow) -> Result<Self, Self::Error> {
Ok(Self {
key: source.key.0,
ledger: source.ledger.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 31 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::edition_marker::EditionMarker {
fn table() -> &'static str {
"edition_marker_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "key", "ledger"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for EditionMarkerRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO edition_marker_account (
"key",
"ledger",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
)"#,
)
.bind(&self.key)
.bind(&self.ledger)
.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 EditionMarkerRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO edition_marker_account (
"key",
"ledger",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"ledger" = EXCLUDED."ledger",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.key)
.bind(&self.ledger)
.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 EditionMarkerRow {
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 edition_marker_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 EditionMarkerRow {
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 edition_marker_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 EditionMarkerMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for EditionMarkerMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS edition_marker_account (
-- Account data
"key" JSONB NOT NULL,
"ledger" 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 edition_marker_account"#)
.execute(connection)
.await?;
Ok(())
}
}