ビューを作成する必要があり、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のメソッドがあります
それで、私は何が欠けていますか?申し訳ありませんが、非常に明白なことになることはわかっていますが、gem がシステムにどのように統合されているかについてはまだよくわかりません。
よろしくお願いします