Skip to content

Make AR::Attributes independent of AR::ModelSchema - #38871

Closed
jonathanhefner wants to merge 8 commits into
rails:masterfrom
jonathanhefner:refactor-model_schema-attributes
Closed

jonathanhefner wants to merge 8 commits into
rails:masterfrom
jonathanhefner:refactor-model_schema-attributes

Conversation

@jonathanhefner

@jonathanhefner jonathanhefner commented Apr 3, 2020 •

Copy link
Copy Markdown
Member

This refactor is a step towards porting more modules from Active Record to Active Model. ActiveRecord::Attributes no longer depends on ActiveRecord::ModelSchema. Likewise, ActiveRecord::AttributeDecorators no longer depends on ActiveRecord::ModelSchema, and now depends on ActiveRecord::Attributes instead.

An overview of the changes:

  • Move _default_attributes and attribute_types from ModelSchema to Attributes.
  • Replace ModelSchema::reload_schema_from_cache with Attributes::reset_attributes.
  • Replace calls to Attributes::define_attribute with calls to Attributes::add_attribute_to_attribute_set, which does not mutate global state. (The original method was not removed because it is publicly documented.)
  • Replace ModelSchema::load_schema! with Attributes::add_deferred_to_attribute_set, which does not mutate global state.
  • Define Attributes::_default_attributes as the point of entry for Attributes::add_deferred_to_attribute_set, and ensure neither are part of a circular call tree.
  • Remove the Monitor from ModelSchema::load_schema, which now only calls Attributes::_default_attributes, which itself only writes one piece of global state, atomically.
  • Remove now-redundant calls to ModelSchema::load_schema.

Coincidentally, fixes #38293.

@jonathanhefner

Copy link
Copy Markdown
Member Author

/cc @matthewd since you added the Monitor in #29216. I believe the problem described by #28589 should no longer be possible after these changes, correct?

@jonathanhefner
jonathanhefner force-pushed the refactor-model_schema-attributes branch from f509c0e to 1bfb1a4 Compare April 14, 2020 20:28
@jonathanhefner

Copy link
Copy Markdown
Member Author

@casperisfine Would this PR be of any interest to you? I noticed you had some issues with load_schema that were worked around in #37288.

Also, I know you've done several optimizations to improve application boot performance. I'm curious if removing the Monitor (and two class_attributes) affects any performance characteristics in your benchmark.

@casperisfine

Copy link
Copy Markdown
Contributor

Would this PR be of any interest to you? I noticed you had some issues with load_schema that were worked around in #37288.

Well, the issue I had is that there are several things during attribute definition that requires to access Base#connection, which tries to connect to the database.

The main one being connection.schema_cache, but there's also connection.lookup_cast_type_from_column. From what I understand of your PR, none of that is solved.

TBH I'm having a bit of a hard time to understand this PR (not your fault), so it's hard for me to say wether it goes in what I think is the good direction or not. But I'll continue exploring it.

I'm curious if removing the Monitor (and two class_attributes) affects any performance characteristics in your benchmark.

So while trying to answer this, I realized my benchmark had a blindspot (because of the connection problem explained above). I'll be fixing it and will come back to you once I have more accurate benchmarks.

@casperisfine

Copy link
Copy Markdown
Contributor

I'll be fixing it and will come back to you once I have more accurate benchmarks.

Ok, so I'm afraid I'll have to give up for now on that one. I've been fighting with AR for the last 3 hours and defining attributes without connecting to the DB is terribly hard, even with ugly monkey patches.

@jonathanhefner
jonathanhefner force-pushed the refactor-model_schema-attributes branch from 1bfb1a4 to 88c6a92 Compare April 16, 2020 20:50
@jonathanhefner jonathanhefner changed the title Invert AR::ModelSchema / AR::Attributes dependency Make AR::Attributes independent of AR::ModelSchema Apr 16, 2020
@jonathanhefner

Copy link
Copy Markdown
Member Author

@casperisfine

TBH I'm having a bit of a hard time to understand this PR

Thank you for your feedback! I realized I needed to do a better job of explaining this PR. I hope I've done that now, but I would be appreciative of further critique.

The main one being connection.schema_cache, but there's also connection.lookup_cast_type_from_column. From what I understand of your PR, none of that is solved.

I can't say for sure if this PR will help, because I haven't had to face those problems, but here's what happens now (in this PR):

  • ModelSchema::columns_hash sets @columns_hash, but only if connection.schema_cache.columns_hash(table_name) is successfully called.
  • All attribute types and decorations (e.g. serialize) are "loaded" as a single unit, atomically. If ModelSchema::columns_hash or any connection.lookup_cast_type_from_column calls raise an error, the load will be prevented and the error will bubble up.

@casperisfine

Copy link
Copy Markdown
Contributor
  • raise an error, the load will be prevented and the error will bubble up.

I see. That's indeed is a bit cleaner than the way we unload on error currently. But that doesn't really solve the root cause IMO (doesn't mean this isn't worth it, I just can't say).

kamipo added a commit to kamipo/rails that referenced this pull request May 29, 2020
This is a common `class_attribute` implementation issue (e.g. rails#39467,
rails#39468, rails#38871).

For now, we should take care of the change propagation by themselves if
we want to ensure the propagation after the `class_attribute` are
accessed in subclasses.

Fixes rails#20678.
@rails-bot

rails-bot Bot commented Jul 16, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot Bot added stale and removed stale labels Jul 16, 2020
@jonathanhefner
jonathanhefner force-pushed the refactor-model_schema-attributes branch 2 times, most recently from 734c664 to 9430583 Compare July 20, 2020 16:42
jonathanhefner added a commit to jonathanhefner/rails that referenced this pull request Jul 20, 2020
This is an alternate take on rails#39882, based on top of rails#38871.

Co-authored-by: Ryuta Kamizono <[email protected]>
@jonathanhefner
jonathanhefner force-pushed the refactor-model_schema-attributes branch from 9430583 to acb539d Compare July 25, 2020 20:56
`AttributeDecorators` was removed in rails#39882 in favor of calling the
`attribute` macro with a block.  However, this technique overwrote the
original attribute type, which is still needed by some decorators.  Thus
those decorators had to bypass the encapsulation of the `Attributes`
module to retrieve the original attribute type.

This commit makes the `attribute` macro retain the original attribute
type when a new type is not specified.
@jonathanhefner
jonathanhefner force-pushed the refactor-model_schema-attributes branch from acb539d to e035a9e Compare July 27, 2020 15:17
@rails-bot

rails-bot Bot commented Oct 25, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot Bot added the stale label Oct 25, 2020
@rails-bot rails-bot Bot closed this Nov 1, 2020
@bf4 bf4 mentioned this pull request Oct 7, 2022
8 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(Object doesn't support #inspect) for singleton_class of model instance

2 participants