Skip to content

Commit 587dcf6

Browse files
committed
Making some audit action database columns not null.
1 parent b62a0ac commit 587dcf6

File tree

4 files changed

+84
-9
lines changed

4 files changed

+84
-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: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11
use chrono::NaiveDateTime;
2+
use diesel::prelude::*;
3+
use diesel::{
4+
deserialize::{self, FromSql},
5+
expression::{bound::Bound, AsExpression},
6+
pg::Pg,
7+
sql_types::Integer,
8+
};
29

310
use crate::models::{ApiToken, User, Version};
411
use crate::schema::*;
512

6-
#[derive(Debug, Clone, Copy)]
13+
#[derive(Debug, Clone, Copy, FromSqlRow)]
714
#[repr(u32)]
815
pub enum VersionAction {
916
Publish = 0,
1017
Yank = 1,
1118
Unyank = 2,
1219
}
1320

21+
impl Into<i32> for VersionAction {
22+
fn into(self) -> i32 {
23+
match self {
24+
VersionAction::Publish => 0,
25+
VersionAction::Yank => 1,
26+
VersionAction::Unyank => 2,
27+
}
28+
}
29+
}
30+
31+
impl FromSql<Integer, Pg> for VersionAction {
32+
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
33+
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
34+
0 => Ok(VersionAction::Publish),
35+
1 => Ok(VersionAction::Yank),
36+
2 => Ok(VersionAction::Unyank),
37+
n => Err(format!("unknown version action: {}", n).into()),
38+
}
39+
}
40+
}
41+
42+
impl AsExpression<Integer> for VersionAction {
43+
type Expression = Bound<Integer, i32>;
44+
45+
fn as_expression(self) -> Self::Expression {
46+
Bound::new(self.into())
47+
}
48+
}
49+
50+
impl<'a> AsExpression<Integer> for &'a VersionAction {
51+
type Expression = Bound<Integer, i32>;
52+
53+
fn as_expression(self) -> Self::Expression {
54+
Bound::new((*self).into())
55+
}
56+
}
57+
1458
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)]
1559
#[belongs_to(Version)]
1660
#[belongs_to(User, foreign_key = "owner_id")]
@@ -20,7 +64,38 @@ pub struct VersionOwnerAction {
2064
pub id: i32,
2165
pub version_id: i32,
2266
pub owner_id: i32,
23-
pub owner_token_id: i32,
67+
pub owner_token_id: Option<i32>,
2468
pub action: VersionAction,
2569
pub time: NaiveDateTime,
2670
}
71+
72+
#[derive(Copy, Clone, Debug, Insertable)]
73+
#[table_name = "version_owner_actions"]
74+
pub struct NewVersionOwnerAction {
75+
pub version_id: i32,
76+
pub owner_id: i32,
77+
pub owner_token_id: Option<i32>,
78+
pub action: VersionAction,
79+
}
80+
81+
impl NewVersionOwnerAction {
82+
pub fn new(
83+
version_id: i32,
84+
owner_id: i32,
85+
owner_token_id: Option<i32>,
86+
action: VersionAction,
87+
) -> Self {
88+
Self {
89+
version_id,
90+
owner_id,
91+
owner_token_id,
92+
action,
93+
}
94+
}
95+
96+
pub fn save(&self, conn: &PgConnection) -> QueryResult<VersionOwnerAction> {
97+
diesel::insert_into(version_owner_actions::table)
98+
.values(self)
99+
.get_result(conn)
100+
}
101+
}

src/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,16 +836,16 @@ table! {
836836
id -> Int4,
837837
/// The `version_id` column of the `version_owner_actions` table.
838838
///
839-
/// Its SQL type is `Nullable<Int4>`.
839+
/// Its SQL type is `Int4`.
840840
///
841841
/// (Automatically generated by Diesel.)
842-
version_id -> Nullable<Int4>,
842+
version_id -> Int4,
843843
/// The `owner_id` column of the `version_owner_actions` table.
844844
///
845-
/// Its SQL type is `Nullable<Int4>`.
845+
/// Its SQL type is `Int4`.
846846
///
847847
/// (Automatically generated by Diesel.)
848-
owner_id -> Nullable<Int4>,
848+
owner_id -> Int4,
849849
/// The `owner_token_id` column of the `version_owner_actions` table.
850850
///
851851
/// Its SQL type is `Nullable<Int4>`.

0 commit comments

Comments
 (0)