我需要创建视图,发现使用SQL创建视图会导致运行测试时遇到问题。
在我的开发系统(即sqlite3)上,我将数据库迁移回滚到创建视图之前。
我从git://github.com/unleashed/rails_sql_views添加了rails_sql_views gem。
我修改了视图的迁移,现在它已经
了def up
# dividing by 100 for percentages and 1000 for kW, hence 100000
create_view :view_sub_power_ratings,
"select
((s.power_off * p.percent_off + s.standby * p.percent_standby + s.idle * p.percent_idle + s.normal * p.percent_normal + s.maximum * p.percent_max) * p.working_days * 24 +
(s.power_off * percent_off_nw + s.standby * percent_standby_nw + s.idle * p.percent_idle_nw + s.normal * p.percent_normal_nw + s.maximum * percent_max_nw) * (365 - p.working_days) * 24) /100000
as power_usage,
p.subscription_id,
s.device_id
from sub_category_params p
inner join devices d on d.device_category_id = p.device_category_id
inner join device_power_summaries s on s.device_id = d.id"
# dividing by 100 for percentage accuracy, 100 for percent used, and 1000 for kW, hence 10000000
create_view :view_sub_power_ratings_variations,
"select
(s.standby * s.standby_accuracy * p.percent_standby + s.idle * s.idle_accuracy *p.percent_idle + s.normal * s.normal_accuracy * p.percent_normal +
s.maximum * s.maximum_accuracy * p.percent_max + s.power_off * s.power_off_accuracy * p.percent_off) * 24 * p.working_days/ 10000000 as variation_wd,
(s.standby * s.standby_accuracy * p.percent_standby_nw + s.idle * s.idle_accuracy *p.percent_idle_nw + s.normal * s.normal_accuracy * p.percent_normal_nw +
s.maximum * s.maximum_accuracy * p.percent_max_nw + s.power_off * s.power_off_accuracy * p.percent_off_nw) * 24 * (365-p.working_days)/ 10000000 as variation_nw,
p.subscription_id,
s.device_id
from sub_category_params p
inner join devices d on d.device_category_id = p.device_category_id
inner join device_power_summaries s on s.device_id = d.id"
create_view :view_sub_power_maximums,
"select
r.power_usage + (v.variation_nw + v.variation_wd) as maximum_power_usage,
r.power_usage - (v.variation_nw + v.variation_wd) as minimum_power_usage,
v.subscription_id,
v.device_id
from sub_category_params p
inner join devices d on d.device_category_id = p.device_category_id
inner join device_power_summaries s on s.device_id = d.id
inner join view_sub_power_ratings r on d.id = r.device_id and p.subscription_id = r.subscription_id
inner join view_sub_power_ratings_variations v on d.id = v.device_id and p.subscription_id = v.subscription_id"
end
def down
drop_view :view_sub_power_ratings
drop_view :view_sub_power_ratings_variations
drop_view :view_sub_power_maximums
end
然后我再次进行迁移,一切都很好。创建视图,create_view位于schema.rb中。结果!
但是,在部署到我的登台服务器时,我收到了迁移错误。这是在MySQL2上。
这是追踪
** Execute db:schema:dump
rake aborted!
undefined method `base_tables' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xb876f1c>
/usr/local/rvm/gems/ruby-1.9.3-p125/bundler/gems/rails_sql_views-9d781715bcab/lib/rails_sql_views/schema_dumper.rb:98:in `tables_with_views_excluded'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/schema_dumper.rb:27:in `dump'
/usr/local/rvm/gems/ruby-1.9.3-p125/bundler/gems/rails_sql_views-9d781715bcab/lib/rails_sql_views/schema_dumper.rb:27:in `dump_with_views'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/schema_dumper.rb:21:in `dump'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/railties/databases.rake:354:in `block (4 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/railties/databases.rake:352:in `open'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/railties/databases.rake:352:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/railties /databases.rake:161:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/railties/databases.rake:156:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p125@global/bin/rake:19:in `<main>'
Tasks: TOP => db:schema:dump
我的Gemfile有
gem 'rails_sql_views', :git => 'git://github.com/unleashed/rails_sql_views', require: 'rails_sql_views'
捆绑安装运行正常:
** [out :: myserver.com]
** [out :: myserver.com] Using rails_sql_views (0.8.0.1.unleashed) from git://github.com/unleashed/rails_sql_views (at master)
** [out :: myserver.com]
** [out :: myserver.com] Using sass (3.1.15)
** [out :: myserver.com] * mysql2-0.3.11.gem
** [out :: myserver.com] Removing outdated .gem files from vendor/cache
** [out :: myserver.com] * rails_sql_views-0.8.0.gem
** [out :: myserver.com] Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
command finished in 4018ms
我看到有些人报告类似,并且解释说MySQL2没有适配器。但它位于lib / rails_sql_views / connection_adapters目录中:mysql2_adapter.rb
我看了一下base_tables的方法
那我错过了什么?对不起,我知道它会变得非常明显,但我仍然不太了解宝石是如何整合到系统中的。
非常感谢提前
答案 0 :(得分:1)
我已将gem的来源更改为:
GIT中://github.com/centresource/rails_sql_views
这解决了问题 - 迁移现在正常运行
答案 1 :(得分:0)
当您向Gemfile添加gem时,您需要运行bundle install
。
这将看你的Gemfile是否已经改变,如果有,例如添加一个新的gem,它将转到源代码,拉入gem代码然后将其包含在你的应用程序中。
要查看项目中的gem,请查看从最后一个bundle生成的Gemfile.lock文件。