From d07addbf6e3841ae31a7e62ecbb29523f4d8c859 Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Mon, 5 Feb 2018 12:26:33 +0100 Subject: Add foreign keys to todos table. Fixes #32282. --- .../20180201110056_add_foreign_keys_to_todos.rb | 38 ++++++++++++++++++++++ db/schema.rb | 3 ++ 2 files changed, 41 insertions(+) create mode 100644 db/migrate/20180201110056_add_foreign_keys_to_todos.rb (limited to 'db') diff --git a/db/migrate/20180201110056_add_foreign_keys_to_todos.rb b/db/migrate/20180201110056_add_foreign_keys_to_todos.rb new file mode 100644 index 00000000000..b7c40f8c01a --- /dev/null +++ b/db/migrate/20180201110056_add_foreign_keys_to_todos.rb @@ -0,0 +1,38 @@ +class AddForeignKeysToTodos < 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('NOT EXISTS ( SELECT true FROM users WHERE id=todos.user_id )').each_batch(of: BATCH_SIZE) do |batch| + batch.delete_all + end + + Todo.where('NOT EXISTS ( SELECT true FROM users WHERE id=todos.author_id )').each_batch(of: BATCH_SIZE) do |batch| + batch.delete_all + end + + Todo.where('note_id IS NOT NULL AND NOT EXISTS ( SELECT true FROM notes WHERE id=todos.note_id )').each_batch(of: BATCH_SIZE) do |batch| + batch.delete_all + end + + add_concurrent_foreign_key :todos, :users, column: :user_id, on_delete: :cascade + add_concurrent_foreign_key :todos, :users, column: :author_id, on_delete: :cascade + add_concurrent_foreign_key :todos, :notes, column: :note_id, on_delete: :cascade + end + + def down + remove_foreign_key :todos, :users + remove_foreign_key :todos, column: :author_id + remove_foreign_key :todos, :notes + end +end diff --git a/db/schema.rb b/db/schema.rb index 14024164359..dd8d14fb401 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2045,7 +2045,10 @@ ActiveRecord::Schema.define(version: 20180202111106) do add_foreign_key "system_note_metadata", "notes", name: "fk_d83a918cb1", on_delete: :cascade add_foreign_key "timelogs", "issues", name: "fk_timelogs_issues_issue_id", on_delete: :cascade add_foreign_key "timelogs", "merge_requests", name: "fk_timelogs_merge_requests_merge_request_id", on_delete: :cascade + add_foreign_key "todos", "notes", name: "fk_91d1f47b13", on_delete: :cascade add_foreign_key "todos", "projects", name: "fk_45054f9c45", on_delete: :cascade + add_foreign_key "todos", "users", column: "author_id", name: "fk_ccf0373936", on_delete: :cascade + add_foreign_key "todos", "users", name: "fk_d94154aa95", on_delete: :cascade add_foreign_key "trending_projects", "projects", on_delete: :cascade add_foreign_key "u2f_registrations", "users" add_foreign_key "user_callouts", "users", on_delete: :cascade -- cgit v1.2.1 From 24a11c957a34d2f22c0276fa8dcc3e2c23d1f164 Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Sun, 4 Feb 2018 20:46:14 +0100 Subject: Set todos#author_id to NOT NULL. --- ...200836_change_author_id_to_not_null_in_todos.rb | 26 ++++++++++++++++++++++ db/schema.rb | 4 ++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 db/post_migrate/20180204200836_change_author_id_to_not_null_in_todos.rb (limited to 'db') diff --git a/db/post_migrate/20180204200836_change_author_id_to_not_null_in_todos.rb b/db/post_migrate/20180204200836_change_author_id_to_not_null_in_todos.rb new file mode 100644 index 00000000000..92c32feebf7 --- /dev/null +++ b/db/post_migrate/20180204200836_change_author_id_to_not_null_in_todos.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index dd8d14fb401..c56cb461320 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" -- cgit v1.2.1