1

我需要创建视图,发现使用 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 的方法

那么我错过了什么?抱歉,我知道这将是一件很明显的事情,但我仍然不太了解宝石是如何集成到系统中的。

提前谢谢了

4

2 回答 2

1

我已将 gem 的来源更改为:

git://github.com/centresource/rails_sql_views

这已经解决了问题 - 迁移现在工作正常

于 2012-07-16T16:14:57.527 回答
0

当您将 gem 添加到您的 Gemfile 时,您需要运行bundle install.

这将查看您的 Gemfile 是否已更改,如果已更改,例如添加一个新 gem,它将转到源,拉入 gem 代码,然后将其包含在您的应用程序中。

要查看项目有哪些 gem,只需查看从最后一个包生成的 Gemfile.lock 文件。

于 2012-07-16T14:28:20.450 回答