Set todos#author_id to NOT NULL.

This commit is contained in:
Andreas Brandl 2018-02-04 20:46:14 +01:00
parent d07addbf6e
commit 24a11c957a
5 changed files with 31 additions and 3 deletions

View File

@ -28,6 +28,7 @@ class Todo < ActiveRecord::Base
delegate :name, :email, to: :author, prefix: true, allow_nil: true
validates :action, :project, :target_type, :user, presence: true
validates :author, presence: true
validates :target_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?

View File

@ -1,5 +1,5 @@
---
title: Add foreign key constraints to todos table.
title: Add foreign key and NOT NULL constraints to todos table.
merge_request: 16849
author:
type: other

View File

@ -0,0 +1,26 @@
class ChangeAuthorIdToNotNullInTodos < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
class Todo < ActiveRecord::Base
self.table_name = 'todos'
include EachBatch
end
BATCH_SIZE = 1000
DOWNTIME = false
disable_ddl_transaction!
def up
Todo.where(author_id: nil).each_batch(of: BATCH_SIZE) do |batch|
batch.delete_all
end
change_column_null :todos, :author_id, false
end
def down
change_column_null :todos, :author_id, true
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180202111106) do
ActiveRecord::Schema.define(version: 20180204200836) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -1707,7 +1707,7 @@ ActiveRecord::Schema.define(version: 20180202111106) do
t.integer "project_id", null: false
t.integer "target_id"
t.string "target_type", null: false
t.integer "author_id"
t.integer "author_id", null: false
t.integer "action", null: false
t.string "state", null: false
t.datetime "created_at"

View File

@ -20,6 +20,7 @@ describe Todo do
it { is_expected.to validate_presence_of(:action) }
it { is_expected.to validate_presence_of(:target_type) }
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:author) }
context 'for commits' do
subject { described_class.new(target_type: 'Commit') }