From 9fca80592d5d837b11f2b3f56811764857b0616c Mon Sep 17 00:00:00 2001 From: Peter Lehwess Date: Fri, 6 Nov 2015 17:11:01 +0100 Subject: [PATCH] Seperate random and demo data into individual seeders --- app/seeders/demo_data/wiki_seeder.rb | 45 +---- app/seeders/demo_data/work_package_seeder.rb | 162 +-------------- app/seeders/demo_data_seeder.rb | 13 +- .../board_seeder.rb | 2 +- .../{demo_data => random_data}/news_seeder.rb | 7 +- app/seeders/random_data/wiki_seeder.rb | 62 ++++++ .../random_data/work_package_seeder.rb | 191 ++++++++++++++++++ app/seeders/random_data_seeder.rb | 54 +++++ db/production.rb | 1 + .../{demo_data.rake => random_data.rake} | 21 +- 10 files changed, 341 insertions(+), 217 deletions(-) rename app/seeders/{demo_data => random_data}/board_seeder.rb (99%) rename app/seeders/{demo_data => random_data}/news_seeder.rb (97%) create mode 100644 app/seeders/random_data/wiki_seeder.rb create mode 100644 app/seeders/random_data/work_package_seeder.rb create mode 100644 app/seeders/random_data_seeder.rb create mode 100644 db/production.rb rename lib/tasks/{demo_data.rake => random_data.rake} (73%) diff --git a/app/seeders/demo_data/wiki_seeder.rb b/app/seeders/demo_data/wiki_seeder.rb index 9d8d8014e72e..7916dfede074 100644 --- a/app/seeders/demo_data/wiki_seeder.rb +++ b/app/seeders/demo_data/wiki_seeder.rb @@ -27,52 +27,11 @@ # See doc/COPYRIGHT.rdoc for more details. module DemoData class WikiSeeder - attr_accessor :user, :project + def self.seed!(project) + user = User.admin.first - def initialize(project) - self.user = User.admin.first - self.project = project - end - - def seed!(random: true) puts '' print ' ↳ Creating wikis' - - if random - create_random_wiki_pages - else - create_demo_wiki - end - end - - def create_random_wiki_pages - rand(5).times do - print '.' - wiki_page = WikiPage.create( - wiki: project.wiki, - title: Faker::Lorem.words(5).join(' ') - ) - - ## create some wiki contents - rand(5).times do - print '.' - wiki_content = WikiContent.create( - page: wiki_page, - authorz: user, - text: Faker::Lorem.paragraph(5, true, 3) - ) - - ## create some journal entries - rand(5).times do - wiki_content.reload - wiki_content.text = Faker::Lorem.paragraph(5, true, 3) if rand(99).even? - wiki_content.save! - end - end - end - end - - def create_demo_wiki print '.' wiki_page = WikiPage.create!( wiki: project.wiki, diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rb index a2b17d0aa99a..8fed5d8f8814 100644 --- a/app/seeders/demo_data/work_package_seeder.rb +++ b/app/seeders/demo_data/work_package_seeder.rb @@ -38,40 +38,15 @@ def initialize(project) self.types = project.types.all.reject(&:is_milestone?) end - def seed!(random: true) + def seed! puts '' print ' ↳ Creating work_packages' - if random - seed_random_work_packages - else - seed_demo_work_packages - end + seed_demo_work_packages end private - def seed_random_work_packages - rand(50).times do - print '.' - work_package = new_work_package - work_package.priority = IssuePriority.all.sample - work_package.description = Faker::Lorem.paragraph(5, true, 3) - work_package.save! - end - - work_package = WorkPackage.first - - if repository - add_changeset(work_package) - end - - add_time_entries(work_package) - add_attachments(work_package) - add_custom_values(work_package) - make_changes(work_package) - end - def seed_demo_work_packages work_packages_data = I18n.t('seeders.demo_data.work_packages') @@ -79,7 +54,9 @@ def seed_demo_work_packages start_date = calculate_start_date(attributes[:start]) print '.' - work_package = new_work_package( + work_package = WorkPackage.create!( + project: project, + author: user, subject: attributes[:subject], status: Status.find_by!(name: I18n.t(attributes[:status_name])), type: Type.find_by!(name: I18n.t(attributes[:type_name])), @@ -91,7 +68,9 @@ def seed_demo_work_packages start_date = calculate_start_date(child_attributes[:start]) print '.' - child = new_work_package( + child = WorkPackage.create!( + project: project, + author: user, subject: child_attributes[:subject], status: Status.find_by!(name: I18n.t(child_attributes[:status_name])), type: Type.find_by!(name: I18n.t(child_attributes[:type_name])), @@ -105,131 +84,6 @@ def seed_demo_work_packages end end - def add_changeset(work_package) - 2.times do |changeset_count| - print '.' - changeset = Changeset.create( - repository: repository, - user: user, - revision: work_package.id * 10 + changeset_count, - scmid: work_package.id * 10 + changeset_count, - user: user, - work_packages: [work_package], - committer: Faker::Name.name, - committed_on: Date.today, - comments: Faker::Lorem.words(8).join(' ') - ) - - 5.times do - print '.' - change = Change.create( - action: Faker::Lorem.characters(1), - path: Faker::Internet.url - ) - - changeset.file_changes << change - end - - repository.changesets << changeset - - changeset.save! - - rand(5).times do - print '.' - changeset.reload - - changeset.committer = Faker::Name.name if rand(99).even? - changeset.committed_on = Date.today + rand(999) if rand(99).even? - changeset.comments = Faker::Lorem.words(8).join(' ') if rand(99).even? - - changeset.save! - end - end - end - - def add_time_entries(work_package) - 5.times do |time_entry_count| - time_entry = TimeEntry.create( - project: project, - user: user, - work_package: work_package, - spent_on: Date.today + time_entry_count, - activity: time_entry_activities.sample, - hours: time_entry_count - ) - work_package.time_entries << time_entry - end - end - - def add_attachments(work_package) - 3.times do |_attachment_count| - file = OpenProject::Files.create_uploaded_file(name: Faker::Lorem.words(8).join(' ')) - attachment = Attachment.new( - container: work_package, - author: user, - file: file - ) - attachment.save! - - work_package.attachments << attachment - end - end - - def add_custom_values(work_package) - project.work_package_custom_fields.each do |custom_field| - work_package.type.custom_fields << custom_field if !work_package.type.custom_fields.include?(custom_field) - work_package.custom_values << CustomValue.new(custom_field: custom_field, - value: Faker::Lorem.words(8).join(' ')) - end - - work_package.type.save! - work_package.save! - end - - def make_changes(work_package) - 20.times do - print '.' - work_package.reload - - work_package.status = statuses.sample if rand(99).even? - work_package.subject = Faker::Lorem.words(8).join(' ') if rand(99).even? - work_package.description = Faker::Lorem.paragraph(5, true, 3) if rand(99).even? - work_package.type = types.sample if rand(99).even? - - work_package.time_entries.each do |t| - t.spent_on = Date.today + rand(100) if rand(99).even? - t.activity = time_entry_activities.sample if rand(99).even? - t.hours = rand(10) if rand(99).even? - end - - work_package.reload - - attachments = work_package.attachments.select { |_a| rand(999) < 10 } - work_package.attachments = work_package.attachments - attachments - - work_package.reload - - work_package.custom_values.each do |cv| - cv.value = Faker::Code.isbn if rand(99).even? - end - - work_package.save! - end - end - - # Using the attribute values passed in or random onesbb - def new_work_package(subject: nil, status: nil, type: nil, start_date: nil, due_date: nil) - WorkPackage.create!( - project: project, - author: user, - subject: subject || Faker::Lorem.words(8).join(' '), - status: status || statuses.sample, - type: type || types.sample, - start_date: start_date || s = Date.today - (25 - rand(50)).days, - due_date: due_date || s + (1 + rand(120)).days - ) - end - def calculate_start_date(days_ahead) monday = Date.today.monday days_ahead > 0 ? monday + days_ahead : monday diff --git a/app/seeders/demo_data_seeder.rb b/app/seeders/demo_data_seeder.rb index a47aa77f8378..98ba29e614e9 100644 --- a/app/seeders/demo_data_seeder.rb +++ b/app/seeders/demo_data_seeder.rb @@ -30,21 +30,16 @@ def self.seed! project = DemoData::ProjectSeeder.seed! DemoData::CustomFieldSeeder.seed!(project) - DemoData::BoardSeeder.seed!(project) - DemoData::WikiSeeder.new(project).seed! random: false - DemoData::WorkPackageSeeder.new(project).seed! random: false - DemoData::NewsSeeder.seed!(project) + DemoData::WikiSeeder.seed!(project) + DemoData::WorkPackageSeeder.new(project).seed! puts "\n\n" puts ' ###############################' puts ' # Demo data seeding....done #' puts ' ###############################' + puts " # %02d %-22s #" % [1, 'project created.'] puts " # %02d %-22s #" % [WorkPackage.where(project_id: project.id).count, 'issues created.'] - puts " # %02d %-22s #" % [Message.joins(:board).where(boards: { project_id: project.id }).count, 'messages created.'] - puts " # %02d %-22s #" % [News.where(project_id: project.id).count, 'news created.'] - puts " # %02d %-22s #" % [WikiContent.joins(page: [:wiki]).where('wikis.project_id = ?', project.id).count, 'wiki contents created.'] - puts " # %02d %-22s #" % [TimeEntry.where(project_id: project.id).count, 'time entries created.'] - puts " # %02d %-22s #" % [Changeset.joins(:repository).where(repositories: { project_id: project.id }).count, 'changesets created.'] + puts " # %02d %-22s #" % [WikiContent.joins(page: [:wiki]).where('wikis.project_id = ?', project.id).count, 'wiki created.'] puts " ###############################\n\n" end end diff --git a/app/seeders/demo_data/board_seeder.rb b/app/seeders/random_data/board_seeder.rb similarity index 99% rename from app/seeders/demo_data/board_seeder.rb rename to app/seeders/random_data/board_seeder.rb index f619dabe06d1..161ee014e60e 100644 --- a/app/seeders/demo_data/board_seeder.rb +++ b/app/seeders/random_data/board_seeder.rb @@ -25,7 +25,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # See doc/COPYRIGHT.rdoc for more details. -module DemoData +module RandomData class BoardSeeder def self.seed!(project) user = User.admin.first diff --git a/app/seeders/demo_data/news_seeder.rb b/app/seeders/random_data/news_seeder.rb similarity index 97% rename from app/seeders/demo_data/news_seeder.rb rename to app/seeders/random_data/news_seeder.rb index 216ce60637eb..0d310dff66c9 100644 --- a/app/seeders/demo_data/news_seeder.rb +++ b/app/seeders/random_data/news_seeder.rb @@ -25,14 +25,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # See doc/COPYRIGHT.rdoc for more details. -module DemoData +module RandomData class NewsSeeder - def self.seed!(project) user = User.admin.first - ## create some news - puts '' print ' ↳ Creating news' @@ -45,7 +42,6 @@ def self.seed!(project) description: Faker::Lorem.paragraph(5, true, 3) ## create some journal entries - rand(5).times do news.reload @@ -57,6 +53,5 @@ def self.seed!(project) end end end - end end diff --git a/app/seeders/random_data/wiki_seeder.rb b/app/seeders/random_data/wiki_seeder.rb new file mode 100644 index 000000000000..ac00f2867ca3 --- /dev/null +++ b/app/seeders/random_data/wiki_seeder.rb @@ -0,0 +1,62 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +module RandomData + class WikiSeeder + def self.seed!(project) + user = User.admin.first + + puts '' + print ' ↳ Creating wikis' + + rand(5).times do + print '.' + wiki_page = WikiPage.create( + wiki: project.wiki, + title: Faker::Lorem.words(5).join(' ') + ) + + ## create some wiki contents + rand(5).times do + print '.' + wiki_content = WikiContent.create( + page: wiki_page, + author: user, + text: Faker::Lorem.paragraph(5, true, 3) + ) + + ## create some journal entries + rand(5).times do + wiki_content.reload + wiki_content.text = Faker::Lorem.paragraph(5, true, 3) if rand(99).even? + wiki_content.save! + end + end + end + end + end +end diff --git a/app/seeders/random_data/work_package_seeder.rb b/app/seeders/random_data/work_package_seeder.rb new file mode 100644 index 000000000000..09c5d88d046c --- /dev/null +++ b/app/seeders/random_data/work_package_seeder.rb @@ -0,0 +1,191 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +module RandomData + class WorkPackageSeeder + attr_accessor :project, :user, :statuses, :repository, :time_entry_activities, :types + + def initialize(project) + self.project = project + self.user = User.admin.first + self.statuses = Status.all + self.repository = Repository.first + self.time_entry_activities = TimeEntryActivity.all + self.types = project.types.all.reject(&:is_milestone?) + end + + def seed!(random: true) + puts '' + print ' ↳ Creating work_packages' + + seed_random_work_packages + end + + private + + def seed_random_work_packages + rand(50).times do + print '.' + work_package = WorkPackage.create!( + project: project, + author: user, + subject: Faker::Lorem.words(8).join(' '), + status: statuses.sample, + type: types.sample, + start_date: s = Date.today - (25 - rand(50)).days, + due_date: s + (1 + rand(120)).days + ) + work_package.priority = IssuePriority.all.sample + work_package.description = Faker::Lorem.paragraph(5, true, 3) + work_package.save! + end + + work_package = WorkPackage.first + + if repository + add_changeset(work_package) + end + + add_time_entries(work_package) + add_attachments(work_package) + add_custom_values(work_package) + make_changes(work_package) + end + + def add_changeset(work_package) + 2.times do |changeset_count| + print '.' + changeset = Changeset.create( + repository: repository, + user: user, + revision: work_package.id * 10 + changeset_count, + scmid: work_package.id * 10 + changeset_count, + user: user, + work_packages: [work_package], + committer: Faker::Name.name, + committed_on: Date.today, + comments: Faker::Lorem.words(8).join(' ') + ) + + 5.times do + print '.' + change = Change.create( + action: Faker::Lorem.characters(1), + path: Faker::Internet.url + ) + + changeset.file_changes << change + end + + repository.changesets << changeset + + changeset.save! + + rand(5).times do + print '.' + changeset.reload + + changeset.committer = Faker::Name.name if rand(99).even? + changeset.committed_on = Date.today + rand(999) if rand(99).even? + changeset.comments = Faker::Lorem.words(8).join(' ') if rand(99).even? + + changeset.save! + end + end + end + + def add_time_entries(work_package) + 5.times do |time_entry_count| + time_entry = TimeEntry.create( + project: project, + user: user, + work_package: work_package, + spent_on: Date.today + time_entry_count, + activity: time_entry_activities.sample, + hours: time_entry_count + ) + work_package.time_entries << time_entry + end + end + + def add_attachments(work_package) + 3.times do |_attachment_count| + file = OpenProject::Files.create_uploaded_file(name: Faker::Lorem.words(8).join(' ')) + attachment = Attachment.new( + container: work_package, + author: user, + file: file + ) + attachment.save! + + work_package.attachments << attachment + end + end + + def add_custom_values(work_package) + project.work_package_custom_fields.each do |custom_field| + work_package.type.custom_fields << custom_field if !work_package.type.custom_fields.include?(custom_field) + work_package.custom_values << CustomValue.new(custom_field: custom_field, + value: Faker::Lorem.words(8).join(' ')) + end + + work_package.type.save! + work_package.save! + end + + def make_changes(work_package) + 20.times do + print '.' + work_package.reload + + work_package.status = statuses.sample if rand(99).even? + work_package.subject = Faker::Lorem.words(8).join(' ') if rand(99).even? + work_package.description = Faker::Lorem.paragraph(5, true, 3) if rand(99).even? + work_package.type = types.sample if rand(99).even? + + work_package.time_entries.each do |t| + t.spent_on = Date.today + rand(100) if rand(99).even? + t.activity = time_entry_activities.sample if rand(99).even? + t.hours = rand(10) if rand(99).even? + end + + work_package.reload + + attachments = work_package.attachments.select { |_a| rand(999) < 10 } + work_package.attachments = work_package.attachments - attachments + + work_package.reload + + work_package.custom_values.each do |cv| + cv.value = Faker::Code.isbn if rand(99).even? + end + + work_package.save! + end + end + end +end diff --git a/app/seeders/random_data_seeder.rb b/app/seeders/random_data_seeder.rb new file mode 100644 index 000000000000..3d12eda39c8b --- /dev/null +++ b/app/seeders/random_data_seeder.rb @@ -0,0 +1,54 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +class RandomDataSeeder + def self.seed! + puts ' ########################################################' + puts ' # WARNING: THIS DELETES ANY DEMO DATA THAT WAS SEEDED #' + puts ' ########################################################' + + project = DemoData::ProjectSeeder.seed! + + DemoData::CustomFieldSeeder.seed!(project) + RandomData::BoardSeeder.seed!(project) + RandomData::NewsSeeder.seed!(project) + RandomData::WikiSeeder.seed!(project) + RandomData::WorkPackageSeeder.new(project).seed! + + puts "\n\n" + puts ' ################################' + puts ' # Random seeding....done #' + puts ' ################################' + puts " # %02d %-23s #" % [WorkPackage.where(project_id: project.id).count, 'issues created.'] + puts " # %02d %-23s #" % [Message.joins(:board).where(boards: { project_id: project.id }).count, 'messages created.'] + puts " # %02d %-23s #" % [News.where(project_id: project.id).count, 'news created.'] + puts " # %02d %-23s #" % [WikiContent.joins(page: [:wiki]).where('wikis.project_id = ?', project.id).count, 'wiki contents created.'] + puts " # %02d %-23s #" % [TimeEntry.where(project_id: project.id).count, 'time entries created.'] + puts " # %02d %-23s #" % [Changeset.joins(:repository).where(repositories: { project_id: project.id }).count, 'changesets created.'] + puts " ################################\n\n" + end +end diff --git a/db/production.rb b/db/production.rb new file mode 100644 index 000000000000..219ca24099f2 --- /dev/null +++ b/db/production.rb @@ -0,0 +1 @@ +DemoDataSeeder.seed! diff --git a/lib/tasks/demo_data.rake b/lib/tasks/random_data.rake similarity index 73% rename from lib/tasks/demo_data.rake rename to lib/tasks/random_data.rake index e74411eee82b..a06f72e65a44 100644 --- a/lib/tasks/demo_data.rake +++ b/lib/tasks/random_data.rake @@ -26,9 +26,22 @@ # See doc/COPYRIGHT.rdoc for more details. #++ -namespace 'demo_data' do - desc 'Seed the database with usefull demo data for onboarding users' - task :seed do - puts 'Seeded the database.' +namespace :random_data do + desc 'seeds the data base wth random data' + task seed: :environment do + + puts '*** Seeding basic data' + BasicDataSeeder.seed! + + puts '*** Seeding admin user' + AdminUserSeeder.seed! + + puts '*** Seeding demo data' + RandomDataSeeder.seed! + + ::Rails::Engine.subclasses.map(&:instance).each do |engine| + puts "*** Loading #{engine.engine_name} seed data" + engine.load_seed + end end end