Sha256: 167deac12126e90731b1576c64dbe16f3d222e697cbbc99e542010b7c51bbda8

Contents?: true

Size: 1.25 KB

Versions: 18

Compression:

Stored size: 1.25 KB

Contents

class AddParentShaToCommits < ActiveRecord::Migration
  def up
    add_column :commits, :parent_sha, :string

    commits = Commit.reachable.reorder(nil)
    projects = Project.unretired
      .where(id: commits.pluck("DISTINCT project_id"))
      .reject { |project| project.repo.nil? }
    commits = commits.where(project_id: projects.map(&:id))

    # Do one `git pull` before starting
    puts "\e[94mSyncing commits...\e[0m"
    pbar = ProgressBar.new("projects", projects.count)
    projects.each do |project|
      project.commits.sync!
      pbar.inc
    end
    pbar.finish

    puts "\e[94mUpdating commits...\e[0m"
    pbar = ProgressBar.new("commits", commits.count)
    projects.each do |project|
      repo = project.repo
      conn = repo.send(:connection)
      project_commits = commits.where(project_id: project.id)

      project_commits.pluck(:sha).each do |sha|
        begin
          parent_sha = conn.lookup(sha).parent_oids[0]
          project_commits.where(sha: sha).update_all(parent_sha: parent_sha) if parent_sha
        rescue Rugged::OdbError
          project_commits.where(sha: sha).update_all(unreachable: true)
        end
        pbar.inc
      end
    end
    pbar.finish
  end

  def down
    remove_column :commits, :parent_sha
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
houston-core-0.8.0.pre db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.7.0 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.7.0.beta4 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.7.0.beta3 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.7.0.beta2 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.7.0.beta db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.6.3 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.6.2 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.6.1 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.6.0 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.6 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.5 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.4 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.3 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.2 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.1 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.0 db/migrate/20150808193354_add_parent_sha_to_commits.rb
houston-core-0.5.0.beta1 db/migrate/20150808193354_add_parent_sha_to_commits.rb