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

View file

@ -17,6 +17,7 @@ use super::types::{
ExtraFields, ExtraFields,
IdentityProofs, IdentityProofs,
PaymentOptions, PaymentOptions,
PaymentType,
ProfileCreateData, ProfileCreateData,
ProfileUpdateData, ProfileUpdateData,
}; };
@ -488,6 +489,34 @@ pub async fn update_post_count(
Ok(profile) 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)] #[cfg(test)]
mod tests { mod tests {