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

Migration from the Squeel Community #20

Closed
Schwad opened this issue Aug 4, 2016 · 11 comments
Closed

Migration from the Squeel Community #20

Schwad opened this issue Aug 4, 2016 · 11 comments

Comments

@Schwad
Copy link

Schwad commented Aug 4, 2016

I am here after going through this thread: activerecord-hackery/squeel#412

I'm sure I'm not the only one maintaining an app that relied on Squeel for a long time and is now trying to upgrade to Rails 5, seeing Baby Squeel as the only option going forward. Would it be possible to get a chunk in the README to help us flag up what changes we will need to make to an application to switch from Squeel to Baby Squeel integration?

Happy to have a dialogue here on this, hope my issue makes sense. Thanks!

@Schwad
Copy link
Author

Schwad commented Aug 4, 2016

Example one:

  1. Command+Shift+F changing where{ to where.has { seemed to work in one instance.

@rzane
Copy link
Owner

rzane commented Aug 4, 2016

Hi @Schwad, this is a great idea. I actually added a Squeel compatibility mode for this exact reason, which should make the migration a little bit easier. Enabling compatibility mode will override Active Record's query methods, allowing where { to work. It's meant as a slightly cheaper migration path.

I do, however, recommend avoiding compatibility mode. Because it overrides methods in Active Record, it is more likely to break with future Active Record releases. Stability is one of my top goals for this project.

I like the idea of this thread, and I'd appreciate it if we could get a list of the changes you had to make as you migrated. I'll also put together a list of all of the differences I'm aware of.

@Schwad
Copy link
Author

Schwad commented Aug 4, 2016

Glad to hear it @rzane , hopefully as I go forward I can be a bit of use, that way future folks coming to this gem can do the switch in minutes :)

On 4 Aug 2016, at 14:41, Ray Zane [email protected] wrote:

Hi @Schwad, this is a great idea. I actually added a Squeel compatibility mode for this exact reason, which should make the migration a little bit easier. Enabling compatibility mode will override Active Record's query methods, allowing where { to work. It's meant as a slightly cheaper migration path.

I do, however, recommend avoiding compatibility mode. Because it overrides methods in Active Record, it is more likely to break with future Active Record releases. Stability is one of my top goals for this project.

I like the idea of this thread, and I'd appreciate it if we could get a list of the changes you had to make as you migrated. I'll also put together a list of all of the differences I'm aware of.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@rzane
Copy link
Owner

rzane commented Aug 4, 2016

I've started a wiki page here: https://github.com/rzane/baby_squeel/wiki/Migrating-from-Squeel

Contributions welcome!

@Schwad
Copy link
Author

Schwad commented Aug 5, 2016

This is really handy! Thanks!!!

@Schwad
Copy link
Author

Schwad commented Aug 5, 2016

I'm not sure how to make contributions to someone else's wiki, a quick search in this wasn't clear.

On https://github.com/rzane/baby_squeel/wiki/Compatibility-Mode you may want to change your code to:

BabySqueel.configure do |config|
  config.enable_compatibility!
end

To match your gem. I do not intend to rely on enable_compatibility per your guidance, but it is currently helpful as I try to get this up and running and put the migration to baby_squeel as its own ticket separate from Rails 5 upgrade.

@rzane
Copy link
Owner

rzane commented Aug 5, 2016

Oh, nice catch! Thanks.

@Schwad
Copy link
Author

Schwad commented Aug 18, 2016

Okay, so here's a command working on someone else's machine but not mine with Baby Squeel, let me know what you think or what configurations I need to do.

Customer.includes(:custom_fields).where("custom_fields.customer_id IS NULL")

That's an example. Essentially for when I want to find customers with no custom_fields, when there's a has_many relationship to the fields. Similarly with IS NOT NULL for the inverse.

Here is the error that I get on my machine (with object names edited to match example above):

Customer Load (52.4ms)  SELECT "customers".* FROM "customers" WHERE "customers"."deleted_at" IS NULL AND (custom_fields.customer_id IS NULL)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "custom_fields"
LINE 1: ...mers" WHERE "customers"."deleted_at" IS NULL AND (custom_fields...
                                                             ^
: SELECT "customers".* FROM "customers" WHERE "customers"."deleted_at" IS NULL AND (custom_fields.customer_id IS NULL)
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/rack-mini-profiler-0.10.1/lib/patches/db/pg.rb:90:in `exec'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/rack-mini-profiler-0.10.1/lib/patches/db/pg.rb:90:in `async_exec'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activesupport-5.0.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:103:in `exec_query'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:373:in `select'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:41:in `select_all'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/querying.rb:39:in `find_by_sql'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:699:in `exec_queries'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:580:in `load'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:260:in `records'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:683:in `inspect'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/nickschwaderer/.rvm/gems/ruby-2.2.3/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'2.2.3 

I'm sorry if this is related to something else in my Rails 5 upgrade - just wanted your thoughts.

@rzane
Copy link
Owner

rzane commented Aug 18, 2016

The example you've provided above isn't using Baby Squeel at all. However, the problem is that you need to tell Active Record that you're referencing that table (to kick off a join).

Try this:

Customer.includes(:custom_fields).where("custom_fields.customer_id IS NULL").references(:custom_fields)

@Schwad
Copy link
Author

Schwad commented Aug 18, 2016

Thanks @rzane, perhaps I should have articulated that it wasn't a direct use of Baby Squeel, but me trying to use logic that was a bit closer to SQL to see why I was getting that error, I guess since Baby Squeel didn't monkey patch activerecord at all, it won't have any possibility of a knock-on effect when I'm using non BabySqueel style queries!

Thanks for the prompt response and feedback!

@rzane
Copy link
Owner

rzane commented Aug 18, 2016

No problem. And yes, because there are no monkey patches, Baby Squeel will not affect any of Active Record's bevahior.

@rzane rzane closed this as completed Sep 9, 2016
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

No branches or pull requests

2 participants