Running an RSpec test suite against a TimescaleDB database with Rails 4.2

99封情书 提交于 2020-06-01 05:57:28

问题


I have a Rails 4.2.11.1 application that I am trying to use with a TimescaleDB database.

I've solved the majority of the issues already (using the composite_primary_keys gem to get around Timescale's restriction that unique indexes should always include the table's timestamp column).

This has my application fully functional, but my test suite fails whenever I try to write to the Timescale table, with the following error:

PG::FeatureNotSupported: ERROR:  invalid INSERT on the root table of hypertable "events"
HINT:  Make sure the TimescaleDB extension has been preloaded.

As far as I can tell, Rails is not enabling the extension within the test database; this applies whichever schema dumping mode I'm using (either :sql or :ruby). Connecting to the database manually with bundle exec rails dbconsole, I can see that the extension is enabled.

I'm not sure whether this is a bug with my code, with Timescale, or with Rails, but it's stopping me from being able to ship this change.

I'm hoping that someone else has come across this scenario before and is able to help!


回答1:


Are you certain that the extension is enabled for your test database?

Try running: RAILS_ENV=test bundle exec rails dbconsole

If you wrote a migration to enable the extension I would have expected that it would run against your test database as well, unless you've specifically run it only against development.




回答2:


I had the same problem on Rails 6.0.2.2 / TimescaleDB 1.7.0 (On Minitest).

The problem was solved, when I switched from sql schema dump back to ruby schema dump.

config.active_record.schema_format = :ruby # (in application.rb)

Apparently, the schema dump of the sql format contained errors/was partial, and preparing from it introduced the error (db:test:clone).

Hope it helps your case.




回答3:


It sounds like the schema dump file is missing is missing the extension enable command,

If config.active_record.schema_format = :ruby then schema.rb will contain enable_extension "timescaledb"

If config.active_record.schema_format = :sql then structure.sql will contain CREATE EXTENSION IF NOT EXISTS timescaledb WITH SCHEMA public;

If these lines exist in your schema dumps, you might have to do a clean restore of the test db schema

# take care with dropping your test db, make sure the database is configured correctly
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:schema:load # RAILS_ENV=test rake db:structure:load if :sql

You should then be able to run your test and the extension will be enabled through the schema restore process above.



来源:https://stackoverflow.com/questions/59141045/running-an-rspec-test-suite-against-a-timescaledb-database-with-rails-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!