【问题标题】:Rails 3, rake db:test:prepare doesn't work when using a postgres SchemaRails 3,rake db:test:prepare 在使用 postgres 模式时不起作用
【发布时间】:2023-03-24 06:04:02
【问题描述】:
我有一个 Rails 3.2.1 项目,我正在尝试运行我的规范。
当我跑步时:
耙子
rake 中止! PG::Error: 错误:参数值无效
“search_path”:“example” 详细信息:模式“example”不存在:
将 search_path 设置为示例
据我所知,rake db:test:prepare 会删除测试数据库,然后尝试从 schema.rb 重新创建它。问题是,database.yml 有一行
schema_search_path:示例
所以当它尝试重新连接时,它会因上述错误而失败。
我想我的问题是,我怎样才能让 rake db:test:prepare 也设置 schema_path?
【问题讨论】:
标签:
ruby-on-rails-3
rspec
rake
【解决方案1】:
您应该在 config/database.yml 中设置 schema_search_path
例如
development:
adapter: postgresql
encoding: unicode
database: test
pool: 5
username: user
password: password
#host: localhost
#port: 5432
# Schema search path. The server defaults to $user,public
schema_search_path: example
【解决方案2】:
一种解决方案是向创建模式的 db:create 任务添加一些代码。把它放在一个 rake 任务中,例如 'APP_ROOT/lib/tasks/db_create_schema.rake'
namespace :db do
task :create do
config = Rails.configuration.database_configuration[Rails.env].merge!({'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("CREATE SCHEMA schema_name")
end
end
https://gist.github.com/4280172