OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add db:seed:only task to run a single seeder
Christophe Bliard18382e756c3a
diff --git a/app/seeders/root_seeder.rb b/app/seeders/root_seeder.rbindex d8baf468235..fd12837e166 100644--- a/app/seeders/root_seeder.rb+++ b/app/seeders/root_seeder.rb@@ -50,21 +50,21 @@ class RootSeeder < Seederreset_active_record!set_locale! doprepare_seed! do- do_seed!+ ActiveRecord::Base.transaction do+ block_given? ? (yield self) : do_seed!+ endendendenddef do_seed!- ActiveRecord::Base.transaction do- # Basic data needs be seeded before anything else.- seed_basic_data- seed_admin_user- seed_demo_data- seed_development_data if seed_development_data?- seed_plugins_data- seed_env_data- end+ # Basic data needs be seeded before anything else.+ seed_basic_data+ seed_admin_user+ seed_demo_data+ seed_development_data if seed_development_data?+ seed_plugins_data+ seed_env_dataenddef seed_development_data?diff --git a/lib/tasks/seed.rake b/lib/tasks/seed.rakenew file mode 100644index 00000000000..72f8d5cf21c--- /dev/null+++ b/lib/tasks/seed.rake@@ -0,0 +1,54 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++namespace :db do+ namespace :seed do+ desc "Run a single seeder"+ task :only, [:seeder_class_name] => [:environment] do |_task, args|+ seeder_class_name = args.fetch(:seeder_class_name, nil)+ if seeder_class_name.nil?+ raise "Specify a seeder class name 'rake db:seed:only[Some::ClassName]'"+ end++ RootSeeder.new.seed_data! do |root_seeder|+ seeder_class = seeder_class_name.constantize+ if seeder_class.ancestors.exclude?(Seeder)+ raise ArgumentError, "#{seeder_class_name} is not a seeder"+ end++ puts "Running #{seeder_class} seeder"+ seeder = seeder_class.new(root_seeder.seed_data)+ seeder.seed!+ rescue NameError+ raise ArgumentError, "No seeder with class name #{seeder_class_name}"+ end+ end+ end+enddiff --git a/spec/tasks/seed_spec.rb b/spec/tasks/seed_spec.rbnew file mode 100644index 00000000000..d4bd8a3be4a--- /dev/null+++ b/spec/tasks/seed_spec.rb@@ -0,0 +1,66 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# 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 COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'++RSpec.describe 'db:seed' do # rubocop:disable RSpec/DescribeClass+ include_context 'rake'+ let(:task_path) { 'lib/tasks/seed' }++ describe 'db:seed:only' do+ before do+ allow($stdout).to receive(:puts)+ end++ it 'fails if seeder is not specified' do+ expect { subject.invoke }+ .to raise_error "Specify a seeder class name 'rake db:seed:only[Some::ClassName]'"+ end++ it 'runs the specified seeder' do+ subject.invoke('BasicData::WorkPackageRoleSeeder')+ expect(WorkPackageRole.count).to eq 3+ end++ it 'displays an error if the given seeder class name does not exist' do+ expect { subject.invoke('BasicData::BadSeeder') }+ .to raise_error ArgumentError, 'No seeder with class name BasicData::BadSeeder'+ end++ it 'displays an error if the given class name is not a seeder' do+ expect { subject.invoke('Queries::Queries::QueryQuery') }+ .to raise_error ArgumentError, 'Queries::Queries::QueryQuery is not a seeder'+ end++ it 'does not work for all seeders because of missing references' do+ expect { subject.invoke('DevelopmentData::ProjectsSeeder') }+ .to raise_error ActiveRecord::RecordNotFound+ expect(Project.count).to eq 0+ end+ end+end