Commit ef2504ee authored by Jason Goodman's avatar Jason Goodman Committed by Andreas Brandl

Add version column to operations feature flags table

Default to 1 and do not allow null values
Allow version to equal 1 or 2 with model validations
parent 59344b22
---
title: Add version column to operations_feature_flags table
merge_request: 25552
author:
type: added
# frozen_string_literal: true
class AddVersionToFeatureFlagsTable < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
FEATURE_FLAG_LEGACY_VERSION = 1
def up
# The operations_feature_flags table is small enough that we can disable this cop.
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/25552#note_291202882
# rubocop: disable Migration/AddColumnWithDefault
add_column_with_default(:operations_feature_flags, :version, :smallint, default: FEATURE_FLAG_LEGACY_VERSION, allow_null: false)
# rubocop: enable Migration/AddColumnWithDefault
end
def down
remove_column(:operations_feature_flags, :version)
end
end
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_02_21_144534) do ActiveRecord::Schema.define(version: 2020_02_24_163804) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm" enable_extension "pg_trgm"
...@@ -2916,6 +2916,7 @@ ActiveRecord::Schema.define(version: 2020_02_21_144534) do ...@@ -2916,6 +2916,7 @@ ActiveRecord::Schema.define(version: 2020_02_21_144534) do
t.string "name", null: false t.string "name", null: false
t.text "description" t.text "description"
t.integer "iid", null: false t.integer "iid", null: false
t.integer "version", limit: 2, default: 1, null: false
t.index ["project_id", "iid"], name: "index_operations_feature_flags_on_project_id_and_iid", unique: true t.index ["project_id", "iid"], name: "index_operations_feature_flags_on_project_id_and_iid", unique: true
t.index ["project_id", "name"], name: "index_operations_feature_flags_on_project_id_and_name", unique: true t.index ["project_id", "name"], name: "index_operations_feature_flags_on_project_id_and_name", unique: true
end end
......
...@@ -25,6 +25,7 @@ module Operations ...@@ -25,6 +25,7 @@ module Operations
} }
validates :name, uniqueness: { scope: :project_id } validates :name, uniqueness: { scope: :project_id }
validates :description, allow_blank: true, length: 0..255 validates :description, allow_blank: true, length: 0..255
validates :version, inclusion: { in: [1, 2], message: 'must be 1 or 2' }
validate :first_default_scope, on: :create, if: :has_scopes? validate :first_default_scope, on: :create, if: :has_scopes?
before_create :build_default_scope, unless: :has_scopes? before_create :build_default_scope, unless: :has_scopes?
......
...@@ -16,6 +16,7 @@ describe Operations::FeatureFlag do ...@@ -16,6 +16,7 @@ describe Operations::FeatureFlag do
it { is_expected.to validate_presence_of(:project) } it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:name) } it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) } it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_inclusion_of(:version).in_array([1, 2]).with_message('must be 1 or 2') }
it_behaves_like 'AtomicInternalId', validate_presence: false do it_behaves_like 'AtomicInternalId', validate_presence: false do
let(:internal_id_attribute) { :iid } let(:internal_id_attribute) { :iid }
...@@ -26,6 +27,17 @@ describe Operations::FeatureFlag do ...@@ -26,6 +27,17 @@ describe Operations::FeatureFlag do
end end
end end
describe 'feature flag version' do
it 'defaults to 1 if unspecified' do
project = create(:project)
feature_flag = described_class.create(name: 'my_flag', project: project, active: true)
expect(feature_flag).to be_valid
expect(feature_flag.version).to eq(1)
end
end
describe 'Scope creation' do describe 'Scope creation' do
subject { described_class.new(**params) } subject { described_class.new(**params) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment