-
Notifications
You must be signed in to change notification settings - Fork 22k
Add support for SQLite3 full-text-search and other virtual tables #52354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
3aca6e0 to
7cee5e9
Compare
|
This change is so specific for sqlite. |
7cee5e9 to
e7774e4
Compare
|
Yes this is SQLite only. I took inspiration from the native PostgreSQL enum support that landed in Rails 7.0. Indeed, the problem does not occur if using |
|
I very much like the idea of being able to support sqlite search without having to give up on schema.rb. We've faced this problem with ONCE. @kevinmcconnel, could you have a look whether this would work for that case? Also, @matthewd do you have thoughts on the implementation? |
|
FWIW as someone knowledgeable about the sqlite virtual table implementation, I'm comfortable with this approach from the POV of how rails is interacting with the schema tables. |
e7774e4 to
0635b72
Compare
|
Pinging @kevinmcconnell since your username was misspelled above. |
|
Thanks for the ping @flavorjones! I had missed this earlier. +1 on adding this. As @dhh mentioned, this came up when building ONCE products. We switched to structure.sql to work around it, but I kept meaning to take a stab at adding this functionality so we could switch back. So it's awesome to see someone already has :) I just tried this branch in Campfire to get the app back onto schema.rb, and it worked great. |
activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb
Outdated
Show resolved
Hide resolved
|
@zachasme Can you please squash the commits and force-push? That will also re-kick CI which had a few failures that I don't think are related. |
886f605 to
622277b
Compare
|
@flavorjones sure thing! I've squashed and rebased. |
|
@zachasme Hmm, ok, it seems like there are related tests failing, for example https://buildkite.com/rails/rails/builds/110258#01915c1d-f2a6-4f30-95b5-2ce74ef0a7fc Can you take a look? |
|
Indeed there are! I'll look into it :-) |
622277b to
c4f1203
Compare
|
It looks like some of the Alternatively, when listing tables, I could join on SELECT m.name FROM sqlite_master m
JOIN pragma_table_list p ON m.name = p.name
WHERE m.name <> 'sqlite_sequence'"
AND p.type IN (...)
ORDER BY m.rowidPlease let me know if you think that would be preferable. |
8b4da05 to
d8a7158
Compare
|
@zachasme Made one small comment, but those test changes mostly look OK to me. There are still some failures in the |
d8a7158 to
bc63d28
Compare
|
@flavorjones Many thanks for your help. I believe the test are now passing. |
activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb
Outdated
Show resolved
Hide resolved
02577ac to
ad52362
Compare
c2e8700 to
51d4899
Compare
Previously, adding sqlite3 virtual tables messed up `schema.rb`. Now, virtual tables can safely be added using `create_virtual_table`.
51d4899 to
1ecb91b
Compare
Motivation / Background
As discussed on rubyonrails-core with @flavorjones.
SQLite is a great default database, and even provides a full-text-search module,
fts5. However,fts5is a virtual table module which messes upschema.rb. For example, if you create virtual tablesearchablesin a migration:and run
rails db:migrate, then 6 tables are created, 1virtualand 5 "shadow" tables. This is what the result ofSELECT * FROM pragma_table_list ORDER BY name(relevant part) looks like:While these tables look like regular tables, they should not be dumped in the same way. In
schema.rbthere are error messages for some of them (notably the virtual table) while a few shadow tables are dumped like regular tables:This will put the app into a bad state if you run
rails db:reset, with some shadow tables as regular tables and the virtual table missing entirely:The proper way to dump a virtual table to
schema.rbwould be to ignore the "shadow" tables and only create the virtual table (which in turn will create the shadow tables).Detail
This PR tries to provide general SQLite3 virtual table support using
create_virtual_tableanddrop_virtual_table. Whiledrop_virtual_tableis basicallydrop_table(since dropping a virtual table also drops connected shadow tables), it allows theCommandRecorderto reversedrop_virtual_table.It will dump virtual tables to
schema.rband ignore shadow tables. For example:will append to
schema.rb:Notable changes:
virtual/shadowtables from the regular table dump, I had to usepragma_table_listinstead ofsqlite_masterinActiveRecord::ConnectionAdapters::SQLite3::SchemaStatementsbecause the schema tablesqlite_masterlacks virtual/shadow type information.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]