Skip to content

Instantly share code, notes, and snippets.

@amacado
Forked from edavis10/demo.rake
Last active February 13, 2022 22:24
Show Gist options
  • Save amacado/804efbc79726b3fdc7f4c2a2e3c2cf68 to your computer and use it in GitHub Desktop.
Save amacado/804efbc79726b3fdc7f4c2a2e3c2cf68 to your computer and use it in GitHub Desktop.
Rake tasks to create tons of data for Redmine
# Still a work in progress but is good enough for development
# Original by https://gist.github.com/edavis10/90063
#
# Fork enhancements:
# - Updated to latest redmine/faker lib (using .sample instead .rand)
# - Added `rake redmine:demo_data:enumerations` command
# - Added "How-to" instructions
# - Fixed minimum password length
# - Removed random_data dependency
#
# Usage instructions:
# - Copy this .rake file to [redmine_dir]/lib/tasks
# - Execute `gem add faker`
# - Execute `gem add random_data`
# - Use following commands to generate sample data
# - `rake redmine:demo_data` for it all
# - `rake redmine:demo_data:enumerations`
# - `rake redmine:demo_data:users`
# - `rake redmine:demo_data:projects`
# - `rake redmine:demo_data:issues`
# - `rake redmine:demo_data:time_entries`
require 'faker'
namespace :redmine do
desc "Add a set of demo data"
task :demo_data => [:environment,'demo_data:enumerations', 'demo_data:users', 'demo_data:projects', 'demo_data:issues', 'demo_data:time_entries'] do
# no op
end
namespace :demo_data do
desc "Add basic enumerations"
task :enumerations => :environment do
Enumeration.create(:type => "TimeEntryActivity", :name => "Development", :is_default => true)
Enumeration.create(:type => "TimeEntryActivity", :name => "Misc", :is_default => false)
Enumeration.create(:type => "TimeEntryActivity", :name => "Design", :is_default => false)
Enumeration.create(:type => "TimeEntryActivity", :name => "Management", :is_default => false)
Enumeration.create(:type => "IssuePriority", :name => "Normal", :is_default => true)
Enumeration.create(:type => "IssuePriority", :name => "High", :is_default => false)
Enumeration.create(:type => "IssuePriority", :name => "Low", :is_default => false)
IssueStatus.create(:name => "Open")
IssueStatus.create(:name => "WiP")
IssueStatus.create(:name => "Closed")
IssueStatus.create(:name => "Done")
Tracker.create(:name => "Issue", :default_status => IssueStatus.all.sample)
Tracker.create(:name => "Feature", :default_status => IssueStatus.all.sample)
Tracker.create(:name => "Idea", :default_status => IssueStatus.all.sample)
end
desc "Add up to 20 random users"
task :users => :environment do
status = [User::STATUS_ACTIVE, User::STATUS_REGISTERED, User::STATUS_LOCKED]
(1..20).each do
user = User.new(
:firstname => Faker::Name.first_name,
:lastname => Faker::Name.last_name,
:mail => Faker::Internet.free_email,
:status => status.sample
)
user.login = Faker::Internet.user_name
user.password = '12345678'
user.password_confirmation = '12345678'
user.save
end
puts "#{User.count} users total"
end
desc "Add up to 30 random projects"
task :projects => :environment do
(1..30).each do
project = Project.create(
:name => Faker::Internet.domain_word,
:description => Faker::Company.bs,
:homepage => Faker::Internet.domain_name,
:identifier => Faker::Internet.domain_word
)
project.trackers = Tracker.all
project.save
end
puts "#{Project.count} projects total"
end
desc "Add up to 1000 random issues"
task :issues => :environment do
projects = Project.all
status = IssueStatus.all
priorities = IssuePriority.all
users = User.all
(1..1000).each do
Issue.create(
:tracker => Tracker.first,
:project => projects.sample, # from faker gem
:subject => Faker::Company.catch_phrase,
:description => Faker::Lorem.paragraph_by_chars(number: 500, supplemental: true),
:status => status.sample,
:priority => priorities.sample,
:author => users.sample,
:assigned_to => users.sample
)
end
puts "#{Issue.count} issues total"
end
desc "Add up to 3000 random time entries"
task :time_entries => :environment do
users = User.all
projects = Project.all
issues = Issue.all
activities = TimeEntryActivity.all
(1..3000).each do
issue = issues.sample
TimeEntry.create(
:project => issue.project,
:user => users.sample,
:issue => issue,
:hours => (1..20).to_a.sample,
:comments => Faker::Hacker.say_something_smart,
:activity => activities.sample,
:spent_on => Faker::Date.between(from: 2.year.ago, to: Date.today)
)
end
puts "#{TimeEntry.count} time entries total"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment