Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions db/migrate/20250117172959_create_user_action_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateUserActionEvents < ActiveRecord::Migration[7.2]
def change
create_table :user_action_events do |t|
t.string :details, null: false
t.timestamps null: false
end
end
end
24 changes: 24 additions & 0 deletions db/migrate/20250117173040_create_user_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class CreateUserActions < ActiveRecord::Migration[7.2]
def change
# Create the enum type first
create_enum :user_action_status, ['initial', 'success', 'error']

create_table :user_actions, id: :uuid do |t|
# Core fields
t.uuid :acting_user_account_id, null: false
t.uuid :subject_user_account_id, null: false
Copy link
Contributor

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_keys 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

Copy link
Contributor Author

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.

t.references :user_action_event, null: false, foreign_key: { validate: false }
Copy link
Contributor

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

Copy link
Contributor Author

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.

t.enum :status, enum_type: :user_action_status, default: 'initial', null: false, index: true

# Additional columns from ticket
t.boolean :user_verified, default: false, null: false
t.string :ip_address
t.jsonb :device_info
Copy link
Contributor

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

Copy link
Contributor Author

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!


t.timestamps null: false
end

add_foreign_key :user_actions, :user_accounts, column: :acting_user_account_id, validate: false
add_foreign_key :user_actions, :user_accounts, column: :subject_user_account_id, validate: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ValidateUserActionsForeignKeys < ActiveRecord::Migration[7.2]
def change
validate_foreign_key :user_actions, :user_accounts, column: :acting_user_account_id
validate_foreign_key :user_actions, :user_accounts, column: :subject_user_account_id
validate_foreign_key :user_actions, :user_action_events
end
end
Copy link
Contributor

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

Copy link
Contributor Author

@anjolovic anjolovic Jan 17, 2025

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could see if your text editor has a setting. In vscode it's:
Screenshot 2025-01-17 at 1 41 49 PM
This will ensure all the files you commit, not just ruby, have a new line

26 changes: 25 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading