Commit 39f80884db394decdb4b4a21a30bc1ecb3203944

Authored by Dmitriy Zaporozhets
1 parent 6e73b208

Improve developer seeds

Return execution of observers in seeds.
Mute email sending to letter opening in you browser.
Added `rake dev` task to reset db and add seeds.

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
db/fixtures/development/01_admin.rb
@@ -8,8 +8,7 @@ User.seed(:id, [ @@ -8,8 +8,7 @@ User.seed(:id, [
8 password_confirmation: "5iveL!fe", 8 password_confirmation: "5iveL!fe",
9 admin: true, 9 admin: true,
10 projects_limit: 100, 10 projects_limit: 100,
11 - theme_id: Gitlab::Theme::MARS 11 + theme_id: Gitlab::Theme::MARS,
  12 + confirmed_at: DateTime.now
12 } 13 }
13 ]) 14 ])
14 -  
15 -User.find(1).confirm!  
db/fixtures/development/04_project.rb
1 -project_urls = [  
2 - 'https://github.com/documentcloud/underscore.git',  
3 - 'https://github.com/diaspora/diaspora.git',  
4 - 'https://github.com/diaspora/diaspora-project-site.git',  
5 - 'https://github.com/diaspora/diaspora-client.git',  
6 - 'https://github.com/brightbox/brightbox-cli.git',  
7 - 'https://github.com/brightbox/puppet.git',  
8 - 'https://github.com/gitlabhq/gitlabhq.git',  
9 - 'https://github.com/gitlabhq/gitlab-ci.git',  
10 - 'https://github.com/gitlabhq/gitlab-recipes.git',  
11 - 'https://github.com/gitlabhq/gitlab-shell.git',  
12 - 'https://github.com/gitlabhq/grack.git',  
13 - 'https://github.com/gitlabhq/testme.git',  
14 - 'https://github.com/twitter/flight.git',  
15 - 'https://github.com/twitter/typeahead.js.git',  
16 - 'https://github.com/h5bp/html5-boilerplate.git',  
17 - 'https://github.com/h5bp/mobile-boilerplate.git',  
18 -]  
19 -  
20 -project_urls.each_with_index do |url, i|  
21 - group_path, project_path = url.split('/')[-2..-1]  
22 -  
23 - group = Group.find_by(path: group_path)  
24 -  
25 - unless group  
26 - group = Group.new(  
27 - name: group_path.titleize,  
28 - path: group_path  
29 - )  
30 - group.description = Faker::Lorem.sentence  
31 - group.save  
32 -  
33 - group.add_owner(User.first)  
34 - end  
35 -  
36 - project_path.gsub!(".git", "")  
37 -  
38 - params = {  
39 - import_url: url,  
40 - namespace_id: group.id,  
41 - name: project_path.titleize,  
42 - description: Faker::Lorem.sentence  
43 - }  
44 -  
45 - project = Projects::CreateService.new(User.first, params).execute  
46 -  
47 - if project.valid?  
48 - print '.'  
49 - else  
50 - puts project.errors.full_messages  
51 - print 'F' 1 +Gitlab::Seeder.quiet do
  2 + project_urls = [
  3 + 'https://github.com/documentcloud/underscore.git',
  4 + 'https://github.com/diaspora/diaspora.git',
  5 + 'https://github.com/diaspora/diaspora-project-site.git',
  6 + 'https://github.com/diaspora/diaspora-client.git',
  7 + 'https://github.com/brightbox/brightbox-cli.git',
  8 + 'https://github.com/brightbox/puppet.git',
  9 + 'https://github.com/gitlabhq/gitlabhq.git',
  10 + 'https://github.com/gitlabhq/gitlab-ci.git',
  11 + 'https://github.com/gitlabhq/gitlab-recipes.git',
  12 + 'https://github.com/gitlabhq/gitlab-shell.git',
  13 + 'https://github.com/gitlabhq/grack.git',
  14 + 'https://github.com/gitlabhq/testme.git',
  15 + 'https://github.com/twitter/flight.git',
  16 + 'https://github.com/twitter/typeahead.js.git',
  17 + 'https://github.com/h5bp/html5-boilerplate.git',
  18 + 'https://github.com/h5bp/mobile-boilerplate.git',
  19 + ]
  20 +
  21 + project_urls.each_with_index do |url, i|
  22 + group_path, project_path = url.split('/')[-2..-1]
  23 +
  24 + group = Group.find_by(path: group_path)
  25 +
  26 + unless group
  27 + group = Group.new(
  28 + name: group_path.titleize,
  29 + path: group_path
  30 + )
  31 + group.description = Faker::Lorem.sentence
  32 + group.save
  33 +
  34 + group.add_owner(User.first)
  35 + end
  36 +
  37 + project_path.gsub!(".git", "")
  38 +
  39 + params = {
  40 + import_url: url,
  41 + namespace_id: group.id,
  42 + name: project_path.titleize,
  43 + description: Faker::Lorem.sentence
  44 + }
  45 +
  46 + project = Projects::CreateService.new(User.first, params).execute
  47 +
  48 + if project.valid?
  49 + print '.'
  50 + else
  51 + puts project.errors.full_messages
  52 + print 'F'
  53 + end
52 end 54 end
53 end 55 end
db/fixtures/development/05_users.rb
@@ -6,6 +6,7 @@ Gitlab::Seeder.quiet do @@ -6,6 +6,7 @@ Gitlab::Seeder.quiet do
6 username: Faker::Internet.user_name, 6 username: Faker::Internet.user_name,
7 name: Faker::Name.name, 7 name: Faker::Name.name,
8 email: Faker::Internet.email, 8 email: Faker::Internet.email,
  9 + confirmed_at: DateTime.now
9 }]) 10 }])
10 print '.' 11 print '.'
11 rescue ActiveRecord::RecordNotSaved 12 rescue ActiveRecord::RecordNotSaved
db/fixtures/development/06_teams.rb
1 -ActiveRecord::Base.observers.disable :all  
2 -  
3 Gitlab::Seeder.quiet do 1 Gitlab::Seeder.quiet do
4 Group.all.each do |group| 2 Group.all.each do |group|
5 User.all.sample(4).each do |user| 3 User.all.sample(4).each do |user|
db/fixtures/development/09_issues.rb
1 -ActiveRecord::Base.observers.disable :all  
2 -  
3 Gitlab::Seeder.quiet do 1 Gitlab::Seeder.quiet do
4 (1..300).each do |i| 2 (1..300).each do |i|
5 # Random Project 3 # Random Project
@@ -12,9 +10,7 @@ Gitlab::Seeder.quiet do @@ -12,9 +10,7 @@ Gitlab::Seeder.quiet do
12 10
13 user_id = user.id 11 user_id = user.id
14 12
15 - begin  
16 - Thread.current[:current_user] = user  
17 - 13 + Gitlab::Seeder.by_user(user) do
18 Issue.seed(:id, [{ 14 Issue.seed(:id, [{
19 id: i, 15 id: i,
20 project_id: project.id, 16 project_id: project.id,
@@ -25,8 +21,6 @@ Gitlab::Seeder.quiet do @@ -25,8 +21,6 @@ Gitlab::Seeder.quiet do
25 title: Faker::Lorem.sentence(6), 21 title: Faker::Lorem.sentence(6),
26 description: Faker::Lorem.sentence 22 description: Faker::Lorem.sentence
27 }]) 23 }])
28 - ensure  
29 - Thread.current[:current_user] = nil  
30 end 24 end
31 print('.') 25 print('.')
32 end 26 end
db/fixtures/development/10_merge_requests.rb
1 -ActiveRecord::Base.observers.disable :all  
2 -  
3 Gitlab::Seeder.quiet do 1 Gitlab::Seeder.quiet do
4 (1..100).each do |i| 2 (1..100).each do |i|
5 # Random Project 3 # Random Project
@@ -17,9 +15,8 @@ Gitlab::Seeder.quiet do @@ -17,9 +15,8 @@ Gitlab::Seeder.quiet do
17 next if branches.uniq.size < 2 15 next if branches.uniq.size < 2
18 16
19 user_id = user.id 17 user_id = user.id
20 - begin  
21 - Thread.current[:current_user] = user  
22 18
  19 + Gitlab::Seeder.by_user(user) do
23 MergeRequest.seed(:id, [{ 20 MergeRequest.seed(:id, [{
24 id: i, 21 id: i,
25 source_branch: branches.first, 22 source_branch: branches.first,
@@ -31,8 +28,6 @@ Gitlab::Seeder.quiet do @@ -31,8 +28,6 @@ Gitlab::Seeder.quiet do
31 milestone: project.milestones.sample, 28 milestone: project.milestones.sample,
32 title: Faker::Lorem.sentence(6) 29 title: Faker::Lorem.sentence(6)
33 }]) 30 }])
34 - ensure  
35 - Thread.current[:current_user] = nil  
36 end 31 end
37 print('.') 32 print('.')
38 end 33 end
db/fixtures/development/11_keys.rb
1 -ActiveRecord::Base.observers.enable :all  
2 -  
3 Gitlab::Seeder.quiet do 1 Gitlab::Seeder.quiet do
4 User.first(30).each_with_index do |user, i| 2 User.first(30).each_with_index do |user, i|
5 Key.seed(:id, [ 3 Key.seed(:id, [
db/fixtures/development/12_snippets.rb
1 -ActiveRecord::Base.observers.disable :all  
2 -  
3 Gitlab::Seeder.quiet do 1 Gitlab::Seeder.quiet do
4 contents = [ 2 contents = [
5 `curl https://gist.github.com/randx/4275756/raw/da2f262920c96d1a970d48bf2e99147954b1f4bd/glus1204.sh`, 3 `curl https://gist.github.com/randx/4275756/raw/da2f262920c96d1a970d48bf2e99147954b1f4bd/glus1204.sh`,
db/fixtures/development/13_comments.rb
1 -ActiveRecord::Base.observers.disable :all 1 +Gitlab::Seeder.quiet do
  2 + Issue.all.limit(10).each_with_index do |issue, i|
  3 + 5.times do
  4 + user = issue.project.team.users.sample
2 5
3 -Issue.all.limit(10).each_with_index do |issue, i|  
4 - 5.times do  
5 - Note.seed(:id, [{  
6 - project_id: issue.project.id,  
7 - author_id: issue.project.team.users.sample.id,  
8 - note: Faker::Lorem.sentence,  
9 - noteable_id: issue.id,  
10 - noteable_type: 'Issue'  
11 - }]) 6 + Gitlab::Seeder.by_user(user) do
  7 + Note.seed(:id, [{
  8 + project_id: issue.project.id,
  9 + author_id: user.id,
  10 + note: Faker::Lorem.sentence,
  11 + noteable_id: issue.id,
  12 + noteable_type: 'Issue'
  13 + }])
  14 +
  15 + print '.'
  16 + end
  17 + end
12 end 18 end
13 end 19 end
lib/gitlab/seeder.rb
1 module Gitlab 1 module Gitlab
2 class Seeder 2 class Seeder
3 def self.quiet 3 def self.quiet
  4 + mute_mailer
4 SeedFu.quiet = true 5 SeedFu.quiet = true
5 yield 6 yield
6 SeedFu.quiet = false 7 SeedFu.quiet = false
7 puts "\nOK".green 8 puts "\nOK".green
8 end 9 end
  10 +
  11 + def self.by_user(user)
  12 + begin
  13 + Thread.current[:current_user] = user
  14 + yield
  15 + ensure
  16 + Thread.current[:current_user] = nil
  17 + end
  18 + end
  19 +
  20 + def self.mute_mailer
  21 + code = <<-eos
  22 +def Notify.delay
  23 + self
  24 +end
  25 + eos
  26 + eval(code)
  27 + end
9 end 28 end
10 end 29 end
lib/tasks/dev.rake
  1 +task dev: ["dev:setup"]
  2 +
1 namespace :dev do 3 namespace :dev do
2 desc "GITLAB | Setup developer environment (db, fixtures)" 4 desc "GITLAB | Setup developer environment (db, fixtures)"
3 task :setup => :environment do 5 task :setup => :environment do
4 ENV['force'] = 'yes' 6 ENV['force'] = 'yes'
5 - Rake::Task["db:setup"].invoke  
6 - Rake::Task["db:seed_fu"].invoke 7 + Rake::Task["gitlab:setup"].invoke
7 Rake::Task["gitlab:shell:setup"].invoke 8 Rake::Task["gitlab:shell:setup"].invoke
8 end 9 end
9 end 10 end
10 -