Add reset-subscriptions CLI command
This commit is contained in:
parent
98564c2573
commit
79e161c131
4 changed files with 59 additions and 38 deletions
|
@ -32,6 +32,7 @@ async fn main() {
|
|||
SubCommand::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(),
|
||||
SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
|
||||
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(),
|
||||
SubCommand::ResetSubscriptions(cmd) => cmd.execute(&config, db_client).await.unwrap(),
|
||||
SubCommand::CreateMoneroWallet(cmd) => cmd.execute(&config).await.unwrap(),
|
||||
_ => panic!(),
|
||||
};
|
||||
|
|
31
src/cli.rs
31
src/cli.rs
|
@ -19,8 +19,8 @@ use crate::models::profiles::queries::{
|
|||
delete_profile,
|
||||
get_profile_by_actor_id,
|
||||
get_profile_by_id,
|
||||
reset_subscriptions,
|
||||
};
|
||||
use crate::models::subscriptions::queries::reset_subscriptions;
|
||||
use crate::models::users::queries::{
|
||||
create_invite_code,
|
||||
get_invite_codes,
|
||||
|
@ -51,6 +51,7 @@ pub enum SubCommand {
|
|||
DeleteUnusedAttachments(DeleteUnusedAttachments),
|
||||
DeleteOrphanedFiles(DeleteOrphanedFiles),
|
||||
UpdateCurrentBlock(UpdateCurrentBlock),
|
||||
ResetSubscriptions(ResetSubscriptions),
|
||||
CreateMoneroWallet(CreateMoneroWallet),
|
||||
}
|
||||
|
||||
|
@ -272,26 +273,40 @@ impl DeleteOrphanedFiles {
|
|||
#[derive(Parser)]
|
||||
pub struct UpdateCurrentBlock {
|
||||
number: u64,
|
||||
|
||||
#[clap(long)]
|
||||
reset_db: bool,
|
||||
}
|
||||
|
||||
impl UpdateCurrentBlock {
|
||||
pub async fn execute(
|
||||
&self,
|
||||
config: &Config,
|
||||
db_client: &impl GenericClient,
|
||||
_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(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset all subscriptions
|
||||
/// (can be used during development or when switching between chains)
|
||||
#[derive(Parser)]
|
||||
pub struct ResetSubscriptions {
|
||||
#[clap(long)]
|
||||
ethereum_contract_replaced: bool,
|
||||
}
|
||||
|
||||
impl ResetSubscriptions {
|
||||
pub async fn execute(
|
||||
&self,
|
||||
_config: &Config,
|
||||
db_client: &impl GenericClient,
|
||||
) -> Result<(), Error> {
|
||||
reset_subscriptions(db_client, self.ethereum_contract_replaced).await?;
|
||||
println!("subscriptions deleted");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Monero wallet
|
||||
#[derive(Parser)]
|
||||
pub struct CreateMoneroWallet {
|
||||
|
|
|
@ -18,7 +18,6 @@ use super::types::{
|
|||
ExtraFields,
|
||||
IdentityProofs,
|
||||
PaymentOptions,
|
||||
PaymentType,
|
||||
ProfileCreateData,
|
||||
ProfileUpdateData,
|
||||
};
|
||||
|
@ -520,35 +519,6 @@ 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 {
|
||||
use serial_test::serial;
|
||||
|
|
|
@ -6,6 +6,7 @@ use uuid::Uuid;
|
|||
|
||||
use crate::database::catch_unique_violation;
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::profiles::types::PaymentType;
|
||||
use crate::models::relationships::queries::{subscribe, subscribe_opt};
|
||||
use crate::models::relationships::types::RelationshipType;
|
||||
use crate::utils::caip2::ChainId;
|
||||
|
@ -148,6 +149,40 @@ pub async fn get_incoming_subscriptions(
|
|||
Ok(subscriptions)
|
||||
}
|
||||
|
||||
pub async fn reset_subscriptions(
|
||||
db_client: &impl GenericClient,
|
||||
ethereum_contract_replaced: bool,
|
||||
) -> Result<(), DatabaseError> {
|
||||
if ethereum_contract_replaced {
|
||||
// Ethereum subscription configuration is stored in contract.
|
||||
// If contract is replaced, payment option needs to be deleted.
|
||||
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 {
|
||||
use serial_test::serial;
|
||||
|
|
Loading…
Reference in a new issue