How can I use ActiveRecord to set up an FTS virtual table in SQLite3?
I'm using AR with Rails 3.0.9 and SQLite 3.6.22 (Ubuntu Lucid Lynx). I've added an FTS3 virtual table to a database that did not previously use FTS. I don't know how to use AR calls (e.g. create_table) to create the virtual table I want. So I create and populate the table using raw SQL. I began by using a migration. That worked well at first, but it turned out schema.rb was incomplete. Somehow AR could not dump the FTS tables into the schema. I have not been able to solve this problem.
That means, for example, that when I run my RSpec test suite, the FTS table is not in the database. I've worked around this by adding a helper method to populate the FTS table (which only takes a few seconds) and adde开发者_如何学Pythond it in before blocks for relevant RSpec examples. I also have to have a separate DB seed task to build the FTS table after the seed.
The current arrangement actually works all right, since it takes so little time to build the virtual table on the fly. But I am certain I ought to be able to build the FTS table using AR calls directly, which I imagine will avoid the other problem (tables not dumped to schema.rb). Is there any documentation for setting up FTS with SQLite through AR? I have not been able to find any. Thanks.
You need to set the schema format to SQL in config/application.rb
with the config.active_record.schema_format
key. There's really no way for AR to dump database specific stuff like that into schema.rb (short of Connection#execute
)
The crazy awesome alternative would be to patch the migrations to store those execute statements somewhere and put them in schema.rb as execute
calls.
I eventually worked around the problem and kind of forgot about the project long before actually seeing these responses. But I've started doing some more development recently, and this was an obstacle again, so I tried to apply the suggestion above, setting config.active_record.schema_format to :sql instead of :ruby. Unfortunately, life is just never that easy:
schema.sql not creating even after setting schema_format = :sql
Apparently this just never worked with Rails 3. So I've started converting the project to Rails 4. I still have a fair bit of ActiveRecord agita to sort through, but it appears that the :sql setting works for schema_format in AR 4.0.
精彩评论