Issues migrating test database

Thanks, @Mittineague!

After a while, that made sense. I even wrote a spec, but even before I added my spec (and when I reverted to before I added any code), specs fail because:

An error occurred while loading ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb.
Failure/Error: Group.find_by(id: id)

ActiveRecord::StatementInvalid:
  PG::UndefinedTable: ERROR:  relation "groups" does not exist
  LINE 8:  WHERE a.attrelid = '"groups"'::regclass
                              ^
# (eval):24:in `async_exec'
# (eval):24:in `async_exec'
# ./app/models/group.rb:529:in `lookup_group'
# ./app/models/group.rb:513:in `block in ensure_automatic_groups!'
# ./app/models/group.rb:512:in `each_key'
# ./app/models/group.rb:512:in `ensure_automatic_groups!'
# (eval):3:in `block (2 levels) in run_file'
# ./spec/rails_helper.rb:79:in `<top (required)>'
# ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb:3:in `require'
# ./plugins/discourse-prometheus/spec/middleware/metrics_spec.rb:3:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::UndefinedTable:
#   ERROR:  relation "groups" does not exist
#   LINE 8:  WHERE a.attrelid = '"groups"'::regclass
#                               ^
#   (eval):24:in `async_exec'
No examples found.

That seems to hit for nearly every spec in the site. I think that I properly did git reset --hard upstream/master to get to what should be working and then deleted and re-created the database but I’m still getting that error.

I admit I’m not the best at reading error messages. I looked at what file I thought they were referencing but didn’t see any “group(s)” line.

If this is your own, it might be something I struggle with some. ie. When pluralization means more than one or when it is convention

That is, you may be thinking “groups” when the syntax wants “group” without the “s”.

Similar, sometimes code wants specific case eg. “Group” vs. “group”.

I would like to say I know, but I don’t. I suggest “monkey see, monkey do” coding unless anyone that knows better chimes in.

I didn’t do anything with groups (group doesn’t appear on my plugin) and those specs fail all over discourse. I’ll do one more clone of the master to see if somehow my fork. Thanks again!

Looks like you need to migrate your test database

RAILS_ENV=test bin/rake db:migrate

If that doesn’t work, scrap it and start again

RAILS_ENV=test bin/rake db:drop
RAILS_ENV=test bin/rake db:create
RAILS_ENV=test bin/rake db:migrate

Make double sure you put RAILS_ENV=test, otherwise you’ll accidentally delete your development database (I managed to do that by accident this week :man_facepalming:)

1 Like

Maybe that’s my problem! Maybe I dropped development and not test. Thanks. Ooh, and I need to install a fresh development environment in my laptop before I head to the airport.

1 Like

I just added a check for pending migrations at the beginning of the spec suite, so it should be easier to identify this kind of issue in future

https://github.com/discourse/discourse/commit/f47909a2b6cfec50e3fdd5027b284621c5f0a6cc

3 Likes

Thanks, David! I saw that it prints a warning now. I don’t quite understand why it was so difficult to get the test database migrated, but after I did a bunch of destroy,create,migrate and then the RAILS_ENV=test bin/rake db:migrate you recommended, I managed to see that my spec is broken! Now, I can fix it and I’m on my way to learning how to write specs! Thanks again for your push, @Mittineague.

1 Like

The context behind my commit is that I found a bug where

RAILS_ENV=test rake db:drop db:create db:migrate

doesn’t include post_deploy migrations. You have to run db:migrate by itself.

We’ll definitely get that fixed, but in the meantime this warning should reduce the frustration!

4 Likes

Has that been fixed? It looks like a ./bin/rake db:drop db:create drops and creates the test database, but it seems that ./bin/rake db:migrate doesn’t also do the migrations on test.