From 13af7de9411ef20fcab00f7c5d7cdc6b90dbd4b7 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 14 Sep 2014 20:38:57 +0300 Subject: [PATCH] Fix migration and association for new members table Signed-off-by: Dmitriy Zaporozhets --- app/models/members/group_member.rb | 2 ++ app/models/members/project_member.rb | 2 ++ app/models/user.rb | 6 ++-- app/services/system_hooks_service.rb | 2 +- .../20140914113604_add_members_table.rb | 5 +++ ...0914145549_migrate_to_new_members_model.rb | 11 +++++++ ...40914145549_migrate_to_newmembers_model.rb | 31 ------------------- ...20140914173417_remove_old_member_tables.rb | 26 ++++++++++++++++ db/schema.rb | 31 ++++--------------- 9 files changed, 56 insertions(+), 60 deletions(-) create mode 100644 db/migrate/20140914145549_migrate_to_new_members_model.rb delete mode 100644 db/migrate/20140914145549_migrate_to_newmembers_model.rb create mode 100644 db/migrate/20140914173417_remove_old_member_tables.rb diff --git a/app/models/members/group_member.rb b/app/models/members/group_member.rb index 7f68662de70..e7eb4a18e34 100644 --- a/app/models/members/group_member.rb +++ b/app/models/members/group_member.rb @@ -1,6 +1,8 @@ class GroupMember < Member SOURCE_TYPE = 'Group' + belongs_to :group, class_name: 'Group', foreign_key: 'source_id' + # Make sure group member points only to group as it source default_value_for :source_type, SOURCE_TYPE validates_format_of :source_type, with: /\AGroup\z/ diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index 403b87b7e36..3f8137ed06e 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -3,6 +3,8 @@ class ProjectMember < Member include Gitlab::ShellAdapter + belongs_to :project, class_name: 'Project', foreign_key: 'source_id' + # Make sure project member points only to project as it source default_value_for :source_type, SOURCE_TYPE validates_format_of :source_type, with: /\AProject\z/ diff --git a/app/models/user.rb b/app/models/user.rb index ef006e07b64..d5a6b468fcd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -85,8 +85,8 @@ class User < ActiveRecord::Base has_many :project_members, source: 'ProjectMember' has_many :group_members, source: 'GroupMember' has_many :groups, through: :group_members - has_many :owned_groups, -> { where group_members: { access_level: Gitlab::Access::OWNER } }, through: :group_members, source: :group - has_many :masters_groups, -> { where group_members: { access_level: Gitlab::Access::MASTER } }, through: :group_members, source: :group + has_many :owned_groups, -> { where members: { access_level: Gitlab::Access::OWNER } }, through: :group_members, source: :group + has_many :masters_groups, -> { where members: { access_level: Gitlab::Access::MASTER } }, through: :group_members, source: :group # Projects has_many :groups_projects, through: :groups, source: :projects @@ -97,7 +97,7 @@ class User < ActiveRecord::Base has_many :starred_projects, through: :users_star_projects, source: :project has_many :snippets, dependent: :destroy, foreign_key: :author_id, class_name: "Snippet" - has_many :project_members, dependent: :destroy + has_many :project_members, dependent: :destroy, class_name: 'ProjectMember' has_many :issues, dependent: :destroy, foreign_key: :author_id has_many :notes, dependent: :destroy, foreign_key: :author_id has_many :merge_requests, dependent: :destroy, foreign_key: :author_id diff --git a/app/services/system_hooks_service.rb b/app/services/system_hooks_service.rb index 7de85c51805..a6b68749a71 100644 --- a/app/services/system_hooks_service.rb +++ b/app/services/system_hooks_service.rb @@ -54,7 +54,7 @@ class SystemHooksService data.merge!({ project_name: model.project.name, project_path: model.project.path, - project_id: model.project_id, + project_id: model.project.id, user_name: model.user.name, user_email: model.user.email, access_level: model.human_access, diff --git a/db/migrate/20140914113604_add_members_table.rb b/db/migrate/20140914113604_add_members_table.rb index 1738c8dc655..d311f3033ee 100644 --- a/db/migrate/20140914113604_add_members_table.rb +++ b/db/migrate/20140914113604_add_members_table.rb @@ -10,5 +10,10 @@ class AddMembersTable < ActiveRecord::Migration t.timestamps end + + add_index :members, :type + add_index :members, :user_id + add_index :members, :access_level + add_index :members, [:source_id, :source_type] end end diff --git a/db/migrate/20140914145549_migrate_to_new_members_model.rb b/db/migrate/20140914145549_migrate_to_new_members_model.rb new file mode 100644 index 00000000000..b4f7718f26d --- /dev/null +++ b/db/migrate/20140914145549_migrate_to_new_members_model.rb @@ -0,0 +1,11 @@ +class MigrateToNewMembersModel < ActiveRecord::Migration + def up + execute "INSERT INTO members ( user_id, source_id, source_type, access_level, notification_level, type ) SELECT user_id, group_id, 'Group', group_access, notification_level, 'GroupMember' FROM users_groups" + execute "INSERT INTO members ( user_id, source_id, source_type, access_level, notification_level, type ) SELECT user_id, project_id, 'Project', project_access, notification_level, 'ProjectMember' FROM users_projects" + end + + def down + Member.delete_all + end +end + diff --git a/db/migrate/20140914145549_migrate_to_newmembers_model.rb b/db/migrate/20140914145549_migrate_to_newmembers_model.rb deleted file mode 100644 index 9c4517f95e5..00000000000 --- a/db/migrate/20140914145549_migrate_to_newmembers_model.rb +++ /dev/null @@ -1,31 +0,0 @@ -class MigrateToNewmembersModel < ActiveRecord::Migration - def up - UsersGroup.find_each(batch_size: 500) do |user_group| - GroupMember.create( - user_id: user_group.user_id, - source_type: 'Group', - source_id: user_group.group_id, - access_level: user_group.group_access, - notification_level: user_group.notification_level, - ) - - print '.' - end - - UsersProject.find_each(batch_size: 500) do |user_project| - ProjectMember.create( - user_id: user_project.user_id, - source_type: 'Project', - source_id: user_project.project_id, - access_level: user_project.project_access, - notification_level: user_project.notification_level, - ) - - print '.' - end - end - - def down - Member.destroy_all - end -end diff --git a/db/migrate/20140914173417_remove_old_member_tables.rb b/db/migrate/20140914173417_remove_old_member_tables.rb new file mode 100644 index 00000000000..408b9551dbb --- /dev/null +++ b/db/migrate/20140914173417_remove_old_member_tables.rb @@ -0,0 +1,26 @@ +class RemoveOldMemberTables < ActiveRecord::Migration + def up + drop_table :users_groups + drop_table :users_projects + end + + def down + create_table :users_groups do |t| + t.integer :group_access, null: false + t.integer :group_id, null: false + t.integer :user_id, null: false + t.integer :notification_level, null: false, default: 3 + + t.timestamps + end + + create_table :users_projects do |t| + t.integer :project_access, null: false + t.integer :project_id, null: false + t.integer :user_id, null: false + t.integer :notification_level, null: false, default: 3 + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 41ae717c670..4e249caa022 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: 20140914145549) do +ActiveRecord::Schema.define(version: 20140914173417) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -141,6 +141,11 @@ ActiveRecord::Schema.define(version: 20140914145549) do t.datetime "updated_at" end + add_index "members", ["access_level"], name: "index_members_on_access_level", using: :btree + add_index "members", ["source_id", "source_type"], name: "index_members_on_source_id_and_source_type", using: :btree + add_index "members", ["type"], name: "index_members_on_type", using: :btree + add_index "members", ["user_id"], name: "index_members_on_user_id", using: :btree + create_table "merge_request_diffs", force: true do |t| t.string "state" t.text "st_commits" @@ -374,30 +379,6 @@ ActiveRecord::Schema.define(version: 20140914145549) do add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree add_index "users", ["username"], name: "index_users_on_username", using: :btree - create_table "users_groups", force: true do |t| - t.integer "group_access", null: false - t.integer "group_id", null: false - t.integer "user_id", null: false - t.datetime "created_at" - t.datetime "updated_at" - t.integer "notification_level", default: 3, null: false - end - - add_index "users_groups", ["user_id"], name: "index_users_groups_on_user_id", using: :btree - - create_table "users_projects", force: true do |t| - t.integer "user_id", null: false - t.integer "project_id", null: false - t.datetime "created_at" - t.datetime "updated_at" - t.integer "project_access", default: 0, null: false - t.integer "notification_level", default: 3, null: false - end - - add_index "users_projects", ["project_access"], name: "index_users_projects_on_project_access", using: :btree - add_index "users_projects", ["project_id"], name: "index_users_projects_on_project_id", using: :btree - add_index "users_projects", ["user_id"], name: "index_users_projects_on_user_id", using: :btree - create_table "users_star_projects", force: true do |t| t.integer "project_id", null: false t.integer "user_id", null: false