BigW Consortium Gitlab

20140502125220_migrate_repo_size.rb 995 Bytes
Newer Older
1
# rubocop:disable all
2
class MigrateRepoSize < ActiveRecord::Migration
3 4
  DOWNTIME = false

5
  def up
6 7 8 9 10
    project_data = execute('SELECT projects.id, namespaces.path AS namespace_path, projects.path AS project_path FROM projects LEFT JOIN namespaces ON projects.namespace_id = namespaces.id')

    project_data.each do |project|
      id = project['id']
      namespace_path = project['namespace_path'] || ''
11 12
      repos_path = Gitlab.config.gitlab_shell['repos_path'] || Gitlab.config.repositories.storages.default
      path = File.join(repos_path, namespace_path, project['project_path'] + '.git')
13

14
      begin
15 16
        repo = Gitlab::Git::Repository.new(path)
        if repo.empty?
17 18
          print '-'
        else
19
          size = repo.size
20
          print '.'
21
          execute("UPDATE projects SET repository_size = #{size} WHERE id = #{id}")
22
        end
23 24
      rescue => e
        puts "\nFailed to update project #{id}: #{e}"
25 26
      end
    end
27
    puts "\nDone"
28 29 30 31 32
  end

  def down
  end
end