From 034bcd10a859f736900082023d3f0867a3655d3a Mon Sep 17 00:00:00 2001 From: Martin Czuchra Date: Thu, 29 Aug 2013 15:30:47 +0200 Subject: [PATCH] Import seeds and Gemfile from feature/rails3. git checkout opf/feature/rails3 -- db/seeds.rb db/seeds Gemfile --- Gemfile | 9 +- db/seeds.rb | 36 ++-- db/seeds/all.rb | 69 ++++++++ db/seeds/development.rb | 362 ++++++++++++++++++++++++++++++++++++++++ db/seeds/production.rb | 1 + db/seeds/test.rb | 1 + 6 files changed, 465 insertions(+), 13 deletions(-) create mode 100644 db/seeds/all.rb create mode 100644 db/seeds/development.rb create mode 100644 db/seeds/production.rb create mode 100644 db/seeds/test.rb diff --git a/Gemfile b/Gemfile index 4ae1d7d8bc..ed1b7000da 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,8 @@ gem 'htmldiff' gem 'execjs' gem 'therubyracer' +gem "date_validator" + # will need to be removed once we are on rails4 as it will be part of the rails4 core gem 'strong_parameters' @@ -83,6 +85,7 @@ group :test do # why in Gemfile? see: https://github.com/guard/guard-test gem 'ruby-prof' gem 'simplecov', ">= 0.8.pre" + gem "shoulda-matchers" end group :ldap do @@ -94,19 +97,19 @@ group :openid do end group :development do - gem 'rails-footnotes', '>= 3.7.5.rc4' - gem 'bullet' gem 'letter_opener', '~> 1.0.0' gem 'pry-rails' gem 'pry-stack_explorer' gem 'pry-rescue' - gem 'pry-debugger' + gem 'pry-byebug', :platforms => :mri_20 + gem 'pry-debugger', :platforms => :mri_19 gem 'pry-doc' gem 'rails-dev-tweaks', '~> 0.6.1' gem 'guard-rspec' gem 'guard-cucumber' gem 'rb-fsevent', :group => :test gem 'thin' + gem 'faker' end group :tools do diff --git a/db/seeds.rb b/db/seeds.rb index e2e43a4b5a..2db26e4b9e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -17,14 +17,30 @@ # # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) # Mayor.create(:name => 'Emanuel', :city => cities.first) +# +# loads environment-specific seeds. The assumed directory structure in db/ is like this: +#|___seeds +#| |___all.rb +#| |___development.rb +#| |___staging.rb +#| |___production.rb +#|___seeds.rb + +# clear some schema caches and column information. +ActiveRecord::Base.descendants.each do |klass| + klass.connection.schema_cache.clear! + klass.reset_column_information +end + +['all', Rails.env].each do |seed| + seed_file = "#{Rails.root}/db/seeds/#{seed}.rb" + if File.exists?(seed_file) + puts "*** Loading #{seed} seed data" + require seed_file + end +end -Type.connection.schema_cache.clear! -Type.reset_column_information -Type.create!(name: 'none', - color_id: '#000000', - is_standard: true, - is_default: true, - is_in_chlog: true, - is_in_roadmap: true, - in_aggregation: true, - is_milestone: false) +Rails::Application::Railties.engines.each do |engine| + puts "*** Loading #{engine.engine_name} seed data" + engine.load_seed +end diff --git a/db/seeds/all.rb b/db/seeds/all.rb new file mode 100644 index 0000000000..5a96cf32da --- /dev/null +++ b/db/seeds/all.rb @@ -0,0 +1,69 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# +# Copyright (C) 2012-2013 the OpenProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +# add seeds here, that need to be available in all environments + +# Magic code. This useless use of a constant in void context actually +# has autoloader side-effects that allow the circular dependency (User-> +# Principal-> Project-> User) to exist well into the future. Hooray for +# not fixing stuff! +Project + +PlanningElementTypeColor.ms_project_colors.map(&:save) +default_color = PlanningElementTypeColor.find_by_name('pjSilver') + +Type.find_or_create_by_is_standard(true, name: 'none', + position: 0, + color_id: default_color.id, + is_default: true, + is_in_chlog: true, + is_in_roadmap: true, + in_aggregation: true, + is_milestone: false) + +if Role.find_by_builtin(Role::BUILTIN_NON_MEMBER).nil? + role = Role.new + + role.name = 'Non member' + role.position = 0 + role.builtin = Role::BUILTIN_NON_MEMBER + role.save! +end + +if Role.find_by_builtin(Role::BUILTIN_ANONYMOUS).nil? + role = Role.new + + role.name = 'Anonymous' + role.position = 1 + role.builtin = Role::BUILTIN_ANONYMOUS + role.save! +end + +if User.admin.empty? + user = User.new + + old_password_length = Setting.password_min_length + Setting.password_min_length = 0 + + user.admin = true + user.login = "admin" + user.password = "admin" + user.firstname = "OpenProject" + user.lastname = "Admin" + user.mail = "admin@example.net" + user.mail_notification = User::USER_MAIL_OPTION_NON.first + user.language = "en" + user.status = 1 + user.save! + + Setting.password_min_length = old_password_length +end diff --git a/db/seeds/development.rb b/db/seeds/development.rb new file mode 100644 index 0000000000..d7e971b730 --- /dev/null +++ b/db/seeds/development.rb @@ -0,0 +1,362 @@ +# set some sensible defaults: +include Redmine::I18n + +# sensible shortcut: Create the default data in english +begin + set_language_if_valid('en') + Redmine::DefaultData::Loader.load(current_language) + puts "Default configuration data loaded." +rescue Redmine::DefaultData::DataAlreadyLoaded + puts "Redmine Default-Data already loaded" +end + +# Careful: The seeding recreates the seeded project before it runs, so any changes on the seeded project will be lost. +puts "Creating seeded project..." +if delete_me=Project.find_by_identifier("seeded_project") + delete_me.destroy +end + +project = Project.create(name: "Seeded Project", + identifier: "seeded_project", + description: Faker::Lorem.paragraph(5), + types: Type.all, + is_public: true + ) + +# this will fail rather miserably, when there are no statuses present +statuses = IssueStatus.all +# don't bother with milestones, too difficult to handle all cases +types = project.types.all.reject{|type| type.is_milestone?} + +project.enabled_module_names += ["timelines"] + +# create some custom fields and add them to the project +3.times do |count| + cf = WorkPackageCustomField.create!(name: Faker::Lorem.words(2).join(" "), + regexp: "", + is_required: false, + min_length: false, + default_value: "", + max_length: false, + editable: true, + possible_values: "", + visible: true, + field_format: "text") + + project.work_package_custom_fields << cf +end + +# create a default timeline that shows all our planning elements +timeline = Timeline.create() +timeline.project = project +timeline.name = "Sample Timeline" +timeline.options.merge!({zoom_factor: ["4"]}) +timeline.save + +board = Board.create! project: project, + name: Faker::Lorem.words(2).join(" "), + description: Faker::Lorem.paragraph(5).slice(0, 255) + +wiki = Wiki.create project: project, start_page: "Seed" + +time_entry_activities = [] + +5.times do + time_entry_activity = TimeEntryActivity.create name: Faker::Lorem.words(2).join(" ") + + time_entry_activity.save! + time_entry_activities << time_entry_activity +end + +Setting.enabled_scm = Setting.enabled_scm.dup << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem') + +repository = Repository::Filesystem.create! project: project, + url: Faker::Internet.url() + + +print "Creating objects for..." +20.times do |count| + login = "#{Faker::Name.first_name}#{rand(10000)}" + + puts + print "...for user number #{count} (#{login})" + + user = User.find_by_login(login) + + unless user + user = User.new() + user.tap do |u| + u.login = login + u.firstname = Faker::Name.first_name + u.lastname = Faker::Name.last_name + u.mail = Faker::Internet.email + u.save + end + end + + ## let every user create some issues... + + puts "" + print "......create issues" + + rand(10).times do + print "." + Issue.create!(project: project, + author: user, + status: statuses.sample, + subject: Faker::Lorem.words(8).join(" "), + description: Faker::Lorem.paragraph(5, true, 3), + type: types.sample + ) + + end + + ## extend user's last issue + created_issues = WorkPackage.find :all, conditions: { author_id: user.id } + + if !created_issues.empty? + issue = created_issues.last + + ## add changesets + + 2.times do |changeset_count| + print "." + changeset = Changeset.create(repository: repository, + user: user, + revision: issue.id * 10 + changeset_count, + scmid: issue.id * 10 + changeset_count, + user: user, + work_packages: [issue], + 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.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 + + ## add time entries + + 5.times do |time_entry_count| + issue.time_entries << TimeEntry.create(project: project, + user: user, + work_package: issue, + spent_on: Date.today + time_entry_count, + activity: time_entry_activities.sample, + hours: time_entry_count) + end + + ## add attachments + + 3.times do |attachment_count| + + attachment = Attachment.new(container: issue, + author: user, + filename: Faker::Lorem.words(8).join(" "), + disk_filename: Faker::Lorem.words(8).join("_")) + attachment.save! + + issue.attachments << attachment + + end + + ## add custom values + + project.work_package_custom_fields.each do |custom_field| + issue.type.custom_fields << custom_field if !issue.type.custom_fields.include?(custom_field) + issue.custom_values << CustomValue.new(custom_field: custom_field, value: Faker::Lorem.words(8).join(" ")) + end + + issue.type.save! + issue.save! + + ## create some changes + + 20.times do + print "." + issue.reload + + issue.status = statuses.sample if rand(99).even? + issue.subject = Faker::Lorem.words(8).join(" ") if rand(99).even? + issue.description = Faker::Lorem.paragraph(5, true,3) if rand(99).even? + issue.type = types.sample if rand(99).even? + + issue.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 + + issue.reload + + attachments = issue.attachments.select { |a| rand(999) < 10 } + issue.attachments = issue.attachments - attachments + + issue.reload + + issue.custom_values.each do |cv| + cv.value = Faker::Code.isbn if rand(99).even? + end + + issue.save! + end + end + + puts "" + print "......create planning elements" + + rand(30).times do + print "." + start_date = rand(90).days.from_now + due_date = start_date + 5.day + rand(30).days + child_element = nil + + + element = PlanningElement.create!(project: project, + author: user, + status: statuses.sample, + subject: Faker::Lorem.words(5).join(" "), + description: Faker::Lorem.paragraph(5, true,3), + type: types.sample, + start_date: start_date, + due_date: due_date) + rand(5).times do + print "." + sub_start_date = rand(start_date..due_date) + sub_due_date = rand(sub_start_date..due_date) + child_element = PlanningElement.create!(project: project, + parent: element, + author: user, + status: statuses.sample, + subject: Faker::Lorem.words(5).join(" "), + description: Faker::Lorem.paragraph(5, true,3), + type: types.sample, + start_date: sub_start_date, + due_date: sub_due_date) + end + + [element, child_element].compact.each do |e| + 2.times do + print "." + e.reload + + e.status = statuses.sample if rand(99).even? + e.subject = Faker::Lorem.words(8).join(" ") if rand(99).even? + e.description = Faker::Lorem.paragraph(5, true,3) if rand(99).even? + e.type = types.sample if rand(99).even? + + e.save! + end + end + + end + + ## create some messages + + puts "" + print "......create messages" + + rand(30).times do + print "." + message = Message.create board: board, + author: user, + subject: Faker::Lorem.words(5).join(" "), + content: Faker::Lorem.paragraph(5, true, 3) + + rand(5).times do + print "." + Message.create board: board, + author: user, + subject: message.subject, + content: Faker::Lorem.paragraph(5, true, 3), + parent: message + end + end + + ## create some news + + puts "" + print "......create news" + + rand(30).times do + print "." + news = News.create project: project, + author: user, + title: Faker::Lorem.characters(60), + summary: Faker::Lorem.paragraph(1, true, 3), + description: Faker::Lorem.paragraph(5, true, 3) + + ## create some journal entries + + rand(5).times do + news.reload + + news.title = Faker::Lorem.words(5).join(" ").slice(0, 60) if rand(99).even? + news.summary = Faker::Lorem.paragraph(1, true, 3) if rand(99).even? + news.description = Faker::Lorem.paragraph(5, true, 3) if rand(99).even? + + news.save! + end + end + + ## create some wiki pages + + puts "" + print "......create wikis" + + rand(5).times do + print "." + wiki_page = WikiPage.create wiki: 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 + +print "done." +puts "\n" +puts "#{PlanningElement.where(:project_id => project.id).count} planning_elements created." +puts "#{Issue.where(:project_id => project.id).count} issues created." +puts "#{Message.joins(:board).where(boards: { :project_id => project.id }).count} messages created." +puts "#{News.where(:project_id => project.id).count} news created." +puts "#{WikiContent.joins(page: [ :wiki ]).where("wikis.project_id = ?", project.id).count} wiki contents created." +puts "#{TimeEntry.where(:project_id => project.id).count} time entries created." +puts "#{Changeset.joins(:repository).where(repositories: { :project_id => project.id }).count} changesets created." +puts "Creating seeded project...done." diff --git a/db/seeds/production.rb b/db/seeds/production.rb new file mode 100644 index 0000000000..702a5eae89 --- /dev/null +++ b/db/seeds/production.rb @@ -0,0 +1 @@ +# add seeds specific for the production-environment here \ No newline at end of file diff --git a/db/seeds/test.rb b/db/seeds/test.rb new file mode 100644 index 0000000000..9681c90338 --- /dev/null +++ b/db/seeds/test.rb @@ -0,0 +1 @@ +# add seeds specific for the test-environment here \ No newline at end of file