관련 링크: Db:drop, create & migrate behavior with RAILS_ENV=development - #2 by taylorthurlow - A May Of WTFs - Ruby on Rails Discussions
이전에 이 주제를 다뤄본 적이 있는지 확실하지 않지만, Rails는 ActiveRecord::Migration.maintain_test_schema!를 사용하여 테스트 데이터베이스 스키마를 자동으로 유지할 수 있습니다. (메서드 정의, rails/test_help 내, 최신 rspec/rails 헬퍼 생성기 내)
Discourse의 spec/rails_helper에 이를 통합하기 위한 diff는 다음과 같습니다:
그러나, 제 Docker 기반 개발 환경에서는 실패합니다:
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: must be owner of database discourse_test
이것은 maintain_test_schema!가 db:test:prepare를 사용하며, 먼저 테스트 데이터베이스를 삭제하려고 시도하기 때문입니다. (제 생각에는) Docker 기반 설정에서 discourse 사용자는 해당 데이터베이스에 접근 권한이 없습니다.
네이티브 개발 환경을 가진 누군가가 이 기능을 테스트해 볼 수 있을까요? 작동한다면, 이 변경 사항을 고려해 볼 가치가 있을 것입니다.
해당 diff를 적용하고 데이터베이스를 삭제한 후 다음을 실행했습니다.
bin/rails db:create
bin/rails db:migrate
그 후 무작위로 하나의 스펙을 선택하여 실행했습니다.
▶ 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
config.before('suite') 블록에는 진입하지 못했습니다.