Updating AI plugin fails

Distilling this from Latest update won't build due to AI plugin since that topic is about three different issues at the moment.


When trying to update Discourse with the AI plugin installed, the update yields an error with

PG::UndefinedColumn: ERROR:  column llm_models.url does not exist
LINE 1: SELECT "llm_models".* FROM "llm_models" WHERE "llm_models"

and renders Discourse unusable until the AI plugin is removed.

When does this happen:

When updating an instance with the AI plugin
From a version that does not have the migration 20240514171609 yet
To commit 8d5f901 or newer

(My main question was why this only happens when updating an already-installed plugin and not when the plugin is newly installed. The answer is that core catches PG::UndefinedTable exceptions but not PG:UndefinedColumn. If the plugin is already installed then the exception is the latter, if the plugin is brand new then it is the former).


plugin.rb calls LlmModel.enable_or_disable_srv_llm! which depends on the column llm_models.url being present. But if migration 20240514171609 (which adds that column) has not been applied yet, it crashes.

The problem is that db:migrate also runs this code, which crashes the rake task before the migration can be applied.

Suggested workarounds / fixes:

  1. change the code to LlmModel.enable_or_disable_srv_llm! if Discourse.running_in_rack? (so db:migrate does not call it)
  2. change the code to LlmModel.enable_or_disable_srv_llm! rescue nil (so it does not crash)
  3. change the bootstrapping code in core to catch PG::UndefinedColumn in addition to PG::UndefinedTable (so this and future similar issues are caught)



Thanks for letting us know, @RGJ. I’ll take care of this and update once it’s fixed.


Pushed a fix here:

Let me know if you still have problems, please.


Works like a charm! Thank you :slight_smile: