Unexpected error in Discourse Plugin CI

AI-generated summary

In a GitHub Actions CI workflow for a Discourse plugin, merefield encountered an ActiveRecord::NoDatabaseError when accessing the database via User.count inside an after_initialize block. After feedback from NateDhaliwal, it became clear that directly using ActiveRecord models in initializers violates current best practices — even if it previously worked by luck. merefield confirmed the issue was in their code and acknowledged the need to refactor, moving the logic to a scheduled job instead. NateDhaliwal expressed relief that the fix worked. The discussion highlights a recent tightening of rules around database access in Discourse plugin initializers, emphasizing that such operations should be deferred to jobs or event hooks to avoid CI failures. The plugin CI workflow referenced is based on Discourse’s official guide, though the underlying GitHub Actions template has evolved recently.

UPDATE: @NateDhaliwal rightfully points out my accessing the DB in after_initializer may break the rules - I was just lucky for a long time.

I’m going to make some changes.

I’m getting:

ActiveRecord::NoDatabaseError: We could not find your database: discourse_test. Available database configurations can be found in config/database.yml. (ActiveRecord::NoDatabaseError)
detail
rake aborted!
ActiveRecord::NoDatabaseError: We could not find your database: discourse_test. Available database configurations can be found in config/database.yml. (ActiveRecord::NoDatabaseError)
To resolve this error:
- Did you not create the database, or did you delete it? To create the database, run:
    bin/rails db:create
- Has the database name changed? Verify that config/database.yml contains the correct database name.
/__w/discourse-chatbot/discourse-chatbot/config/initializers/000-pg-connection-debug.rb:14:in `rescue in new_client'
/__w/discourse-chatbot/discourse-chatbot/config/initializers/000-pg-connection-debug.rb:4:in `new_client'

in my github workflow

Has something changed around the standard plugin CI workflow I wasn’t aware of?

I’m still following:

and using v1

There is no code of mine running up to this point (except gem installs) and this PR has in any case just minor config changes.

PS Looks like it’s been evolving recently: History for .github/workflows/discourse-plugin.yml - discourse/.github · GitHub

Not 100% sure, but I think it happens if you use ActiveRecord through things like User, etc openly, e.g. not in on() or in a job but just in after_initialize.

I encountered this too not long ago… IIRC I had to ask AI for this; I moved the code to a scheduled job and it was fine after that.

Apologies, on second look it does seem to be failing on my code, but I am accessing it in after_initialize

Looks like the rules might have changed:

user_count = User.count

doesn’t look legal in an initializer anymore (perhaps it was always bad practice)

thanks for the suggestion! :clap:

I think you are absolutely right that I should move this to a job and run it once. :+1:

No problem, glad you got it working!