From 08c6a7679d8f74e5b63df167c179388bd9e3e43f Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 20 Jul 2021 12:25:59 +0530 Subject: [PATCH] DEV: allow `dev:populate` rake task in production environment. (#13777) To use this rake task in production `ALLOW_DEV_POPULATE` environment variable should be set to "1". --- Gemfile | 8 ++++++++ lib/discourse.rb | 4 ++++ lib/discourse_dev/record.rb | 4 ++++ lib/freedom_patches/translate_accelerator.rb | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 80d264469e9..efd3fd5c34f 100644 --- a/Gemfile +++ b/Gemfile @@ -175,8 +175,16 @@ group :development do gem 'better_errors', platform: :mri, require: !!ENV['BETTER_ERRORS'] gem 'binding_of_caller' gem 'yaml-lint' +end + +if ENV["ALLOW_DEV_POPULATE"] == "1" gem 'discourse_dev_assets' gem 'faker', "~> 2.16" +else + group :development do + gem 'discourse_dev_assets' + gem 'faker', "~> 2.16" + end end # this is an optional gem, it provides a high performance replacement diff --git a/lib/discourse.rb b/lib/discourse.rb index dd6b65ee575..1d203658f0d 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -1003,6 +1003,10 @@ module Discourse headers['Access-Control-Allow-Methods'] = CDN_REQUEST_METHODS.join(", ") headers end + + def self.allow_dev_populate? + Rails.env.development? || ENV["ALLOW_DEV_POPULATE"] == "1" + end end # rubocop:enable Style/GlobalVars diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb index b8b07d25ff6..6a57189bf12 100644 --- a/lib/discourse_dev/record.rb +++ b/lib/discourse_dev/record.rb @@ -30,6 +30,10 @@ module DiscourseDev end def populate!(ignore_current_count: false) + unless Discourse.allow_dev_populate? + raise 'To run this rake task in a production site, set the value of `ALLOW_DEV_POPULATE` environment variable to "1"' + end + unless ignore_current_count if current_count >= @count puts "Already have #{current_count} #{type} records" diff --git a/lib/freedom_patches/translate_accelerator.rb b/lib/freedom_patches/translate_accelerator.rb index 2bc4cac1e8d..7bf8988d422 100644 --- a/lib/freedom_patches/translate_accelerator.rb +++ b/lib/freedom_patches/translate_accelerator.rb @@ -55,7 +55,7 @@ module I18n # load it I18n.backend.load_translations(I18n.load_path.grep(/\.#{Regexp.escape locale}\.yml$/)) - if Rails.env.development? + if Discourse.allow_dev_populate? I18n.backend.load_translations(I18n.load_path.grep(/.*faker.*\/#{Regexp.escape locale}\.yml$/)) I18n.backend.load_translations(I18n.load_path.grep(/.*faker.*\/#{Regexp.escape locale}\/.*\.yml$/)) end