Add not-null check to non-extensions settings at the DB level

Issue: Enforce not-null constraint on settings_context... (#503812 - closed)

What does this MR do and why?

Add not-null constraint check for non-extensions settings

Add not-null check for non-extensions settings at the DB level.

Changelog: other

References

Enforce not-null constraint on settings_context... (#503812 - closed)

Screenshots or screen recordings

DB migration; no user facing changes. I have tested the cases outlined in the next section and confirmed that the expected results are observed.

How to set up and validate locally

  1. Pull this branch and run migrations: bundle exec rails db:migrate

  2. Apply the following patch to disable application level validation:

    diff --git a/app/models/vs_code/settings/vs_code_setting.rb b/app/models/vs_code/settings/vs_code_setting.rb
    index 0171b3728f9363..abf68407bdb5cd 100644
    --- a/app/models/vs_code/settings/vs_code_setting.rb
    +++ b/app/models/vs_code/settings/vs_code_setting.rb
    @@ -40,16 +40,16 @@ class VsCodeSetting < ApplicationRecord
           private
     
           def settings_context_hash_check
    -        case setting_type
    -        when EXTENSIONS
    -          return unless settings_context_hash.nil?
    +        # case setting_type
    +        # when EXTENSIONS
    +        #   return unless settings_context_hash.nil?
     
    -          errors.add(:settings_context_hash, 'cannot be blank for extensions setting type')
    -        else
    -          return if settings_context_hash.nil?
    +        #   errors.add(:settings_context_hash, 'cannot be blank for extensions setting type')
    +        # else
    +        #   return if settings_context_hash.nil?
     
    -          errors.add(:settings_context_hash, 'must be blank for non extensions setting type')
    -        end
    +        #   errors.add(:settings_context_hash, 'must be blank for non extensions setting type')
    +        # end
           end
         end
       end
  3. Open the Rails console: bin/rails c

  4. Reset all VSCode settings:

    VsCode::Settings::VsCodeSetting.destroy_all
  5. Follow steps below to test all cases:

    Case Command Expectation

    Create extension setting with null settings context hash

    VsCode::Settings::VsCodeSetting.create!(setting_type: "extensions", settings_context_hash: nil, content: "{}", uuid: SecureRandom.uuid, user_id: 1)

    fail due to database constraint

    Create non-extension setting with not-null settings context hash

    VsCode::Settings::VsCodeSetting.create!(setting_type: "keybindings", settings_context_hash: "1234", content: "{}", uuid: SecureRandom.uuid, user_id: 1)

    fail due to database constraint

    Create extension setting with not-null settings context hash

    VsCode::Settings::VsCodeSetting.create!(setting_type: "extensions", settings_context_hash: "1234", content: "{}", uuid: SecureRandom.uuid, user_id: 1)

    succeeds

    Create non-extension setting with null settings context hash

    VsCode::Settings::VsCodeSetting.create!(setting_type: "keybindings", settings_context_hash: nil, content: "{}", uuid: SecureRandom.uuid, user_id: 1)

    succeeds

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Cindy Halim

Merge request reports

Loading