discourse/lib/tasks/annotate.rake
David Taylor 993f94d54c
DEV: Load all bundled plugins when running annotate:clean (#39978)
Core models often pick up associations and columns from bundled plugins
(e.g. `qa_vote_count` from `discourse-post-voting`, locale-detection
indexes from `discourse-ai`), and the schema blocks at the bottom of
`app/models/*.rb` should reflect those.

`annotate:clean` now defaults `LOAD_PLUGINS` to the bundled plugin list
(`script/list_bundled_plugins`) — the result is the same regardless of
which extra plugins happen to be checked out under `plugins/`. Setting
`LOAD_PLUGINS` explicitly still overrides.

Includes the resulting one-off annotation regen.
2026-05-13 17:19:15 +01:00

64 lines
2.6 KiB
Ruby
Vendored

# frozen_string_literal: true
# Runs annotaterb in a TemporaryDb so the seed topics that
# `annotate:ensure_all_indexes` creates don't leak into the persistent test DB.
def annotate_in_temp_db(load_plugins:, annotaterb_args: [])
env = { "RAILS_ENV" => "test", "LOAD_PLUGINS" => load_plugins }
db = TemporaryDb.new
db.start
db.with_env do
system(env, "bin/rails", "db:migrate", exception: true)
system(env, "bin/rails", "annotate:ensure_all_indexes", exception: true)
system(env, "bin/annotaterb", "models", "--force", *annotaterb_args, exception: true)
end
ensure
db&.stop
db&.remove
end
desc "ensure the asynchronously-created post_search_data index is present"
task "annotate" => :environment do |task, args|
system("bin/annotaterb models", exception: true)
STDERR.puts "Annotate executed successfully"
non_core_plugins =
Dir["plugins/*"].filter { |plugin_path| `git check-ignore #{plugin_path}`.present? }
if non_core_plugins.length > 0
STDERR.puts "Warning: you have non-core plugins installed which may affect the annotations"
STDERR.puts "For core annotations, consider running `bin/rails annotate:clean`"
end
end
desc "ensure the asynchronously-created post_search_data index is present"
task "annotate:ensure_all_indexes" => :environment do |task, args|
# One of the indexes on post_search_data is created by a sidekiq job
# We need to do some acrobatics to create it on-demand
SeedData::Topics.with_default_locale.create
SiteSetting.search_enable_recent_regular_posts_offset_size = 1
Jobs::CreateRecentPostSearchIndexes.new.execute([])
end
desc "regenerate core model annotations using a temporary database"
task "annotate:clean" => :environment do |task, args|
load_plugins = ENV["LOAD_PLUGINS"].presence || bundled_plugins_list
model_dir = ENV["MODEL_DIR"].presence || "app/models"
annotate_in_temp_db(load_plugins: load_plugins, annotaterb_args: ["--model-dir", model_dir])
STDERR.puts "Annotate executed successfully"
end
def bundled_plugins_list
require "open3"
output, status = Open3.capture2("script/list_bundled_plugins")
raise "script/list_bundled_plugins failed (#{status.exitstatus})" unless status.success?
output.split("\n").map { |p| File.basename(p.strip) }.reject(&:empty?).join(",")
end
desc "regenerate plugin model annotations using a temporary database"
task "annotate:clean:plugins", [:plugin] => :environment do |task, args|
annotaterb_args = []
if args[:plugin].present?
annotaterb_args.push("--model-dir", "plugins/#{args[:plugin]}/app/models")
end
annotate_in_temp_db(load_plugins: "1", annotaterb_args: annotaterb_args)
STDERR.puts "Annotate executed successfully"
end