# Updating AI plugin fails

**URL:** <https://meta.discourse.org/t/updating-ai-plugin-fails/315346>\
**Category:** Bug\
**Tags:** ai, fixed\
**Created:** [July 7, 2024, 10:04pm UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346 "2024-07-07T22:04:52Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [July 7, 2024, 10:04pm UTC](https://meta.discourse.org/t/updating-ai-plugin-fails/315346/1 "2024-07-07T22:04:52Z")

</div>

Distilling this from [Latest update won't build due to various reasons](https://meta.discourse.org/t/latest-update-wont-build-due-to-ai-plugin/314875) 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

```plaintext
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](https://github.com/discourse/discourse-ai/commit/8d5f901a6748d8a15fd227ef739f3c2f543fb376) 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](https://github.com/discourse/discourse/blob/52a047a8beff58f3c9df57fd5b426e66636efd95/lib/plugin/instance.rb#L578-L583) `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](https://github.com/discourse/discourse/blob/52a047a8beff58f3c9df57fd5b426e66636efd95/lib/plugin/instance.rb#L578-L583) to catch `PG::UndefinedColumn` in addition to `PG::UndefinedTable` (so this _and_ future similar issues are caught)

@Roman

---

_[View the full topic](https://meta.discourse.org/t/updating-ai-plugin-fails/315346)._
