-
Notifications
You must be signed in to change notification settings - Fork 11
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
Is it possible to add indexes to views? #17
Comments
Yeah, ability to add indices like |
I see two options here: either pass column names and languages as a hash in a |
I agree that this would be a valuable feature. I am hesitant to add SQL statements to the Another option would be requiring the |
@jasoncodes You could grab anything starting with |
Having thought about this some more, I think my preferred solution here is a new is_view materialized: true, after_create_sql: <<-SQL
CREATE UNIQUE INDEX ON foos(name);
CREATE INDEX ON foos USING gin(to_tsvector('english', content));
SQL This fits in with existing options, is easier to implement, and keeps the If you wish to have the index creation statements in another file, you could use something like How does this sound? |
Personally, I would rather not fracture definition of the view into 3 files: model file, select query and separate indices query for sake of DRYness. That's why I believe keeping everything in sql file is the best option. You could parse #{table_name} in sql file to make things easier for defining indices, like It all boils down to "abstract SQL away" vs. "flexibility and directness of raw SQL" in the end. You can't really eliminate raw sql completely, so why not give users ability to leverage it? Abstracting away CREATE [MATERIALIZED] VIEW [view_name] is nice, but not a deal breaker. Or maybe you can provide dsl a-la what you have now for unique indices, something along the lines of
Problem with DSL approach is of course a need for developer to learn the DSL as well as difficulty covering all use-cases. As I said, full text indices could get quite complex, with sql function calls and different weights of different language indices and so on and so on. |
I am against creating a new DSL for indexes but could see supporting running a block in a migration-like context which would give access to If I were to add support for additional statements in the I am not opposed to also having something like the above but I think I personally would prefer to use an |
I’ve revisited this Issue and discussion and I am thinking adding support for a generic “after view create” ActiveSupport callback is the best way to solve this. A “before view drop” callback might be useful too for anything that won’t automatically drop when the view is dropped. The callbacks would be invoke in the context of the model class and allow you create indexes with We may have to do something extra to ensure |
Is it possible to add indexes to views?
The text was updated successfully, but these errors were encountered: