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::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(),
|
||||||
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, 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(),
|
SubCommand::CreateMoneroWallet(cmd) => cmd.execute(&config).await.unwrap(),
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
};
|
};
|
||||||
|
|
31
src/cli.rs
31
src/cli.rs
|
@ -19,8 +19,8 @@ use crate::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 crate::models::subscriptions::queries::reset_subscriptions;
|
||||||
use crate::models::users::queries::{
|
use crate::models::users::queries::{
|
||||||
create_invite_code,
|
create_invite_code,
|
||||||
get_invite_codes,
|
get_invite_codes,
|
||||||
|
@ -51,6 +51,7 @@ pub enum SubCommand {
|
||||||
DeleteUnusedAttachments(DeleteUnusedAttachments),
|
DeleteUnusedAttachments(DeleteUnusedAttachments),
|
||||||
DeleteOrphanedFiles(DeleteOrphanedFiles),
|
DeleteOrphanedFiles(DeleteOrphanedFiles),
|
||||||
UpdateCurrentBlock(UpdateCurrentBlock),
|
UpdateCurrentBlock(UpdateCurrentBlock),
|
||||||
|
ResetSubscriptions(ResetSubscriptions),
|
||||||
CreateMoneroWallet(CreateMoneroWallet),
|
CreateMoneroWallet(CreateMoneroWallet),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,26 +273,40 @@ impl DeleteOrphanedFiles {
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct UpdateCurrentBlock {
|
pub struct UpdateCurrentBlock {
|
||||||
number: u64,
|
number: u64,
|
||||||
|
|
||||||
#[clap(long)]
|
|
||||||
reset_db: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UpdateCurrentBlock {
|
impl UpdateCurrentBlock {
|
||||||
pub async fn execute(
|
pub async fn execute(
|
||||||
&self,
|
&self,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &impl GenericClient,
|
_db_client: &impl GenericClient,
|
||||||
) -> Result<(), Error> {
|
) -> 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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Create Monero wallet
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct CreateMoneroWallet {
|
pub struct CreateMoneroWallet {
|
||||||
|
|
|
@ -18,7 +18,6 @@ use super::types::{
|
||||||
ExtraFields,
|
ExtraFields,
|
||||||
IdentityProofs,
|
IdentityProofs,
|
||||||
PaymentOptions,
|
PaymentOptions,
|
||||||
PaymentType,
|
|
||||||
ProfileCreateData,
|
ProfileCreateData,
|
||||||
ProfileUpdateData,
|
ProfileUpdateData,
|
||||||
};
|
};
|
||||||
|
@ -520,35 +519,6 @@ 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 {
|
||||||
use serial_test::serial;
|
use serial_test::serial;
|
||||||
|
|
|
@ -6,6 +6,7 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::database::catch_unique_violation;
|
use crate::database::catch_unique_violation;
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
|
use crate::models::profiles::types::PaymentType;
|
||||||
use crate::models::relationships::queries::{subscribe, subscribe_opt};
|
use crate::models::relationships::queries::{subscribe, subscribe_opt};
|
||||||
use crate::models::relationships::types::RelationshipType;
|
use crate::models::relationships::types::RelationshipType;
|
||||||
use crate::utils::caip2::ChainId;
|
use crate::utils::caip2::ChainId;
|
||||||
|
@ -148,6 +149,40 @@ pub async fn get_incoming_subscriptions(
|
||||||
Ok(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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use serial_test::serial;
|
use serial_test::serial;
|
||||||
|
|
Loading…
Reference in a new issue