diff options
author | Kornel <kornel@geekhood.net> | 2020-03-01 19:32:22 +0000 |
---|---|---|
committer | Kornel <kornel@geekhood.net> | 2020-03-01 22:14:40 +0000 |
commit | 9ac4a8feb2e9aa8bb41f9125c6a6624a147c35f3 (patch) | |
tree | 78fd9b4126d4fb53e20eb9ab31e27a99ec0b2a27 | |
parent | 01ad124c4872ed24751d5c36a8444bddbc4a4a64 (diff) |
Store author<>crates
-rw-r--r-- | crate_db/src/lib_crate_db.rs | 25 | ||||
-rw-r--r-- | crate_db/src/schema.rs | 7 |
2 files changed, 31 insertions, 1 deletions
diff --git a/crate_db/src/lib_crate_db.rs b/crate_db/src/lib_crate_db.rs index 85f8411..f84579d 100644 --- a/crate_db/src/lib_crate_db.rs +++ b/crate_db/src/lib_crate_db.rs @@ -3,6 +3,7 @@ use chrono::prelude::*; use failure::*; use heck::KebabCase; use rich_crate::CrateVersionSourceData; +use rich_crate::CrateOwner; use rich_crate::Derived; use rich_crate::Manifest; use rich_crate::ManifestExt; @@ -103,7 +104,7 @@ impl CrateDb { let mut conn = self.exclusive_conn.lock().await; tokio::task::block_in_place(|| { - let conn = conn.get_or_insert_with(|| self.connect().unwrap()); + let conn = conn.get_or_insert_with(|| self.connect().expect("db setup")); let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?; let res = cb(&tx).context(context)?; @@ -624,6 +625,28 @@ impl CrateDb { }).await } + pub async fn index_crate_owners(&self, origin: &Origin, owners: &[CrateOwner]) -> FResult<bool> { + self.with_write("index_crate_owners", |tx| { + let mut get_crate_id = tx.prepare_cached("SELECT id FROM crates WHERE origin = ?1")?; + let mut insert = tx.prepare_cached("INSERT OR IGNORE INTO author_crates(github_id, crate_id, invited_by_github_id, invited_at) VALUES(?1, ?2, ?3, ?4)")?; + let crate_id: u32 = match get_crate_id.query_row(&[&origin.to_str()], |row| row.get(0)) { + Ok(id) => id, + Err(_) => return Ok(false), + }; + for o in owners { + if let Some(github_id) = o.github_id { + let invited_by_github_id = match o.invited_by_github_id { + Some(id) if id != github_id => Some(id), + _ => None, + }; + let args: &[&dyn ToSql] = &[&github_id, &crate_id, &invited_by_github_id, &o.invited_at]; + insert.execute(args)?; + } + } + Ok(true) + }).await + } + /// Fetch or guess categories for a crate /// /// Returns category slugs diff --git a/crate_db/src/schema.rs b/crate_db/src/schema.rs index 75202d9..0319f8d 100644 --- a/crate_db/src/schema.rs +++ b/crate_db/src/schema.rs @@ -13,6 +13,13 @@ impl CrateDb { next_update INTEGER, recent_downloads INTEGER NOT NULL ); + CREATE TABLE IF NOT EXISTS author_crates ( + github_id INTEGER NOT NULL, + crate_id INTEGER NOT NULL, + invited_by_github_id INTEGER, + invited_at TEXT + ); + CREATE UNIQUE INDEX IF NOT EXISTS author_crates_idx ON author_crates(github_id, crate_id); CREATE TABLE IF NOT EXISTS keywords ( id INTEGER PRIMARY KEY, keyword TEXT NOT NULL UNIQUE, |