Skip to content

Swallow devise routing errors when re-setting database#2192

Closed
nthj wants to merge 1 commit intoheartcombo:masterfrom
nthj:master
Closed

Swallow devise routing errors when re-setting database#2192
nthj wants to merge 1 commit intoheartcombo:masterfrom
nthj:master

Conversation

@nthj
Copy link

@nthj nthj commented Dec 29, 2012

I frequently run 'rake db:migrate:reset db:seed' when developing, to get back to my default mock-up state. Unfortunately, Devise assumes that the database table should exist at this point, and throws a SQL error. For example:

PG::Error: ERROR:  relation "users" does not exist

Since I'm trying to re-set the database, this is frustrating. I have to comment out the "devise_for" line in my routes.rb, run my migrations, then un-comment the devise_for line again.

This pull request simply swallows any exceptions from devise_for when running db:migrate.

@lucasmazza
Copy link
Contributor

Hi @nthj,

I wasn't able to reproduce this scenario locally but I know how annoying things like this can be, but instead of swallowing any possible error isn't better to emit a warning and skip the whole routes setup?

Also, more input on this would be welcome. /cc @rodrigoflores @carlosantoniodasilva @josevalim

@nthj
Copy link
Author

nthj commented Jan 6, 2013

Hmm, so you would like a backtrace? Or a one-line warning? Or just skip route set up on db:migrate entirely, whether an error is thrown or not?

rake db:migrate:reset will throw the error for me.

@josevalim
Copy link
Contributor

@nthj sorry for taking so long on this.

Can you provide an application that reproduces the issue?

Calling rake db:migrate:reset db:seed did not trigger it for it in any of our apps, which makes me suspect something more is going on. By default, Rails does not make queries when loading the model, so there is something in your model triggering a query. I don't know what it is, it may even be a has_many, but we need your help in finding it out.

Thank you.

@whoisjake
Copy link

I'm having the same exact issue. Rake db loads environment, which loads routes, which upon calling devise_for :users , loads the model, which tries to load the fields. Chicken and egg

The lines in question goes to a scope like this:
scope :active, where(:deleted => 0, :disabled => 0)

https://gist.github.com/whoisjake/15604c08003cc37331be

@lucasmazza
Copy link
Contributor

@whoisjake can you provide an application that reproduces the issue?

@whoisjake
Copy link

Yup! Just created it: https://github.com/whoisjake/devise_model_failure

Easy to reproduce, in fact,

$ rails new my_app
$ rails generate devise:install
$ rails generate devise User

edit user.rb to include a scope like:

scope :have_not_logged_in, where(:sign_in_count => 0)
rake db:migrate

BOOM CRASH

@whoisjake
Copy link

Oh I guess I didn't check in the Gemfile.lock

https://github.com/whoisjake/devise_model_failure/blob/master/Gemfile.lock

@lucasmazza
Copy link
Contributor

@whoisjake thanks a lot. I managed to reproduce the error and the scope definition is causing the error. Maybe @josevalim or @rafaelfranca can shed a light on this.

@whoisjake
Copy link

So apparently the use of those types of scopes is "deprecated" in Rails edge...

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/scoping/named.rb#L145

11 months ago...
rails/rails@0a12a5f

might have snuck in between 3.2.11 and 3.2.12 as that's a change I recently made...

@whoisjake
Copy link

Ok, nope, 3.2.12 does not have the deprecation, but this might have been there all along anyway...

https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/scoping/named.rb

@josevalim
Copy link
Contributor

Yeah, I was thinking about just suggesting to use lambda scopes. Since it
will fix the problem and your app will automatically work with Rails 4.

@whoisjake
Copy link

Yeah, I mean, I can do that... but the issue is still there as with 3.2.12 it's a perfectly valid way to do scopes. I'll probably just change my scopes instead of waiting around.

Good luck!

@whoisjake
Copy link

Ok, so I have confirmed that it's not a change in Devise. I went through each version back and the issue persisted. Upon changing from Rails 3.2.12 back to 3.2.11, it fails.

@whoisjake
Copy link

rails/rails@v3.2.11...v3.2.12

I believe its this:
https://github.com/rails/rails/blob/1dccd44a5c74f20b0406ecc8d39373226f73af35/activerecord/lib/active_record/relation/predicate_builder.rb#L54

When the WHERE clause is an integer, it goes out to the schema cache...

So thanks. Probably should close this out...

@lucasmazza
Copy link
Contributor

@whoisjake yes, and there's already some discussions about this on the Rails issue tracker. Thanks a lot for your help on digging this.

@nthj are you on the same scenario with Devise + Rails 3.2.12? If so, I think we can close this one.

@rafaelfranca
Copy link
Collaborator

Related to rails/rails#9290

@whoisjake
Copy link

Well looks like others came to the same conclusion! Thanks y'all

@josevalim josevalim closed this Feb 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants