Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adds new personal detail fields to Users table#473

Merged
akhilgkrishnan merged 3 commits intodevelopfrom
add-fields-to-users-table
Jun 14, 2022
Merged

Adds new personal detail fields to Users table#473
akhilgkrishnan merged 3 commits intodevelopfrom
add-fields-to-users-table

Conversation

@mohinid
Copy link
Contributor

@mohinid mohinid commented Jun 14, 2022

Notion card

https://www.notion.so/saeloun/Add-tables-for-personal-employment-Allocated-devices-compensation-71ef49d0c38e4242bd2998120141372d

Summary

  1. Added new columns(personal_email_id, date_of_birth & social_accounts) to Users table.
  2. DATA MIGRATION- Updated default empty string values to Social Account URLs(github_url, linkedin_url) for existing Data.
  3. Added "UserDefaultSocialAccountVerifier" - verification file for data migration.
  4. Added rspec for new changes in User model.

Preview

Screenshot 2022-06-14 at 4 19 45 PM

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Tested on Local Rails console.

Checklist:

  • I have manually tested all workflows
  • I have performed a self-review of my own code
  • I have added automated tests for my code

# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
# social_accounts :jsonb
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to have a separate model for social_accounts with a has_many :social_accounts association on User model. Fits well with the relational model.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rohitjoshixyz can you please comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

We decided to go ahead with a JSON field type @apoorv-mishra so that it will be easier for us to add more social links without migrating DB columns. Also, we only need to store links as names and URLs as strings which we could nicely store in a JSON. It will also avoid a join query.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool 👍

store_accessor :social_accounts, :github_url, :linkedin_url

# Validations
before_validation :set_default_social_accounts, on: :create
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you comment on why did you choose the before_validation callback? I think we can have it after_create

Also, we are not checking if the URLs already exist in the object. Wouldn't this overwrite the URLs we actually want to save?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, @akhilgkrishnan suggested creating empty strings by default when the User object is created. @akhilgkrishnan please comment.

Copy link
Member

Choose a reason for hiding this comment

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

I think, In this case, I don't think we need an after_create. The problem with after_create it actually write the DB multiple times.
One for creating the user entry, another write for updating the social_account. before_validation or before_create is suitable for here, because they are just initializing the value and not creating an entry for the DB.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's go ahead with before_create 👍

@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative("./verifiers/user_default_social_account_verifier.rb")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this to be in a separate directory? We could just write the method in the migration itself. Could you post any blog or tutorial you followed for this over here for my reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, its created so that we can add more verifiers for future data migrations.

Comment on lines 121 to 127
def set_default_social_accounts
self.social_accounts = {
"github_url": "",
"linkedin_url": ""
}
end

Copy link
Contributor

Choose a reason for hiding this comment

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

@mohinid Creating empty strings is fine. before_validation :set_default_social_accounts, on: :create can be replaced with before_create as it would be equivalent and I think it should be after_create cc @akhilgkrishnan

Copy link
Member

Choose a reason for hiding this comment

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

Yes, before_create also works

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@akhilgkrishnan @rohitjoshixyz what is the final conclusion? before_create or before_validation?

Copy link
Member

Choose a reason for hiding this comment

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

Go with before_create

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@akhilgkrishnan @rohitjoshixyz I am using "after_initialize" with a check of new_record. It creates social_accounts with empty URLs because 'before_create' is generating an empty social_accounts object.

Tested this by updating the github_url of the first record, as well as running RSpecs. Here is the SS.

Screenshot 2022-06-14 at 8 55 05 PM

# updated_at :datetime not null
# current_workspace_id :bigint
# invited_by_id :bigint
# personal_email_id :string
Copy link
Member

Choose a reason for hiding this comment

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

@mohinid Still got confused with this field. What's the exact difference of the actual email and this one.

Copy link
Contributor Author

@mohinid mohinid Jun 14, 2022

Choose a reason for hiding this comment

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

This is the personal Email Id as of the user just for reference. Like an alternative email. Screenshot attached in the notion task.

@github-actions
Copy link

Current Code Coverage Percent of this PR:

89.15 %

Files having coverage below 100%

Impacted Files Coverage
/app/controllers/users/invitations_controller.rb 86.36 %
/app/services/invoice_payment/checkout.rb 44.0 %
/app/services/invoice_payment/pdf_generation.rb 70.97 %
/app/controllers/internal_api/v1/companies_controller.rb 95.45 %
/app/controllers/internal_api/v1/profile_controller.rb 96.88 %
/app/controllers/internal_api/v1/payment_settings_controller.rb 93.33 %
/app/controllers/internal_api/v1/wise/recipients_controller.rb 90.0 %
/app/controllers/internal_api/v1/payments/providers_controller.rb 94.74 %
/lib/benchmarking/benchmarker.rb 0.0 %

Copy link
Contributor

@rohitjoshixyz rohitjoshixyz left a comment

Choose a reason for hiding this comment

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

LGTM, I like the before_initialize callback 👍

@akhilgkrishnan akhilgkrishnan merged commit 28b0097 into develop Jun 14, 2022
@akhilgkrishnan akhilgkrishnan deleted the add-fields-to-users-table branch June 14, 2022 16:15
vipulnsward pushed a commit that referenced this pull request Feb 15, 2026
* Adds new personal detail fields to Users table

* update callback to after_initialize

Co-authored-by: Akhil G Krishnan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants