-
Notifications
You must be signed in to change notification settings - Fork 66
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
Create user_actions and user_action_events tables #20349
Create user_actions and user_action_events tables #20349
Conversation
Creates schema for tracking user-initiated actions with: - User action events table for event details - User actions table with: - PostgreSQL native enum for status (initial/success/error) - Foreign keys to user_accounts and user_action_events - Device info and IP address tracking - User verification status (boolean, not null) - UUID primary key Follows strong_migrations best practices: - Foreign keys added initially without validation - Separate migration for foreign key validation - Prevents write blocking during deployment All required fields have null: false constraints Indexes added directly to enum fields
validate_foreign_key :user_actions, :user_accounts, column: :subject_user_account_id | ||
validate_foreign_key :user_actions, :user_action_events | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is missing an end of file new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's fixed now. Also, I am going to add this to RuboCop
to always check and enforce newlines before commits.
Layout/TrailingEmptyLines:
Enabled: true
EnforcedStyle: final_newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.uuid :acting_user_account_id, null: false | ||
t.uuid :subject_user_account_id, null: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use references
here and then remove the add_foreign_key
s at the end. This will make strong migrations happy and then we won't need the extra migrations to validate an empty table. This will also add an index to these columns, which we'll need.
t.references :acting_user_account, null: false, foreign_key: { to_table: :user_accounts }, type: :uuid
t.references :subject_user_account, null: false, foreign_key: { to_table: :user_accounts }, type: :uuid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've updated to use references
.
# Core fields | ||
t.uuid :acting_user_account_id, null: false | ||
t.uuid :subject_user_account_id, null: false | ||
t.references :user_action_event, null: false, foreign_key: { validate: false } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to set foreign_key: { validate: false }
since these are empty
t.references :user_action_event, null: false, foreign_key: true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Removed validate: false
since we're creating empty tables. This aligns better with strong_migrations recommendations since there's no existing data to validate. Also removed the separate validation migration since it's no longer needed.
# Additional columns from ticket | ||
t.boolean :user_verified, default: false, null: false | ||
t.string :ip_address | ||
t.jsonb :device_info |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should be a text
column. I don't think we are going to parse the user agent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed device_info
from jsonb
to text
since we won't be parsing the user agent. Thanks for catching that!
…ate:false, change device_info type, remove separate validation migration)
db/schema.rb
Outdated
create_table "user_actions", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| | ||
t.uuid "acting_user_account_id", null: false | ||
t.uuid "subject_user_account_id", null: false | ||
t.bigint "user_action_event_id", null: false | ||
t.enum "status", default: "initial", null: false, enum_type: "user_action_status" | ||
t.boolean "user_verified", default: false, null: false | ||
t.string "ip_address" | ||
t.jsonb "device_info" | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
t.index ["status"], name: "index_user_actions_on_status" | ||
t.index ["user_action_event_id"], name: "index_user_actions_on_user_action_event_id" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the schema didn't get updated with the latest changes. Could you re-run the migrations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Working on fixing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's fixed now.
I am working on fixing the |
…emove validate:false, change to text columns
Summary
user_actions
anduser_action_events
) to support user action auditingRelated issue(s)
Testing done
bin/rails db:drop db:create db:schema:load db:migrate
What areas of the site does it impact?
Acceptance criteria
Database Changes
user_actions
table with:user_action_events
table with:Implementation Details
Table Structure:
Constraints and Validations:
Indexing:
Requested Feedback
Please review: