-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support for Sequel? #87
Comments
Could not you implement a clas SequelLocator < BaseLocator
def locate(gid)
gid.model_class.with_pk!(gid.model_id)
end
private
def find_records(model_class, ids, options)
model_class.where(model_class.primary_key => ids).tap do |result|
if !options[:ignore_missing] && result.count < ids.size
fail Sequel::NoMatchingRow
end
end.all
end
end
GlobalID::Locator.use :my_sequel_app, SequelLocaltor.new This way you don't need to monkey patch anything in GlobalID. |
Yes-ish. I was looking at that as a possibility, but what I really want is a way to replace the default locator rather than having to specify with I actually want to do a lot more for Sequel support than just the locator because Sequel makes it easier to have composite or non-sequence primary keys, and I would like to make GlobalID::Identification do the right thing. It’s not as important to me at the moment as the lookup functionality. In general, how open is the Rails team to supporting non-AR lookup, or getting a modification in to allow for class/module-based dispatch, not just app-name based dispatch? Something like this, maybe: class GlobalID::Locator
class BaseLocator
def locate(gid)
if class_locator = Locator.class_locators.find { |c| gid.model_class <= c }
class_locator.locate(gid)
else
gid.model_class.find gid.model_id
end
end
end
def self.use_class_locator(klass)
@class_locators << klass
end
def self.class_locators
@class_locators ||= []
end
end
class SequelLocator
def locate
gid.model_class.with_pk!(gid.model_id)
end
end
GlobalID::Locator.use_class_locator SequelLocator.new |
globalid/lib/global_id/railtie.rb Line 17 in 14f5a57
I think our existing app locators can do what you ask just fine and I don't think we need to overload the standard locators for Sequel support (or add class/module locating). Happy to hear other arguments, but I'll close for now ❤️ |
We could duck type to a
|
@jeremy sure that could work for the singular case and we could have a |
@kaspth Having looked at the code, no, You could say that it should fail, because this GlobalID isn’t for this app, but that isn’t the way that GlobalID is currently written. If the Railtie instead did a default I sort of like the idea of duck-typing to |
I was assuming you had only one app, but true this is a different case. I'm curious, why would you receive different apps? Are you building an app that could potentially locate Global IDs with unknown apps?
That's a lot of Sequel inside baseball there. Can you try some pseudo code for the For instance it seems like Sequel deals with the singular and many cases differently than Active Record's |
OK. Let me try to break this down, because some of what I mentioned was hypothetical, but this discussion has (to me) pointed out a potential problem for GlobalID that I alluded to. There’s a couple of things to look at here, and I can explain them in non-Sequel-specific terms (but I’ll also answer your questions re: Sequel). There are probably an issue or two that should come out of this, so this might get a bit long. I apologize in advance. The Problem of the Default LocatorFirst, the way that the default locator is implemented may be a vulnerability. The implementation of def locator_for(gid)
@locators.fetch(normalize_app(gid.app)) { DEFAULT_LOCATOR }
end So if
This can be mitigated by removing the GlobalID::Locator.use GlobalID.app, UnscopedLocator.new That way, GlobalID only resolves for apps that are either the default app “detected” through the Railtie or that are explicitly configured. This change alone would be fairly useful as I could create a GlobalID::Locator.use GlobalID.app, SequelGlobalIDLocator.new As long as the This fixes both the default locator vulnerability and the problem of Sequel not implementing (I do have multiple apps, but when I get around to doing all of the locators in general, each app will implement its own, but there will be an external version so we can request data across the wire from the app that owns it. It’s the right thing to do, but I mentioned this as an example of something that could be wrong, although I think the problem is real.) Alternate Approach:
|
Sequel doesn’t follow the same protocol as ActiveRecord, even when ActiveModel compliant (or so it seems). I’ve come up with what looks like the right level of monkey-patching for this, and can turn this into a real patch (with tests) for GlobalID if there is interest. The changes are only necessary in
GlobalID::Locator::BaseLocator
for#locate
and#find_records
.The only other change is including GlobalID::Identification into Sequel::Model, but in my basic testing, these changes create the same result as AR-backed GlobalID, mod the appropriate exceptions.
Also opened as TalentBox/sequel-rails#111.
The text was updated successfully, but these errors were encountered: