20160614065105_add_association_from_commit_to_user.rb revision 81da36894af70bbb8d8e24b004026ad4c5c1bc99
1516N/Aclass AddAssociationFromCommitToUser < MigrationWithData
581N/A def up
581N/A rename_column :commits, :author, :author_name
581N/A rename_column :commits, :committer, :committer_name
581N/A
581N/A add_column :commits, :author_email, :string
581N/A add_column :commits, :committer_email, :string
581N/A
581N/A add_column :commits, :pusher_name, :string
581N/A
581N/A # We emphasize "null: true" because there may be no user
581N/A add_column :commits, :author_id, :integer, null: true
581N/A add_column :commits, :committer_id, :integer, null: true
581N/A add_column :commits, :pusher_id, :integer, null: true
581N/A
581N/A add_index :commits, :author_id
581N/A add_index :commits, :committer_id
581N/A add_index :commits, :pusher_id
581N/A
581N/A up_data
581N/A
581N/A remove_column :ontology_versions, :user_id
926N/A end
3158N/A
926N/A def down
581N/A add_column :ontology_versions, :user_id, :integer
581N/A
581N/A down_data
581N/A
1715N/A remove_index :commits, :author_id
581N/A remove_index :commits, :committer_id
1895N/A remove_index :commits, :pusher_id
581N/A
1895N/A remove_column :commits, :author_id
581N/A remove_column :commits, :committer_id
2055N/A remove_column :commits, :pusher_id
581N/A
1715N/A remove_column :commits, :pusher_name
581N/A
581N/A remove_column :commits, :author_email
792N/A remove_column :commits, :committer_email
581N/A
1895N/A rename_column :commits, :author_name
581N/A rename_column :commits, :committer_name
581N/A end
581N/A
581N/A protected
581N/A
581N/A def up_data
581N/A up_ontology_version
581N/A up_split_name_and_email
611N/A end
611N/A
611N/A def down_data
611N/A Commit.find_each do |commit|
581N/A attrs = select_attributes(commit,
581N/A :pusher_id, :commit_oid,
581N/A :author_name, :author_email,
581N/A :committer_name, :committer_email)
581N/A down_ontology_version(attrs)
581N/A down_commit(attrs)
581N/A end
581N/A end
2026N/A
2097N/A def up_ontology_version
2026N/A OntologyVersion.find_each do |ontology_version|
2026N/A attrs = select_attributes(ontology_version, :user_id, :commit_oid)
2026N/A user_name = select_attributes_class(User, attrs[:id], :name)[:name]
2026N/A commit = Commit.where(commit_oid: attrs[:commit_oid]).first
2026N/A update_columns(commit,
2026N/A pusher_id: attrs[:user_id], pusher_name: user_name)
2026N/A end
2026N/A end
2026N/A
2026N/A def up_split_name_and_email
2026N/A Commit.find_each do |commit|
2026N/A attrs = select_attributes(commit, :author_name, :committer_name)
2026N/A author_info =
2026N/A attrs[:author_name].match(/\A(?<name>.*) <(?<email>[^\>]*)>\z/)
2026N/A committer_info =
2026N/A attrs[:committer_name].match(/\A(?<name>.*) <(?<email>[^\>]*)>\z/)
2026N/A update_columns(commit, **data(author_info, committer_info))
3158N/A end
3158N/A end
2026N/A
2055N/A def data(author_info, committer_info)
2055N/A data = {author_name: author_info[:name],
2055N/A author_email: author_info[:email],
2055N/A committer_name: committer_info[:name],
2055N/A committer_email: committer_info[:email]}
2055N/A data[:author_id] =
2258N/A User.where(email: data[:author_email]).select(:id).first.try(:id)
709N/A data[:committer_id] =
1895N/A User.where(email: data[:committer_email]).select(:id).first.try(:id)
1895N/A data
709N/A end
709N/A
1895N/A def down_ontology_version(attrs)
1895N/A ontology_version =
709N/A OntologyVersion.where(commit_oid: attrs[:commit_oid]).first
1895N/A update_columns(ontology_version, user_id: attrs[:pusher_id])
1895N/A end
709N/A
709N/A def down_commit(attrs)
1895N/A author_info = "#{attrs[:author_name]} <#{attrs[:author_email]}>"
581N/A committer_info = "#{attrs[:committer_name]} <#{attrs[:committer_email]}>"
581N/A update_columns(commit,
author_name: author_info, committer_name: committer_info)
end
end