Welcome to mirror list, hosted at ThFree Co, Russian Federation.

safely_change_column_default.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 567f690d9505ce21419480153da3a07c70d8c9cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

# == SafelyChangeColumnDefault concern.
#
# Contains functionality that allows safely changing a column default without downtime.
# Without this concern, Rails can mutate the old default value to the new default value if the old default is explicitly
# specified.
#
# Usage:
#
#   class SomeModel < ApplicationRecord
#     include SafelyChangeColumnDefault
#
#     columns_changing_default :value
#   end
#
#   # Assume a default of 100 for value
#   SomeModel.create!(value: 100) # INSERT INTO some_model (value) VALUES (100)
#   change_column_default('some_model', 'value', from: 100, to: 101)
#   SomeModel.create!(value: 100) # INSERT INTO some_model (value) VALUES (100)
#   # Without this concern, would be INSERT INTO some_model (value) DEFAULT VALUES and would insert 101.
module SafelyChangeColumnDefault
  extend ActiveSupport::Concern

  class_methods do
    # Indicate that one or more columns will have their database default change.
    #
    # By indicating those columns here, this helper prevents a case where explicitly writing the old database default
    # will be mutated to the new database default.
    def columns_changing_default(*columns)
      self.columns_with_changing_default = columns.map(&:to_s)
    end
  end

  included do
    class_attribute :columns_with_changing_default, default: []

    before_create do
      columns_with_changing_default.to_a.each do |attr_name|
        attr = @attributes[attr_name]

        attribute_will_change!(attr_name) if !attr.changed? && attr.came_from_user?
      end
    end
  end
end