Skip to content

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

Look up existing status data and store them as references

Christophe Bliard747c2c0f72e8
diff --git a/app/seeders/basic_data/model_seeder.rb b/app/seeders/basic_data/model_seeder.rb
index 11fb06a8be4..5e784d5e5f7 100644
--- a/app/seeders/basic_data/model_seeder.rb
+++ b/app/seeders/basic_data/model_seeder.rb
@@ -29,8 +29,13 @@
#++
module BasicData
class ModelSeeder < Seeder
+ # The class of the model for creating records.
class_attribute :model_class
+ # The key under which the data is found in the seed file.
class_attribute :seed_data_model_key
+ # The names of the attributes used to lookup an existing model in an already
+ # seeded database. Optional.
+ class_attribute :attribute_names_for_lookups
def seed_data!
model_class.transaction do
@@ -53,6 +58,17 @@ module BasicData
model_class.none?
end
+ def lookup_existing_references
+ return if attribute_names_for_lookups.blank?
+
+ models_data.each do |model_data|
+ lookup_attributes = model_attributes(model_data).slice(*attribute_names_for_lookups)
+ if model = model_class.find_by(lookup_attributes)
+ seed_data.store_reference(model_data['reference'], model)
+ end
+ end
+ end
+
def not_applicable_message
"Skipping #{model_human_name} as there are already some configured"
end
diff --git a/app/seeders/basic_data/status_seeder.rb b/app/seeders/basic_data/status_seeder.rb
index 42aaca52f22..97806c5cd0d 100644
--- a/app/seeders/basic_data/status_seeder.rb
+++ b/app/seeders/basic_data/status_seeder.rb
@@ -29,6 +29,7 @@ module BasicData
class StatusSeeder < ModelSeeder
self.model_class = Status
self.seed_data_model_key = 'statuses'
+ self.attribute_names_for_lookups = %i[name is_closed is_default]
self.needs = [
BasicData::ColorSeeder,
BasicData::ColorSchemeSeeder
diff --git a/spec/seeders/basic_data/status_seeder_spec.rb b/spec/seeders/basic_data/status_seeder_spec.rb
index 5914caa9512..3078b809a23 100644
--- a/spec/seeders/basic_data/status_seeder_spec.rb
+++ b/spec/seeders/basic_data/status_seeder_spec.rb
@@ -94,6 +94,26 @@ RSpec.describe BasicData::StatusSeeder do
created_status = Status.last
expect(seed_data.find_reference(:status_closed)).to eq(created_status)
end
+
+ context 'when seeding a second time' do
+ subject(:second_seeder) { described_class.new(second_seed_data) }
+
+ let(:second_seed_data) { basic_seed_data.merge(Source::SeedData.new(data_hash)) }
+
+ before do
+ second_seeder.seed!
+ end
+
+ it 'registers existing matching statuses as references in the seed data' do
+ # using the first seed data as the expected value
+ expect(second_seed_data.find_reference(:status_closed))
+ .to eq(seed_data.find_reference(:status_closed))
+ expect(second_seed_data.find_reference(:status_new))
+ .to eq(seed_data.find_reference(:status_new))
+ expect(second_seed_data.find_reference(:status_in_progress))
+ .to eq(seed_data.find_reference(:status_in_progress))
+ end
+ end
end
context 'without statuses defined' do