Skip to content

Commit 64f9676

Browse files
committed
Making some audit action database columns not null.
1 parent 5accf6a commit 64f9676

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

migrations/2019-01-26-090348_create_version_owner_actions/up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE version_owner_actions (
22
id SERIAL PRIMARY KEY,
3-
version_id INTEGER REFERENCES versions(id) ON DELETE CASCADE,
4-
owner_id INTEGER REFERENCES users(id),
3+
version_id INTEGER REFERENCES versions(id) ON DELETE CASCADE NOT NULL,
4+
owner_id INTEGER REFERENCES users(id) NOT NULL,
55
owner_token_id INTEGER REFERENCES api_tokens(id),
66
action INTEGER NOT NULL,
77
time TIMESTAMP NOT NULL DEFAULT now()

src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use self::action::{VersionAction, VersionOwnerAction};
1+
pub use self::action::{NewVersionOwnerAction, VersionAction, VersionOwnerAction};
22
pub use self::badge::{Badge, CrateBadge, MaintenanceStatus};
33
pub use self::category::{Category, CrateCategory, NewCategory};
44
pub use self::crate_owner_invitation::{CrateOwnerInvitation, NewCrateOwnerInvitation};

src/models/action.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
use chrono::NaiveDateTime;
2+
use diesel::prelude::*;
3+
use diesel::{
4+
deserialize::{self, FromSql},
5+
pg::Pg,
6+
serialize::{self, Output, ToSql},
7+
sql_types::Integer,
8+
};
9+
use std::io::Write;
210

311
use crate::models::{ApiToken, User, Version};
412
use crate::schema::*;
513

6-
#[derive(Debug, Clone, Copy)]
14+
#[derive(Debug, Clone, Copy, FromSqlRow, AsExpression)]
715
#[repr(u32)]
816
pub enum VersionAction {
917
Publish = 0,
1018
Yank = 1,
1119
Unyank = 2,
1220
}
1321

22+
impl Into<i32> for VersionAction {
23+
fn into(self) -> i32 {
24+
match self {
25+
VersionAction::Publish => 0,
26+
VersionAction::Yank => 1,
27+
VersionAction::Unyank => 2,
28+
}
29+
}
30+
}
31+
32+
impl FromSql<Integer, Pg> for VersionAction {
33+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
34+
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
35+
0 => Ok(VersionAction::Publish),
36+
1 => Ok(VersionAction::Yank),
37+
2 => Ok(VersionAction::Unyank),
38+
n => Err(format!("unknown version action: {}", n).into()),
39+
}
40+
}
41+
}
42+
43+
impl ToSql<Integer, Pg> for VersionAction {
44+
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
45+
let value: i32 = (*self).into();
46+
ToSql::<Integer, Pg>::to_sql(&value, out)
47+
}
48+
}
49+
1450
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)]
1551
#[belongs_to(Version)]
1652
#[belongs_to(User, foreign_key = "owner_id")]
@@ -20,7 +56,38 @@ pub struct VersionOwnerAction {
2056
pub id: i32,
2157
pub version_id: i32,
2258
pub owner_id: i32,
23-
pub owner_token_id: i32,
59+
pub owner_token_id: Option<i32>,
2460
pub action: VersionAction,
2561
pub time: NaiveDateTime,
2662
}
63+
64+
#[derive(Copy, Clone, Debug, Insertable)]
65+
#[table_name = "version_owner_actions"]
66+
pub struct NewVersionOwnerAction {
67+
pub version_id: i32,
68+
pub owner_id: i32,
69+
pub owner_token_id: Option<i32>,
70+
pub action: i32,
71+
}
72+
73+
impl NewVersionOwnerAction {
74+
pub fn new(
75+
version_id: i32,
76+
owner_id: i32,
77+
owner_token_id: Option<i32>,
78+
action: VersionAction,
79+
) -> Self {
80+
Self {
81+
version_id,
82+
owner_id,
83+
owner_token_id,
84+
action: action.into(),
85+
}
86+
}
87+
88+
pub fn save(&self, conn: &PgConnection) -> QueryResult<VersionOwnerAction> {
89+
diesel::insert_into(version_owner_actions::table)
90+
.values(self)
91+
.get_result(conn)
92+
}
93+
}

src/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,16 +871,16 @@ table! {
871871
id -> Int4,
872872
/// The `version_id` column of the `version_owner_actions` table.
873873
///
874-
/// Its SQL type is `Nullable<Int4>`.
874+
/// Its SQL type is `Int4`.
875875
///
876876
/// (Automatically generated by Diesel.)
877-
version_id -> Nullable<Int4>,
877+
version_id -> Int4,
878878
/// The `owner_id` column of the `version_owner_actions` table.
879879
///
880-
/// Its SQL type is `Nullable<Int4>`.
880+
/// Its SQL type is `Int4`.
881881
///
882882
/// (Automatically generated by Diesel.)
883-
owner_id -> Nullable<Int4>,
883+
owner_id -> Int4,
884884
/// The `owner_token_id` column of the `version_owner_actions` table.
885885
///
886886
/// Its SQL type is `Nullable<Int4>`.

0 commit comments

Comments
 (0)