From 7e9eace52dc7adb88f32fa04d6ad2c5e930e536d Mon Sep 17 00:00:00 2001 From: Josh Leeb-du Toit Date: Thu, 24 Jan 2019 22:12:30 +1100 Subject: [PATCH 1/3] Add migration and model for version_owner_actions table Add a migration to create the `version_owner_actions` table, as well as a `VersionOwnerAction` struct in `models/actions.rs`. --- .../down.sql | 1 + .../up.sql | 8 +++ src/models/action.rs | 26 ++++++++++ src/models/mod.rs | 2 + src/schema.patch | 2 +- src/schema.rs | 52 +++++++++++++++++++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 migrations/2019-01-26-090348_create_version_owner_actions/down.sql create mode 100644 migrations/2019-01-26-090348_create_version_owner_actions/up.sql create mode 100644 src/models/action.rs diff --git a/migrations/2019-01-26-090348_create_version_owner_actions/down.sql b/migrations/2019-01-26-090348_create_version_owner_actions/down.sql new file mode 100644 index 00000000000..f3f9004202f --- /dev/null +++ b/migrations/2019-01-26-090348_create_version_owner_actions/down.sql @@ -0,0 +1 @@ +DROP TABLE version_owner_actions; diff --git a/migrations/2019-01-26-090348_create_version_owner_actions/up.sql b/migrations/2019-01-26-090348_create_version_owner_actions/up.sql new file mode 100644 index 00000000000..7149e80f239 --- /dev/null +++ b/migrations/2019-01-26-090348_create_version_owner_actions/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE version_owner_actions ( + id SERIAL PRIMARY KEY, + version_id INTEGER REFERENCES versions(id) ON DELETE CASCADE, + owner_id INTEGER REFERENCES users(id), + owner_token_id INTEGER REFERENCES api_tokens(id), + action INTEGER NOT NULL, + time TIMESTAMP NOT NULL DEFAULT now() +); diff --git a/src/models/action.rs b/src/models/action.rs new file mode 100644 index 00000000000..474bc252ab8 --- /dev/null +++ b/src/models/action.rs @@ -0,0 +1,26 @@ +use chrono::NaiveDateTime; + +use crate::models::{ApiToken, User, Version}; +use crate::schema::*; + +#[derive(Debug, Clone, Copy)] +#[repr(u32)] +pub enum VersionAction { + Publish = 0, + Yank = 1, + Unyank = 2, +} + +#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)] +#[belongs_to(Version)] +#[belongs_to(User, foreign_key = "owner_id")] +#[belongs_to(ApiToken, foreign_key = "owner_token_id")] +#[table_name = "version_owner_actions"] +pub struct VersionOwnerAction { + pub id: i32, + pub version_id: i32, + pub owner_id: i32, + pub owner_token_id: i32, + pub action: VersionAction, + pub time: NaiveDateTime, +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 1a5aca4f33f..4e339934efc 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,4 @@ +pub use self::action::{VersionAction, VersionOwnerAction}; pub use self::badge::{Badge, CrateBadge, MaintenanceStatus}; pub use self::category::{Category, CrateCategory, NewCategory}; pub use self::crate_owner_invitation::{CrateOwnerInvitation, NewCrateOwnerInvitation}; @@ -16,6 +17,7 @@ pub use self::version::{NewVersion, Version}; pub mod helpers; +mod action; mod badge; pub mod category; mod crate_owner_invitation; diff --git a/src/schema.patch b/src/schema.patch index 3ccdaeeb15e..7331e235443 100644 --- a/src/schema.patch +++ b/src/schema.patch @@ -66,7 +66,7 @@ index df884e4..18e08cd 100644 joinable!(version_authors -> users (user_id)); joinable!(version_authors -> versions (version_id)); joinable!(version_downloads -> versions (version_id)); - joinable!(versions -> crates (crate_id)); + joinable!(version_owner_actions -> api_tokens (owner_token_id)); @@ -913,12 +935,13 @@ allow_tables_to_appear_in_same_query!( dependencies, diff --git a/src/schema.rs b/src/schema.rs index e7a7ced1628..c6ba547765b 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -834,6 +834,54 @@ table! { } } +table! { + use diesel::sql_types::*; + use diesel_full_text_search::{TsVector as Tsvector}; + use diesel_ltree::Ltree; + + /// Representation of the `version_owner_actions` table. + /// + /// (Automatically generated by Diesel.) + version_owner_actions (id) { + /// The `id` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + /// The `version_id` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + version_id -> Nullable, + /// The `owner_id` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + owner_id -> Nullable, + /// The `owner_token_id` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + owner_token_id -> Nullable, + /// The `action` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + action -> Int4, + /// The `time` column of the `version_owner_actions` table. + /// + /// Its SQL type is `Timestamp`. + /// + /// (Automatically generated by Diesel.) + time -> Timestamp, + } +} + table! { use diesel::sql_types::*; use diesel_full_text_search::{TsVector as Tsvector}; @@ -926,6 +974,9 @@ joinable!(recent_crate_downloads -> crates (crate_id)); joinable!(version_authors -> users (user_id)); joinable!(version_authors -> versions (version_id)); joinable!(version_downloads -> versions (version_id)); +joinable!(version_owner_actions -> api_tokens (owner_token_id)); +joinable!(version_owner_actions -> users (owner_id)); +joinable!(version_owner_actions -> versions (version_id)); joinable!(versions -> crates (crate_id)); allow_tables_to_appear_in_same_query!( @@ -950,5 +1001,6 @@ allow_tables_to_appear_in_same_query!( users, version_authors, version_downloads, + version_owner_actions, versions, ); From 52c7fdfa809745ee7f2333ba56bbc9e12f006efa Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 5 Oct 2019 20:45:24 -0400 Subject: [PATCH 2/3] Fix git merge of schema that applied cleanly but was a logical conflict The ltree `use` statements aren't in the schema anymore. --- src/schema.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/schema.rs b/src/schema.rs index e3896e9060e..6b692eeada9 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -910,7 +910,6 @@ table! { table! { use diesel::sql_types::*; use diesel_full_text_search::{TsVector as Tsvector}; - use diesel_ltree::Ltree; /// Representation of the `versions` table. /// From e5c62422a6c036ac3245504c6286845af4e06bf8 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 5 Oct 2019 20:46:50 -0400 Subject: [PATCH 3/3] The version_owner_actions table won't be included in database dumps --- src/tasks/dump_db/dump-db.toml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tasks/dump_db/dump-db.toml b/src/tasks/dump_db/dump-db.toml index 38318037c95..12302c891ba 100644 --- a/src/tasks/dump_db/dump-db.toml +++ b/src/tasks/dump_db/dump-db.toml @@ -191,6 +191,14 @@ counted = "private" date = "public" processed = "private" +[version_owner_actions.columns] +id = "private" +version_id = "private" +owner_id = "private" +owner_token_id = "private" +action = "private" +time = "private" + [versions] dependencies = ["crates", "users"] [versions.columns]