Related: Db:drop, create & migrate behavior with RAILS_ENV=development - #2 by taylorthurlow - A May Of WTFs - Ruby on Rails Discussions
Not sure if we have explored this before, but Rails can automatically maintain test databse schema with ActiveRecord::Migration.maintain_test_schema!
(method definition, in rails/test_help
, in latest rspec/rails
helper generator)
Here is a diff to incorporate it into Discourse’s spec/rails_helper
:
https://github.com/xrav3nz/discourse/commit/90ac78ad3b64c07ddaf86ccc883c87c0dd727348
However, it fails on my Docker-based development setup:
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: must be owner of database discourse_test
This is because maintain_test_schema!
utilizes db:test:prepare
, and tries to drop the test database first, to which (I believe) the discourse
user on Docker-based setup does not have access.
Could someone with a native dev setup could try it out? If it works, maybe the change is worth considering?
8 Likes
I applied that diff and dropped by databases then ran
bin/rails db:create
bin/rails db:migrate
then chose a random spec
▶ bundle exec rspec spec/requests/permalinks_controller_spec.rb
An error occurred while loading ./spec/requests/permalinks_controller_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
^
# ./app/models/group.rb:532:in `lookup_group'
# ./app/models/group.rb:516:in `block in ensure_automatic_groups!'
# ./app/models/group.rb:515:in `each_key'
# ./app/models/group.rb:515:in `ensure_automatic_groups!'
# (eval):3:in `block (2 levels) in run_file'
# ./spec/rails_helper.rb:79:in `<top (required)>'
# ./spec/requests/permalinks_controller_spec.rb:3:in `require'
# ./spec/requests/permalinks_controller_spec.rb:3:in `<top (required)>'
# ------------------
# --- Caused by: ---
# PG::UndefinedTable:
# ERROR: relation "groups" does not exist
# LINE 8: WHERE a.attrelid = '"groups"'::regclass
# ^
# ./app/models/group.rb:532:in `lookup_group'
No examples found.
Finished in 0.00003 seconds (files took 1.6 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
I never got into config.before('suite')
3 Likes