Skip to content

A platform for community discussion. Free, open, simple.

FIX: Rescue `ActiveRecord::ReadOnlyError` when baking theme field (#29776)

Alan Guo Xiang Tan6bf0ac730f20
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index 15cb067894a..0659b447b90 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -475,6 +475,9 @@ class ThemeField < ActiveRecord::Base
error: error,
)
end
+ rescue ActiveRecord::ReadOnlyError
+ # Just noop if ActiveRecord is preventing writes for now. In an ideal world, this method will not be called in GET
+ # requests.
end
def compile_scss(prepended_scss = nil)
diff --git a/lib/scheduler/defer.rb b/lib/scheduler/defer.rb
index 995403093a0..9b0d97f8311 100644
--- a/lib/scheduler/defer.rb
+++ b/lib/scheduler/defer.rb
@@ -6,6 +6,8 @@ module Scheduler
DEFAULT_TIMEOUT = 90
STATS_CACHE_SIZE = 100
+ attr_reader :async
+
def initialize
@async = !Rails.env.test?
@queue =
diff --git a/spec/integration/activerecord_preventing_writes_spec.rb b/spec/integration/activerecord_preventing_writes_spec.rb
new file mode 100644
index 00000000000..16bddce4a3b
--- /dev/null
+++ b/spec/integration/activerecord_preventing_writes_spec.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+RSpec.describe "When ActiveRecord is preventing writes" do
+ before do
+ @original_async = Scheduler::Defer.async
+ Scheduler::Defer.async = true
+ end
+
+ after { Scheduler::Defer.async = @original_async }
+
+ it "should not result in an error response when there is a theme field that needs to be baked" do
+ theme_field =
+ Fabricate(
+ :theme_field,
+ type_id: ThemeField.types[:html],
+ target_id: Theme.targets[:common],
+ name: "head_tag",
+ value: <<~HTML,
+ <script type="text/discourse-plugin" version="0.1">
+ console.log(settings.uploads.imajee);
+ </script>
+ HTML
+ )
+
+ SiteSetting.default_theme_id = theme_field.theme_id
+
+ ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role, prevent_writes: true) do
+ get "/latest"
+
+ expect(request.env[:resolved_theme_id]).to eq(theme_field.theme_id)
+ expect(response.status).to eq(200)
+ end
+ end
+end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index 863bb198741..1133ee99496 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -713,6 +713,7 @@ RSpec.configure do |config|
unfreeze_time
ActionMailer::Base.deliveries.clear
Discourse.redis.flushdb
+ Scheduler::Defer.do_all_work
end
config.after(:each, type: :system) do |example|