-
Notifications
You must be signed in to change notification settings - Fork 649
Add audit trail to the publish, yank and unyank transactions. #1700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d35e31
Adding audit trail actions into the publish, yank and unyank transact…
markcatley 3bf4557
Merge remote-tracking branch 'upstream/master' into version-owner-act…
carols10cents a6430a0
Make parameter names consistent with conventions
carols10cents 3b1061f
Clarify test description comments
carols10cents e38744c
Remove some unnecessary assertions
carols10cents 4d7f8a8
cargo fmt
carols10cents File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
migrations/2019-11-03-003946_tidy_up_version_owner_actions/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
ALTER TABLE version_owner_actions | ||
ALTER COLUMN user_id | ||
DROP NOT NULL | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
ALTER COLUMN version_id | ||
DROP NOT NULL | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
RENAME COLUMN user_id TO owner_id | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
RENAME COLUMN api_token_id TO owner_token_id | ||
; |
20 changes: 20 additions & 0 deletions
20
migrations/2019-11-03-003946_tidy_up_version_owner_actions/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- I expect that it will be save to apply this migration in production as the code that adds records | ||
-- to this table is not used until this changeset. | ||
|
||
ALTER TABLE version_owner_actions | ||
RENAME COLUMN owner_id TO user_id | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
RENAME COLUMN owner_token_id TO api_token_id | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
ALTER COLUMN user_id | ||
SET NOT NULL | ||
; | ||
|
||
ALTER TABLE version_owner_actions | ||
ALTER COLUMN version_id | ||
SET NOT NULL | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,90 @@ | ||
use chrono::NaiveDateTime; | ||
use diesel::prelude::*; | ||
use diesel::{ | ||
deserialize::{self, FromSql}, | ||
pg::Pg, | ||
serialize::{self, Output, ToSql}, | ||
sql_types::Integer, | ||
}; | ||
use std::io::Write; | ||
|
||
use crate::models::{ApiToken, User, Version}; | ||
use crate::schema::*; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
#[repr(u32)] | ||
#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)] | ||
#[repr(i32)] | ||
#[sql_type = "Integer"] | ||
pub enum VersionAction { | ||
Publish = 0, | ||
Yank = 1, | ||
Unyank = 2, | ||
} | ||
|
||
impl FromSql<Integer, Pg> for VersionAction { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? { | ||
0 => Ok(VersionAction::Publish), | ||
1 => Ok(VersionAction::Yank), | ||
2 => Ok(VersionAction::Unyank), | ||
n => Err(format!("unknown version action: {}", n).into()), | ||
} | ||
} | ||
} | ||
|
||
impl ToSql<Integer, Pg> for VersionAction { | ||
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result { | ||
ToSql::<Integer, Pg>::to_sql(&(*self as i32), out) | ||
} | ||
} | ||
|
||
#[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")] | ||
#[belongs_to(User, foreign_key = "user_id")] | ||
#[belongs_to(ApiToken, foreign_key = "api_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 user_id: i32, | ||
pub api_token_id: Option<i32>, | ||
pub action: VersionAction, | ||
pub time: NaiveDateTime, | ||
} | ||
|
||
impl VersionOwnerAction { | ||
pub fn all(conn: &PgConnection) -> QueryResult<Vec<VersionOwnerAction>> { | ||
version_owner_actions::table.load(conn) | ||
} | ||
|
||
pub fn by_version_id_and_action( | ||
conn: &PgConnection, | ||
version_id_: i32, | ||
action_: VersionAction, | ||
) -> QueryResult<Vec<VersionOwnerAction>> { | ||
use version_owner_actions::dsl::{action, version_id}; | ||
|
||
version_owner_actions::table | ||
.filter(version_id.eq(version_id_)) | ||
.filter(action.eq(action_)) | ||
.load(conn) | ||
} | ||
} | ||
|
||
pub fn insert_version_owner_action( | ||
conn: &PgConnection, | ||
version_id_: i32, | ||
user_id_: i32, | ||
api_token_id_: Option<i32>, | ||
action_: VersionAction, | ||
) -> QueryResult<VersionOwnerAction> { | ||
use version_owner_actions::dsl::{action, api_token_id, user_id, version_id}; | ||
|
||
diesel::insert_into(version_owner_actions::table) | ||
.values(( | ||
version_id.eq(version_id_), | ||
user_id.eq(user_id_), | ||
api_token_id.eq(api_token_id_), | ||
action.eq(action_), | ||
)) | ||
.get_result(conn) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.