DEV: Allow plugins to exclude seed data

DEV: Allow plugins to exclude seed data

This allows plugins to specify if they would like to filter out any seed data files from running during migrations.

diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb
index ab382a4..7383849 100644
--- a/lib/discourse_plugin_registry.rb
+++ b/lib/discourse_plugin_registry.rb
@@ -66,6 +66,7 @@ class DiscoursePluginRegistry
   define_register :seed_path_builders, Set
   define_register :vendored_pretty_text, Set
   define_register :vendored_core_pretty_text, Set
+  define_register :seedfu_filter, Set
 
   define_filtered_register :staff_user_custom_fields
   define_filtered_register :public_user_custom_fields
@@ -198,6 +199,10 @@ class DiscoursePluginRegistry
     result.uniq
   end
 
+  def self.register_seedfu_filter(filter = nil)
+    self.seedfu_filter << filter
+  end
+
   VENDORED_CORE_PRETTY_TEXT_MAP = {
     "moment.js" => "vendor/assets/javascripts/moment.js",
     "moment-timezone.js" => "vendor/assets/javascripts/moment-timezone-with-data.js"
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 11f866f..302076e 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -78,6 +78,10 @@ class Plugin::Instance
     @seed_data ||= HashWithIndifferentAccess.new({})
   end
 
+  def seed_fu_filter(filter = nil)
+    @seed_fu_filter = filter
+  end
+
   def self.find_all(parent_path)
     [].tap { |plugins|
       # also follows symlinks - http://stackoverflow.com/q/357754
@@ -415,6 +419,10 @@ class Plugin::Instance
     SeedFu.fixture_paths.concat(paths)
   end
 
+  def register_seedfu_filter(filter = nil)
+    DiscoursePluginRegistry.register_seedfu_filter(filter)
+  end
+
   def listen_for(event_name)
     return unless self.respond_to?(event_name)
     DiscourseEvent.on(event_name, &self.method(event_name))
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index 5a6c67c..7b6c928 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -180,7 +180,12 @@ task 'db:migrate' => ['load_config', 'environment', 'set_locale'] do |_, args|
   end
 
   SeedFu.quiet = true
-  SeedFu.seed(DiscoursePluginRegistry.seed_paths)
+
+  # Allows a plugin to exclude any specified seed data files from running
+  filter = DiscoursePluginRegistry.seedfu_filter.any? ?
+    /^(?!.*(#{DiscoursePluginRegistry.seedfu_filter.to_a.join("|")})).*$/ : nil
+
+  SeedFu.seed(DiscoursePluginRegistry.seed_paths, filter)
 
   if !Discourse.skip_post_deployment_migrations? && ENV['SKIP_OPTIMIZE_ICONS'] != '1'
     SiteIconManager.ensure_optimized!

GitHub sha: a2713578

@oblakeerickson I’m not sure if this is right according to the docs

rake db:seed_fu FILTER=users,articles – Only run seed files with a filename matching the FILTER

It doesn’t exclude the specific files but only runs those files instead.

Ah I see it does

I suppose the regexp will exclude the pattern?

1 Like

Yep the regex will exclude the pattern. I think the docs are a little out of date because I found out about using a regex from this issue:

1 Like