diff options
Diffstat (limited to 'db')
3 files changed, 67 insertions, 3 deletions
diff --git a/db/post_migrate/20180511174224_add_unique_constraint_to_project_features_project_id.rb b/db/post_migrate/20180511174224_add_unique_constraint_to_project_features_project_id.rb new file mode 100644 index 00000000000..88a9f5f8256 --- /dev/null +++ b/db/post_migrate/20180511174224_add_unique_constraint_to_project_features_project_id.rb @@ -0,0 +1,43 @@ +class AddUniqueConstraintToProjectFeaturesProjectId < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + class ProjectFeature < ActiveRecord::Base + self.table_name = 'project_features' + + include EachBatch + end + + def up + remove_duplicates + + add_concurrent_index :project_features, :project_id, unique: true, name: 'index_project_features_on_project_id_unique' + remove_concurrent_index_by_name :project_features, 'index_project_features_on_project_id' + rename_index :project_features, 'index_project_features_on_project_id_unique', 'index_project_features_on_project_id' + end + + def down + rename_index :project_features, 'index_project_features_on_project_id', 'index_project_features_on_project_id_old' + add_concurrent_index :project_features, :project_id + remove_concurrent_index_by_name :project_features, 'index_project_features_on_project_id_old' + end + + private + + def remove_duplicates + features = ProjectFeature + .select('MAX(id) AS max, COUNT(id), project_id') + .group(:project_id) + .having('COUNT(id) > 1') + + features.each do |feature| + ProjectFeature + .where(project_id: feature['project_id']) + .where('id <> ?', feature['max']) + .each_batch { |batch| batch.delete_all } + end + end +end diff --git a/db/post_migrate/20180512061621_add_not_null_constraint_to_project_features_project_id.rb b/db/post_migrate/20180512061621_add_not_null_constraint_to_project_features_project_id.rb new file mode 100644 index 00000000000..5a6d6ff4a10 --- /dev/null +++ b/db/post_migrate/20180512061621_add_not_null_constraint_to_project_features_project_id.rb @@ -0,0 +1,21 @@ +class AddNotNullConstraintToProjectFeaturesProjectId < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + class ProjectFeature < ActiveRecord::Base + include EachBatch + + self.table_name = 'project_features' + end + + def up + ProjectFeature.where(project_id: nil).delete_all + + change_column_null :project_features, :project_id, false + end + + def down + change_column_null :project_features, :project_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4abca1789a0..ed29d202f91 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: 20180511090724) do +ActiveRecord::Schema.define(version: 20180512061621) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1494,7 +1494,7 @@ ActiveRecord::Schema.define(version: 20180511090724) do add_index "project_deploy_tokens", ["project_id", "deploy_token_id"], name: "index_project_deploy_tokens_on_project_id_and_deploy_token_id", unique: true, using: :btree create_table "project_features", force: :cascade do |t| - t.integer "project_id" + t.integer "project_id", null: false t.integer "merge_requests_access_level" t.integer "issues_access_level" t.integer "wiki_access_level" @@ -1505,7 +1505,7 @@ ActiveRecord::Schema.define(version: 20180511090724) do t.integer "repository_access_level", default: 20, null: false end - add_index "project_features", ["project_id"], name: "index_project_features_on_project_id", using: :btree + add_index "project_features", ["project_id"], name: "index_project_features_on_project_id", unique: true, using: :btree create_table "project_group_links", force: :cascade do |t| t.integer "project_id", null: false |