Adding devise with an existing users model and therefor db table

1,698 views
Skip to first unread message

Seb

unread,
Feb 8, 2011, 7:58:29 PM2/8/11
to plataforma...@googlegroups.com
Under the "Getting started" section on https://github.com/plataformatec/devise there is an example migration where a user table is created. However I have already created and executed a migration creating the users table. So the migration I'm looking for adds the desired columns to my existing table.

Is there a smarter way then looking at another database (or the actual source code) and see what columns which needs to be added?

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:00:27 PM2/8/11
to plataforma...@googlegroups.com
Devise methods should work properly when using change_table (Devise for Rails 3 only I believe). So you can just do:

    change_table :users do |t|
      t.database_authenticatable
      t.confirmable
      # etc, include the modules you are going to use based on the readme
    end


On Tue, Feb 8, 2011 at 10:58 PM, Seb <sebastiant...@gmail.com> wrote:
Under the "Getting started" section on https://github.com/plataformatec/devise there is an example migration where a user table is created. However I have already created and executed a migration creating the users table. So the migration I'm looking for adds the desired columns to my existing table.

Is there a smarter way then looking at another database (or the actual source code) and see what columns which needs to be added?



--
At.
Carlos A. da Silva

Sebastian

unread,
Feb 8, 2011, 8:04:40 PM2/8/11
to plataforma...@googlegroups.com
Nice! But that for the sake of best pratice, do you have a suggestion
for how the down method should be?

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:10:17 PM2/8/11
to plataforma...@googlegroups.com
Yeah, you'd have to remove the columns by hand, unfortunately.
Look at Devise schema.rb file, and you'll see which columns should be removed.

Sebastian

unread,
Feb 8, 2011, 8:16:28 PM2/8/11
to plataforma...@googlegroups.com
ok, my existing users table already had an email column... that surly
will get ugly if I need to take that into account. Would that also be
a problem when changing the table in the up method that is?

On Wed, Feb 9, 2011 at 2:10 AM, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:22:09 PM2/8/11
to plataforma...@googlegroups.com

As you can see, if you add database_authenticatable in your migration, Devise will try to add another email column and it'll fail hard.
You have to add the encrypted_password attribute by hand so. As I told you, just add the column as devise schema shows you, it'll work smoothly.

Sebastian

unread,
Feb 8, 2011, 8:28:10 PM2/8/11
to plataforma...@googlegroups.com
The following migration seems to be doing the trick. Would you like me
to put in a gist and update the docs?


class AddDeviseColumnsToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.confirmable
t.recoverable
t.rememberable
t.trackable
end
end

def self.down
#the columns below are manually extracted from
https://github.com/plataformatec/devise/blob/master/lib/devise/schema.rb
#which means that if some of the devise methods executed in the above
self.up method are changed this will break
remove_column :users, :encrypted_password
remove_column :users, :password_salt
remove_column :users, :authentication_token
remove_column :users, :confirmation_token
remove_column :users, :confirmed_at
remove_column :users, :confirmation_sent_at
remove_column :users, :reset_password_token
remove_column :users, :remember_token
remove_column :users, :remember_created_at
remove_column :users, :sign_in_count
remove_column :users, :current_sign_in_at
remove_column :users, :last_sign_in_at
remove_column :users, :current_sign_in_ip
remove_column :users, :last_sign_in_ip
remove_column :users, :failed_attempts
remove_column :users, :unlock_token
remove_column :users, :locked_at
end
end


On Wed, Feb 9, 2011 at 2:22 AM, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:33:47 PM2/8/11
to plataforma...@googlegroups.com
Do you already have the encrypted_password column in your users table? I see you're not adding it in the :up method. If not, you have to.

About the down method, the columns are ok, but you can dry up a bit by using change_table and t.remove instead:

   change_table :users do |t|
      t.remove :foobar
   end

If you're able to, it'd be nice to add this info to the wiki, something like: "How to change an already existing table to add devise required columns", or sth like that =).
Thanks.

Sebastian

unread,
Feb 8, 2011, 8:38:53 PM2/8/11
to plataforma...@googlegroups.com
No encrypted_password column... would sometihng like the below work?
sorry for all the questions I'm new to rails in case thats not clear
by now.

add_column :users, :encrypted_password, String, :null => null,
:default => default, :limit => 128

:string instead of String?

On Wed, Feb 9, 2011 at 2:33 AM, Carlos Antonio da Silva

Sebastian

unread,
Feb 8, 2011, 8:41:18 PM2/8/11
to plataforma...@googlegroups.com
This ofcourse works, but is that enough?

add_column :users, :encrypted_password, :string, :limit => 128

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:42:40 PM2/8/11
to plataforma...@googlegroups.com
Yes, it should be.
But please, use it inside your other migration, with :change_table.

Sebastian

unread,
Feb 8, 2011, 8:51:37 PM2/8/11
to plataforma...@googlegroups.com
Like so... ? Shouldnt it be outside the change_table block?

class AddDeviseColumnsToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|

#if you already have a email column comment the below line
# t.database_authenticatable
t.confirmable
t.recoverable
t.rememberable
t.trackable

add_column :users, :encrypted_password, :string, :limit => 128

end
end


On Wed, Feb 9, 2011 at 2:42 AM, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Feb 8, 2011, 8:54:33 PM2/8/11
to plataforma...@googlegroups.com
   change_table :users do |t|
     t.confirmable
     t.recoverable
     t.rememberable
     t.trackable

     t.string :encrypted_password, :limit => 128
   end

Sebastian

unread,
Feb 8, 2011, 9:10:14 PM2/8/11
to plataforma...@googlegroups.com
Ofcourse!

Anyway I've created
https://github.com/plataformatec/devise/wiki/How-to:-existing-users-table

The time is 0300 here, I need some sleep. Thanks a lot for the support.

take care,
Seb


On Wed, Feb 9, 2011 at 2:54 AM, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Feb 8, 2011, 9:32:58 PM2/8/11
to plataforma...@googlegroups.com
Ok , thanks for adding the wiki info.

Sebastian

unread,
Feb 9, 2011, 1:19:28 PM2/9/11
to plataforma...@googlegroups.com
np, thanks for a great gem and the support.

On Wed, Feb 9, 2011 at 3:32 AM, Carlos Antonio da Silva

Reply all
Reply to author
Forward
0 new messages