Skip to content

Commit d82157a

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

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-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: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
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)]
16+
#[sql_type = "Integer"]
817
pub enum VersionAction {
918
Publish = 0,
1019
Yank = 1,
1120
Unyank = 2,
1221
}
1322

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

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)