The `$rake db:*` commands and initializing a Discourse instance

First off, thank you to the devs for creating such a great app!

I am hoping I might be able to get something clarified that has me confused. I was under the impression that the proper way to use $ rake db:* commands in Rails 6 for initialization was by doing $ rake db:prepare but discourse won’t initialize properly if I do $ rake db:prepare by itself. And from the discussions on Meta/here; I see that prepare only seems to be used for initializing test data and maybe somewhere in the background tasks that are run.

Could someone please help me understand if $ rake db:migrate is the only command I need to initialize discourse’s database for db migrations or initializations and why this is the case?

I think you need to db:create first, then migrate.

1 Like

Like Michael said, the database has to exist. I regularly do

   rake db:drop db:create db:migrate

to reset to a fresh database (when I’ve forgotten to save a backup of a fresh install!).

1 Like

I think the build process must default to doing db:create because unless I’m missing some functionality in my instance the instance appears to be working with only assets precompile (or something) and migrate.

After some digging it appears this is more information on the answer I was looking for:

Discourse’s rake tasks are overriding the rails db:migrate. I believe this can be considered unorthodox by Rails apps, e.g. this PR: Add db:prepare rake task. by robertomiranda · Pull Request #35768 · rails/rails · GitHub. Looking at what db:prepare does, it directly accesses ActiveRecord and does the expected DB setup but it does not call the other necessary individual db:* tasks one might expect it to because it overrides parts that are expected to be their from the upstream.

And a side note: The way I currently have discourse set up does not allow the discourse db user to ever have escalated privileges so having to do things like db:drop and db:create after building seems it will require a slight config change from what I implemented.