Updating AI plugin fails

AI-generated summary

Users reported an issue where updating Discourse with the AI plugin installed causes a database error: PG::UndefinedColumn: ERROR: column llm_models.url does not exist. This occurs when upgrading from a version lacking migration 20240514171609 to a newer commit (8d5f901 or later). The root cause is that plugin.rb calls LlmModel.enable_or_disable_srv_llm! during db:migrate, which fails if the url column doesn’t yet exist — crashing the migration before it can complete. RGJ noted that core catches PG::UndefinedTable but not PG::UndefinedColumn, explaining why the issue only arises on updates, not fresh installs. Three potential fixes were proposed: conditionally skip the call during migrations, rescue the error, or extend core to catch PG::UndefinedColumn. Roman acknowledged the report and pushed a fix in post 5. RGJ confirmed the fix works perfectly. The issue is now resolved.

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

Problem:

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).

Reason:

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)
    or
  2. change the code to LlmModel.enable_or_disable_srv_llm! rescue nil (so it does not crash)
    or
  3. change the bootstrapping code in core to catch PG::UndefinedColumn in addition to PG::UndefinedTable (so this and future similar issues are caught)

@Roman

6 Likes

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

7 Likes

Pushed a fix here:

https://github.com/discourse/discourse-ai/pull/708

Let me know if you still have problems, please.

5 Likes

Works like a charm! Thank you :slight_smile:

3 Likes