自动迁移测试数据库架构

相关讨论: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: 必须拥有数据库 discourse_test 的所有权

这是因为 maintain_test_schema! 使用了 db:test:prepare,并尝试先删除测试数据库,而(我认为)基于 Docker 的 setup 中的 discourse 用户没有该权限。

是否有使用原生开发环境的朋友可以尝试一下?如果可行,或许值得考虑这一更改?

8 个赞

我应用了该差异并删除了数据库,然后运行了
bin/rails db:create
bin/rails db:migrate

接着随机选择了一个规范测试

▶ bundle exec rspec spec/requests/permalinks_controller_spec.rb 
加载 ./spec/requests/permalinks_controller_spec.rb 时发生错误。
Failure/Error: Group.find_by(id: id)

ActiveRecord::StatementInvalid:
  PG::UndefinedTable: 错误:关系 "groups" 不存在
  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)>'
# ------------------
# --- 原因:---
# PG::UndefinedTable:
#   错误:关系 "groups" 不存在
#   LINE 8:  WHERE a.attrelid = '"groups"'::regclass
#                               ^
#   ./app/models/group.rb:532:in `lookup_group'
未找到任何示例。


完成时间:0.00003 秒(文件加载耗时 1.6 秒)
0 个示例,0 个失败,1 个错误发生在示例之外

我从未进入 config.before('suite')

3 个赞