From ca263834e93814105ca9ea77ab213cff0bc95faa Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Wed, 17 May 2023 21:28:37 +0100 Subject: [PATCH] Restructure account commands to account subcommand (#984) * Stop running triggers on history delete * Move to account management dir * Alter trigger function to only run for inserts * wip * Add atuin account subcommands, and re-org delete * Clarify docs * Delete silly dupe migration * Um where did this come from * Oops, insert only plz --- atuin-client/src/api_client.rs | 2 +- .../20230515221038_trigger-delete-only.sql | 30 ++++++++++++++ atuin/src/command/client.rs | 10 +++++ atuin/src/command/client/account.rs | 41 +++++++++++++++++++ .../client/{sync => account}/delete.rs | 0 .../command/client/{sync => account}/login.rs | 0 .../client/{sync => account}/logout.rs | 0 .../client/{sync => account}/register.rs | 0 atuin/src/command/client/sync.rs | 16 +++----- atuin/src/command/mod.rs | 1 + docs/docs/commands/sync.md | 4 +- 11 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 atuin-server/migrations/20230515221038_trigger-delete-only.sql create mode 100644 atuin/src/command/client/account.rs rename atuin/src/command/client/{sync => account}/delete.rs (100%) rename atuin/src/command/client/{sync => account}/login.rs (100%) rename atuin/src/command/client/{sync => account}/logout.rs (100%) rename atuin/src/command/client/{sync => account}/register.rs (100%) diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 2abb815..5ea84b9 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -219,7 +219,7 @@ impl<'a> Client<'a> { } pub async fn delete(&self) -> Result<()> { - let url = format!("{}/register", self.sync_addr); + let url = format!("{}/account", self.sync_addr); let url = Url::parse(url.as_str())?; let resp = self.client.delete(url).send().await?; diff --git a/atuin-server/migrations/20230515221038_trigger-delete-only.sql b/atuin-server/migrations/20230515221038_trigger-delete-only.sql new file mode 100644 index 0000000..3d0bba5 --- /dev/null +++ b/atuin-server/migrations/20230515221038_trigger-delete-only.sql @@ -0,0 +1,30 @@ +-- We do not need to run the trigger on deletes, as the only time we are deleting history is when the user +-- has already been deleted +-- This actually slows down deleting all the history a good bit! + +create or replace function user_history_count() +returns trigger as +$func$ +begin + if (TG_OP='INSERT') then + update total_history_count_user set total = total + 1 where user_id = new.user_id; + + if not found then + insert into total_history_count_user(user_id, total) + values ( + new.user_id, + (select count(1) from history where user_id = new.user_id) + ); + end if; + end if; + + return NEW; -- this is actually ignored for an after trigger, but oh well +end; +$func$ +language plpgsql volatile -- pldfplplpflh +cost 100; -- default value + +create or replace trigger tg_user_history_count + after insert on history + for each row + execute procedure user_history_count(); diff --git a/atuin/src/command/client.rs b/atuin/src/command/client.rs index 2a82563..6a2d868 100644 --- a/atuin/src/command/client.rs +++ b/atuin/src/command/client.rs @@ -9,6 +9,9 @@ use env_logger::Builder; #[cfg(feature = "sync")] mod sync; +#[cfg(feature = "sync")] +mod account; + mod history; mod import; mod search; @@ -34,6 +37,9 @@ pub enum Cmd { #[cfg(feature = "sync")] #[command(flatten)] Sync(sync::Cmd), + + #[cfg(feature = "sync")] + Account(account::Cmd), } impl Cmd { @@ -54,8 +60,12 @@ impl Cmd { Self::Import(import) => import.run(&mut db).await, Self::Stats(stats) => stats.run(&mut db, &settings).await, Self::Search(search) => search.run(db, &mut settings).await, + #[cfg(feature = "sync")] Self::Sync(sync) => sync.run(settings, &mut db).await, + + #[cfg(feature = "sync")] + Self::Account(account) => account.run(settings).await, } } } diff --git a/atuin/src/command/client/account.rs b/atuin/src/command/client/account.rs new file mode 100644 index 0000000..2a4a077 --- /dev/null +++ b/atuin/src/command/client/account.rs @@ -0,0 +1,41 @@ +use clap::{Args, Subcommand}; +use eyre::Result; + +use atuin_client::settings::Settings; + +pub mod delete; +pub mod login; +pub mod logout; +pub mod register; + +#[derive(Args)] +pub struct Cmd { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +pub enum Commands { + /// Login to the configured server + Login(login::Cmd), + + // Register a new account + Register(register::Cmd), + + /// Log out + Logout, + + // Delete your account, and all synced data + Delete, +} + +impl Cmd { + pub async fn run(self, settings: Settings) -> Result<()> { + match self.command { + Commands::Login(l) => l.run(&settings).await, + Commands::Register(r) => r.run(&settings).await, + Commands::Logout => logout::run(&settings), + Commands::Delete => delete::run(&settings).await, + } + } +} diff --git a/atuin/src/command/client/sync/delete.rs b/atuin/src/command/client/account/delete.rs similarity index 100% rename from atuin/src/command/client/sync/delete.rs rename to atuin/src/command/client/account/delete.rs diff --git a/atuin/src/command/client/sync/login.rs b/atuin/src/command/client/account/login.rs similarity index 100% rename from atuin/src/command/client/sync/login.rs rename to atuin/src/command/client/account/login.rs diff --git a/atuin/src/command/client/sync/logout.rs b/atuin/src/command/client/account/logout.rs similarity index 100% rename from atuin/src/command/client/sync/logout.rs rename to atuin/src/command/client/account/logout.rs diff --git a/atuin/src/command/client/sync/register.rs b/atuin/src/command/client/account/register.rs similarity index 100% rename from atuin/src/command/client/sync/register.rs rename to atuin/src/command/client/account/register.rs diff --git a/atuin/src/command/client/sync.rs b/atuin/src/command/client/sync.rs index 12664be..061b737 100644 --- a/atuin/src/command/client/sync.rs +++ b/atuin/src/command/client/sync.rs @@ -3,12 +3,10 @@ use eyre::{Result, WrapErr}; use atuin_client::{database::Database, settings::Settings}; -mod delete; -mod login; -mod logout; -mod register; mod status; +use crate::command::client::account; + #[derive(Subcommand)] #[command(infer_subcommands = true)] pub enum Cmd { @@ -20,16 +18,13 @@ pub enum Cmd { }, /// Login to the configured server - Login(login::Cmd), + Login(account::login::Cmd), /// Log out Logout, /// Register with the configured server - Register(register::Cmd), - - /// Unregister with the configured server - Unregister, + Register(account::register::Cmd), /// Print the encryption key for transfer to another machine Key { @@ -46,9 +41,8 @@ impl Cmd { match self { Self::Sync { force } => run(&settings, force, db).await, Self::Login(l) => l.run(&settings).await, - Self::Logout => logout::run(&settings), + Self::Logout => account::logout::run(&settings), Self::Register(r) => r.run(&settings).await, - Self::Unregister => delete::run(&settings).await, Self::Status => status::run(&settings, db).await, Self::Key { base64 } => { use atuin_client::encryption::{encode_key, load_key}; diff --git a/atuin/src/command/mod.rs b/atuin/src/command/mod.rs index 4ed1691..bcd209d 100644 --- a/atuin/src/command/mod.rs +++ b/atuin/src/command/mod.rs @@ -49,6 +49,7 @@ impl AtuinCmd { match self { #[cfg(feature = "client")] Self::Client(client) => client.run(), + #[cfg(feature = "server")] Self::Server(server) => server.run(), Self::Contributors => { diff --git a/docs/docs/commands/sync.md b/docs/docs/commands/sync.md index 8cd12c5..21d4133 100644 --- a/docs/docs/commands/sync.md +++ b/docs/docs/commands/sync.md @@ -40,9 +40,11 @@ here! You can delete your sync account with ``` -atuin unregister +atuin account delete ``` +This will remove your account and all synchronized history from the server. Local data will not be touched! + ## Key As all your data is encrypted, Atuin generates a key for you. It's stored in the