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

Migrate and delete ClientApplication Sequel model #15886

Merged

Conversation

thedae
Copy link
Contributor

@thedae thedae commented Oct 6, 2020

The spec tests are passing, but there are some problems with the Travis checks that are unrelated to these changes. Summoning @Jesus89 , he might have more information about this issue.

@thedae thedae marked this pull request as ready for review October 7, 2020 13:23
@thedae thedae requested a review from amiedes October 7, 2020 13:28
Copy link
Contributor

@amiedes amiedes left a comment

Choose a reason for hiding this comment

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

Nice work 🎉. I'm glad to see each day we're getting closer to removing these damn models 🙂

I've left some minor comments which should be easy to fix


def reset_client_application!
client_application&.destroy
Carto::ClientApplication.create(user_id: id)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the Sequel equivalent of this method raised an exception when it failed:

def reset_client_application!
  if client_application
    client_application.destroy
  end
  ClientApplication.create(:user_id => self.id)
end

Could you use Carto::ClientApplication.create! instead to maintain the previous behavior?

@@ -51,7 +51,7 @@ class Carto::User < ActiveRecord::Base
has_many :permissions, inverse_of: :owner, foreign_key: :owner_id
has_many :connector_configurations, inverse_of: :user, dependent: :destroy

has_many :client_applications, class_name: Carto::ClientApplication, dependent: :destroy
has_one :client_application, class_name: Carto::ClientApplication, dependent: :destroy
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this 🎉

@@ -437,7 +436,7 @@ def before_destroy(skip_table_drop: false)

assign_search_tweets_to_organization_owner

ClientApplication.where(user_id: id).each(&:destroy)
Carto::ClientApplication.where(user_id: id).each(&:destroy)
Copy link
Contributor

Choose a reason for hiding this comment

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

ActiveRecord already has a method which takes care of calling destroy on each of the association records:

Carto::ClientApplication.where(user_id: id).destroy_all

If you deem appropiate you can replace it.

def build_client_applications_from_hash(client_app_hash)
return [] unless client_app_hash
def build_client_application_from_hash(client_app_hash)
return nil unless client_app_hash
Copy link
Contributor

Choose a reason for hiding this comment

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

No big deal but I think this is equivalent to:

return unless client_app_hash

[client_application]
# Overwrite fields that were created with ORM lifecycle callbacks
client_application.key = client_app_hash[:key]
client_application.secret = client_app_hash[:secret]
Copy link
Contributor

Choose a reason for hiding this comment

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

Another way of achieving this could be to modify the model callbacks so those attributes are only assigned if not present yet, for example:

def initialize_entity
  self.key = OAuth::Helper.generate_key(40)[0, 40] unless key.present?
  self.secret = OAuth::Helper.generate_key(40)[0, 40] unless secret.present?
end

I don't have a strong opinion on this so choose what you think it's more intuitive to understand.

Maybe created_at is not overwritten by Rails if you pass it as an argument to the model constructor, but for updated_at you'll have to force it anyways.

@@ -172,7 +172,7 @@ def destroy_user(user = @user)
st.destroy
end
end
ClientApplication.where(user_id: @user.id).each(&:destroy)
Carto::ClientApplication.where(user_id: @user.id).each(&:destroy)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that now that you've fixed the 1-1 relationship, this could be rewritten as:

user.client_application.destroy

super.sub('carto.com', 'cartodb.com')
end

end
Copy link
Contributor

Choose a reason for hiding this comment

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

Ruby and Rails have a set of rules about how directories/classes should be named in order for the autoloader to work more seamlessly, and avoid to manually having to write explicitly things such as require_relative or require_dependency.

I know this code is just a port from the previous model, but if you feel like so you could move the DomainPatcherRequestProxy class to its own file in order to follow the conventions.

I this case i'd move it to lib/domain_patcher_request_proxy.rb since it's not per se a model, and you'll have to manually require it anyways since lib contents are not loaded in Rails by default.

For more info you can check the Zeitwerk documentation, which is the autoloader of Rails 6 (but the conventions exists from earlier).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree: having several classes in the same file is totally agains any kind of autoloader (and not only for Ruby)

@amiedes amiedes self-requested a review October 9, 2020 07:08
Copy link
Contributor

@amiedes amiedes left a comment

Choose a reason for hiding this comment

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

👍

@thedae thedae merged commit c9b92ed into master Oct 9, 2020
@thedae thedae deleted the chore/ch106931/migrate-and-delete-clientapplication-model branch October 9, 2020 07:19
@thedae thedae mentioned this pull request Nov 6, 2020
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