Skip to content
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

Refactor Talk indexing into a Talk::Index record. #540

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Dec 22, 2024

This does take some extra work since Active Record can't detect rowid is the primary key.

But with some awareness around that, it's pretty smooth sailing and we can entirely remove a lot of the custom SQL with standard Active Record.

This does take some extra work since Active Record can't detect `rowid` is the primary key.

But with some awareness around that, it's pretty smooth sailing and we can entirely remove a lot of the custom SQL with standard Active Record.
end

after_create_commit :create_in_index
after_update_commit :update_in_index
after_destroy_commit :remove_from_index
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get this for free now via dependent: :destroy!

@kaspth
Copy link
Contributor Author

kaspth commented Dec 22, 2024

I don't get what's going on in the tests.

In development the query looks correct and finds the talk:

rubyvideo(dev)> Talk.ft_search "Hotwire Cookbook"
  Talk Load (2.3ms)  SELECT "talks".* FROM "talks" INNER JOIN "talks_search_index" ON "talks_search_index"."rowid" = "talks"."id" WHERE (talks_search_index match 'Hotwire* Cookbook*') /* loading for pp */ LIMIT 11 /*application='Rubyvideo'*/

But in tests the query looks different and doesn't find the talk:

SELECT "talks".* FROM "talks" INNER JOIN "talks_search_index" ON "talks_search_index"."rowid" = "talks"."id" WHERE (talks_search_index match ?)  [[nil, "Hotwire* Cookbook*"]]

The [[nil, "Hotwire* Cookbook*"]] structure seems to indicate a bind parameter and potentially a prepared statement, but we don't seem to have that turned on/off anywhere. Maybe it's a Rails default somehow?

Swapping to where("#{table_name} match :query", query:) also doesn't work. Hm, I'm confused.

@kaspth
Copy link
Contributor Author

kaspth commented Dec 22, 2024

Ok, if I put prepared_statements: false in database.yml, I get the same query as in development:

SELECT "talks".* FROM "talks" INNER JOIN "talks_search_index" ON "talks_search_index"."rowid" = "talks"."id" WHERE (talks_search_index match 'Hotwire* Cookbook*')

But it still doesn't find the book.

@kaspth
Copy link
Contributor Author

kaspth commented Dec 23, 2024

Ah, something seems to be up when we try to insert the record, hm. It's missing the rowid here.

INSERT INTO "talks_search_index" ("title", "summary", "speaker_names") VALUES ('yo', '', 'Yauhen Karatkou') RETURNING "rowid" /*application='Rubyvideo'*/

@kaspth
Copy link
Contributor Author

kaspth commented Dec 23, 2024

Ok, wow, maybe this is more trouble than it's worth. We have to drop back into raw SQL to get Active Record to put in the rowid when inserting.

@adrienpoly
Copy link
Owner

I like how it all feels really ActiveRecord.

@kaspth
Copy link
Contributor Author

kaspth commented Jan 4, 2025

I'm hoping to get back to this, because I'd like to wrap it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants