Reset subscriptions if update-current-block command is called with --reset-db parameter

This commit is contained in:
silverpill 2022-07-25 17:30:22 +00:00
parent 33a711b91c
commit 1061220ee1
2 changed files with 42 additions and 2 deletions

View file

@ -22,6 +22,7 @@ use mitra::models::profiles::queries::{
delete_profile,
get_profile_by_actor_id,
get_profile_by_id,
reset_subscriptions,
};
use mitra::models::users::queries::{
create_invite_code,
@ -162,11 +163,21 @@ impl DeleteOrphanedFiles {
struct UpdateCurrentBlock {
#[clap(short)]
number: u64,
#[clap(long)]
reset_db: bool,
}
impl UpdateCurrentBlock {
fn execute(&self, config: &Config) -> Result<(), Error> {
async fn execute(
&self,
config: &Config,
db_client: &impl GenericClient,
) -> Result<(), Error> {
save_current_block_number(&config.storage_dir, self.number)?;
if self.reset_db {
reset_subscriptions(db_client).await?;
};
println!("current block updated");
Ok(())
}
@ -261,7 +272,7 @@ async fn main() {
println!("unused attachments deleted");
},
SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config).unwrap(),
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(),
_ => panic!(),
};
},

View file

@ -17,6 +17,7 @@ use super::types::{
ExtraFields,
IdentityProofs,
PaymentOptions,
PaymentType,
ProfileCreateData,
ProfileUpdateData,
};
@ -488,6 +489,34 @@ pub async fn update_post_count(
Ok(profile)
}
pub async fn reset_subscriptions(
db_client: &impl GenericClient,
) -> Result<(), DatabaseError> {
db_client.execute(
"
UPDATE actor_profile
SET payment_options = '[]'
WHERE
actor_json IS NULL
AND
EXISTS (
SELECT 1
FROM jsonb_array_elements(payment_options) AS option
WHERE CAST(option ->> 'payment_type' AS SMALLINT) = $1
)
",
&[&i16::from(&PaymentType::EthereumSubscription)],
).await?;
db_client.execute(
"
DELETE FROM relationship
WHERE relationship_type = $1
",
&[&RelationshipType::Subscription],
).await?;
db_client.execute("DELETE FROM subscription", &[]).await?;
Ok(())
}
#[cfg(test)]
mod tests {