20181026143227_migrate_snippets_access_level_default_value.rb 1.34 KB
Newer Older
1 2 3 4 5
# frozen_string_literal: true

# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

6
class MigrateSnippetsAccessLevelDefaultValue < ActiveRecord::Migration[4.2]
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
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  ENABLED = 20

  disable_ddl_transaction!

  class ProjectFeature < ActiveRecord::Base
    include EachBatch

    self.table_name = 'project_features'
  end

  def up
    change_column_default :project_features, :snippets_access_level, ENABLED

    # On GitLab.com this will update about 28 000 rows. Since our updates are
    # very small and this column is not indexed, these updates should be very
    # lightweight.
    ProjectFeature.where(snippets_access_level: nil).each_batch do |batch|
      batch.update_all(snippets_access_level: ENABLED)
    end

    # We do not need to perform this in a post-deployment migration as the
    # ProjectFeature model already enforces a default value for all new rows.
    change_column_null :project_features, :snippets_access_level, false
  end

  def down
    change_column_null :project_features, :snippets_access_level, true
    change_column_default :project_features, :snippets_access_level, nil

    # We can't migrate from 20 -> NULL, as some projects may have explicitly set
    # the access level to 20.
  end
end