Fix migration and association for new members table

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2014-09-14 20:38:57 +03:00
parent 8210d81381
commit 13af7de941
No known key found for this signature in database
GPG key ID: 627C5F589F467F17
9 changed files with 56 additions and 60 deletions

View file

@ -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/

View file

@ -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/

View file

@ -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

View file

@ -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,

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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