rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.08k stars 161 forks source link

Add schema plugins #389

Closed flash-gordon closed 7 years ago

flash-gordon commented 7 years ago

I've found this would be a really nice addition, this will reduce the boilerplate on defining schemas significantly and will allow to add some cool stuff to rom-sql. Specifically I want to have auto-index attributes in schemas because you normally want to have all the FKs to be indexed.

solnic commented 7 years ago

This is cool indeed. I wanted to add it too. So +1

flash-gordon commented 7 years ago

@solnic glad to see you :) I'm gonna make it work with relation finalizers today/tomorrow so that it will be possible to load plugins globally, then this will be ready for going in master. Gotta say the plugin infrastructure is a bit messy and I guess will require some refactoring before 4.0.

solnic commented 7 years ago

Gotta say the plugin infrastructure is a bit messy and I guess will require some refactoring before 4.0.

Yeah it should be improved in 4.0

flash-gordon commented 7 years ago

@solnic so... it turned to be not that easy, I can't extend the schema DSL because it is used before finalization, at the class level. Do you know any reason why we can't move it to the finalization step?

solnic commented 7 years ago

@flash-gordon schema needs to be established already because it is needed in view DSL, so you can't postpone it until finalization :/

flash-gordon commented 7 years ago

we infer schemas at the finalization step thus viewDSL cannot depend on it without having corner cases where view syntax breaks for unclear reasons

solnic commented 7 years ago

@flash-gordon hmm, right, view schemas are finalized here. I guess if you move things around so that schema dsl can be extended in the right moment, things will continue to work.

flash-gordon commented 7 years ago

we infer schemas at the finalization step thus viewDSL cannot depend on it without having corner cases where view syntax breaks for unclear reasons

I actually saw those errors, now just things clicked in place :)

flash-gordon commented 7 years ago

@solnic phew! I store an unresolved schema in @schema_proc of a relation class. Overall, postponing schema initialization wasn't hard, in Relation.view I've changed schema.project(*args[1]) to -> _ { schema.project(*args[1]) }

There is a bunch of related changes in rom-sql and I'll push them later today. While I was working on this I realized how hardly we need some sort of bootstrapping pipeline with events, callbacks and things like that. It's very tough to learn how every single hook in rom works because they works differently.

I wish it would be

ROM::Bootstrap.before(:schema_load, relation: :users) do |container, schema_dsl: , **other_stuff|
  schema_dsl.extend(Timestamps::SchemaDSL)
end

^just a sketch, but wdyt?

solnic commented 7 years ago

@flash-gordon sounds good!

re bootstraping, it crossed my mind many times, but it's such a complex part of rom that I never had the energy to tackle it. There was also a long learning curve to understand what's involved in this process, and now we're in a much better position to come up with a nicer approach. We can try to do it in 4.0.

solnic commented 7 years ago

Well, this is pretty awesome. I've left a couple of comments (small things to address).

flash-gordon commented 7 years ago

@solnic done. I actually removed setting a schema in the inherited hook because it's not defined when the hook gets called.

solnic commented 7 years ago

Looks like rom 4.0 will be more awesome than we thought ;)