aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/bin')
-rw-r--r--rust/src/bin/fatcat-auth.rs2
-rw-r--r--rust/src/bin/fatcat-doctor.rs96
-rw-r--r--rust/src/bin/fatcat-export.rs14
-rw-r--r--rust/src/bin/fatcatd.rs4
4 files changed, 106 insertions, 10 deletions
diff --git a/rust/src/bin/fatcat-auth.rs b/rust/src/bin/fatcat-auth.rs
index 0b5c05b0..c351848e 100644
--- a/rust/src/bin/fatcat-auth.rs
+++ b/rust/src/bin/fatcat-auth.rs
@@ -89,7 +89,7 @@ fn main() -> Result<()> {
};
let editor_row = editor.db_create(&db_conn)?;
//println!("{:?}", editor);
- println!("{}", FatcatId::from_uuid(&editor_row.id).to_string());
+ println!("{}", FatcatId::from_uuid(&editor_row.id));
}
("create-token", Some(subm)) => {
let editor_id = FatcatId::from_str(subm.value_of("editor-id").unwrap())?;
diff --git a/rust/src/bin/fatcat-doctor.rs b/rust/src/bin/fatcat-doctor.rs
new file mode 100644
index 00000000..7255a22f
--- /dev/null
+++ b/rust/src/bin/fatcat-doctor.rs
@@ -0,0 +1,96 @@
+//! Database cleanup tool
+
+use clap::{value_t_or_exit, App, SubCommand};
+
+use fatcat::database_models::*;
+use fatcat::database_schema::*;
+use fatcat::errors::Result;
+use fatcat::identifiers::FatcatId;
+use fatcat::server;
+use fatcat::server::DbConn;
+use std::process;
+use std::str::FromStr;
+
+use diesel::prelude::*;
+
+fn backfill_changelog_gap(conn: &DbConn, last_good: i64, max_index: i64) -> Result<()> {
+ // sanity check arguments against database
+ assert!(last_good > 0);
+ assert!(max_index > 0);
+ assert!(last_good < max_index);
+ let highest_row: ChangelogRow = changelog::table.order(changelog::id.desc()).first(conn)?;
+ assert!(highest_row.id >= max_index);
+
+ // default values
+ // 'root' editor_id is aaaaaaaaaaaabkvkaaaaaaaaae
+ // 'admin' editor_id is aaaaaaaaaaaabkvkaaaaaaaaai
+ let editor_id = FatcatId::from_str("aaaaaaaaaaaabkvkaaaaaaaaai").unwrap();
+ let description = "Backfill of missing changelog entries due to database id gap";
+
+ // fetch the last entry before the gap, to re-use the timestamp
+ let existing_row: ChangelogRow = changelog::table.find(last_good).first(conn)?;
+
+ for index in last_good + 1..max_index + 1 {
+ // ensure this index is actually a gap
+ let count: i64 = changelog::table
+ .filter(changelog::id.eq(index))
+ .count()
+ .get_result(conn)?;
+ if count != 0 {
+ println!("Found existing changelog: {}", index);
+ return Ok(());
+ }
+
+ // create dummy empty editgroup, then add a changelog entry
+ let eg_row: EditgroupRow = diesel::insert_into(editgroup::table)
+ .values((
+ editgroup::editor_id.eq(editor_id.to_uuid()),
+ editgroup::created.eq(existing_row.timestamp),
+ editgroup::is_accepted.eq(true),
+ editgroup::description.eq(Some(description)),
+ ))
+ .get_result(conn)?;
+ let _entry_row: ChangelogRow = diesel::insert_into(changelog::table)
+ .values((
+ changelog::id.eq(index),
+ changelog::editgroup_id.eq(eg_row.id),
+ changelog::timestamp.eq(existing_row.timestamp),
+ ))
+ .get_result(conn)?;
+ println!("Inserted changelog: {}", index);
+ }
+ Ok(())
+}
+
+fn main() -> Result<()> {
+ let m = App::new("fatcat-doctor")
+ .version(env!("CARGO_PKG_VERSION"))
+ .author("Bryan Newbold <bnewbold@archive.org>")
+ .about("Database cleanup / fixup tool")
+ .subcommand(
+ SubCommand::with_name("backfill-changelog-gap")
+ .about("Inserts dummy changelog entries and editgroups for gap")
+ .args_from_usage(
+ "<start> 'changelog index of entry just before gap'
+ <end> 'highest changelog index to backfill'",
+ ),
+ )
+ .get_matches();
+
+ let db_conn = server::database_worker_pool()?
+ .get()
+ .expect("database pool");
+ match m.subcommand() {
+ ("backfill-changelog-gap", Some(subm)) => {
+ let last_good: i64 = value_t_or_exit!(subm.value_of("start"), i64);
+ let max_index: i64 = value_t_or_exit!(subm.value_of("end"), i64);
+ backfill_changelog_gap(&db_conn, last_good, max_index)?;
+ }
+ _ => {
+ println!("Missing or unimplemented command!");
+ println!("{}", m.usage());
+ process::exit(-1);
+ }
+ }
+ Ok(())
+}
diff --git a/rust/src/bin/fatcat-export.rs b/rust/src/bin/fatcat-export.rs
index 7d671b9a..9ac977aa 100644
--- a/rust/src/bin/fatcat-export.rs
+++ b/rust/src/bin/fatcat-export.rs
@@ -28,7 +28,7 @@ use std::thread;
const CHANNEL_BUFFER_LEN: usize = 200;
arg_enum! {
- #[derive(PartialEq, Debug, Clone, Copy)]
+ #[derive(PartialEq, Debug, Clone, Copy, Eq)]
pub enum ExportEntityType {
Creator,
Container,
@@ -163,7 +163,7 @@ fn parse_line(s: &str) -> Result<IdentRow> {
let group_id: Option<FatcatId> = if fields.len() == 4 {
match fields[3].as_ref() {
"" => None,
- val => Some(FatcatId::from_uuid(&Uuid::from_str(&val)?)),
+ val => Some(FatcatId::from_uuid(&Uuid::from_str(val)?)),
}
} else if fields.len() == 3 {
None
@@ -174,11 +174,11 @@ fn parse_line(s: &str) -> Result<IdentRow> {
ident_id: FatcatId::from_uuid(&Uuid::from_str(&fields[0])?),
rev_id: match fields[1].as_ref() {
"" => None,
- val => Some(Uuid::from_str(&val)?),
+ val => Some(Uuid::from_str(val)?),
},
redirect_id: match fields[2].as_ref() {
"" => None,
- val => Some(FatcatId::from_uuid(&Uuid::from_str(&val)?)),
+ val => Some(FatcatId::from_uuid(&Uuid::from_str(val)?)),
},
group_id,
})
@@ -331,7 +331,7 @@ pub fn do_export_batch(
(Some(_), Some(_), false) => (),
_ => {
if row.group_id == None || row.group_id != last_group_id {
- if batch.len() > 0 {
+ if !batch.is_empty() {
row_sender.send(batch);
batch = vec![];
}
@@ -345,7 +345,7 @@ pub fn do_export_batch(
info!("processed {} lines...", count);
}
}
- if batch.len() > 0 {
+ if !batch.is_empty() {
row_sender.send(batch);
}
drop(row_sender);
@@ -385,7 +385,7 @@ fn main() -> Result<()> {
None => std::cmp::min(1, num_cpus::get() / 2) as usize,
};
let expand = match m.value_of("expand") {
- Some(s) => Some(ExpandFlags::from_str(&s)?),
+ Some(s) => Some(ExpandFlags::from_str(s)?),
None => None,
};
let log_level = if m.is_present("quiet") {
diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs
index 006154df..bfa41805 100644
--- a/rust/src/bin/fatcatd.rs
+++ b/rust/src/bin/fatcatd.rs
@@ -27,7 +27,7 @@ pub struct XClacksOverheadMiddleware;
impl AfterMiddleware for XClacksOverheadMiddleware {
fn after(&self, _req: &mut Request, mut res: Response) -> iron::IronResult<Response> {
res.headers
- .set(XClacksOverhead("GNU aaronsw, jpb".to_owned()));
+ .set(XClacksOverhead("GNU aaronsw, jpb, pde".to_owned()));
Ok(res)
}
}
@@ -48,7 +48,7 @@ fn main() -> Result<()> {
let drain = slog_async::Async::new(drain).build().fuse();
let logger = Logger::root(drain, o!());
let _scope_guard = slog_scope::set_global_logger(logger.clone());
- let _log_guard = slog_stdlog::init().unwrap();
+ slog_stdlog::init().unwrap();
let formatter = DefaultLogFormatter;
// sentry exception handling