10

I'm migrating from Authlogic to Devise.

UPDATED:

The migration of devise tries to re-create the table users, so i changed as you can see below the create_table to change_table and drop table at the end to remove what i add

The problem is when I run rake i get an error .

This is the error I get when running rake.

==  DeviseCreateUsers: migrating ==============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL

This is the migration

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
  end

  def self.down
    remove_column :users, :database_authenticatable
    remove_column :users, :recoverable
    remove_column :users, :rememberable
    remove_column :users, :trackable
    remove_index :users, :email
    remove_index :users, :reset_password_token
  end
end

In my schema.rb i already have this from Authlogic.

  create_table "users", :force => true do |t|
    t.string    "username"
    t.string    "email"
    t.string    "crypted_password"
    t.string    "password_salt"
    t.string    "persistence_token"

I think it sees some kind of conflict that i'm not able to realize how to avoid with those devise helpers

Thank you!

Martin
  • 11,216
  • 23
  • 83
  • 140
  • Please mark jamuraa's answer as correct. Otherwise this question appears in the "unanswered questions list". – kikito Mar 30 '12 at 18:57

4 Answers4

22

The error that you're getting is because you are trying to recreate the email field which you already have. The email field is created in the devise helper t.database_authenticatable. You can use your old user table with the new system, but you don't need to include t.database_authenticatable, you just need to rename your old field names. Looking in the Documentation for Devise, you can see that database_authenticatable just creates three fields: email, encrypted_password and password_salt. They are the same as the email, crypted_password, and password_salt that you already have in your authlogic user table, so you can use change_table like so:

def self.up
  change_table(:users) do |t|
    t.recoverable
    t.trackable
    # rememberable uses remember_token, but this field is different
    t.rename :remember_token_expires_at, :remember_created_at
    # these fields are named differently in devise
    t.rename :crypted_password, :encrypted_password
  end
end
jamuraa
  • 3,419
  • 25
  • 29
4

Instead of changing the create_table to change_table, you can just add the following line just before create_table:

rename_table :users, :old_users_authlogic

Then, right after the create_table:

say_with_time 'Migrating users from Authlogic to Devise...' do
  execute "INSERT INTO users (id, email, ...) SELECT id, email FROM old_users_authlogic"
end

If you are using indexes with referential integrity, don't forget to update them to the new table, as the rename_table will make them point to old_users_authlogic.

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
0

I solved this by deleting Devise migration file but not removing it from Sublime. I then did a rails g migration remove_email_from_foo, thus removing email attribute. I then cancelled the Devise delete migration file which bought the migration file back in & ran a rake db:migrate. Devise attributes saved to model

ainza
  • 36
  • 1
  • 2
0
add_column(:users, :database_authenticatable, {:null=>false})

You are not providing information for how this field is to be setup. It needs to be a string I think so you want to pass that information.

add_column :users, :database_authenticatable, :string,  :null=>false

More information about the error you are getting:

undefined method `to_sym'

That error for migrations is really frustrating because it lacks content, but what it means is that you got your migration in the wrong order.

For example - if you add a boolean file to the use model:

 add_column :users, :user_is_a_jackass, :boolean, :default => false

That will migrate ok. But if you do it like this:

add_column :users, :user_is_a_jackass, :default => false

You will get the same error because you are not telling the migration what field type it should be.

thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Thank you for your answer. What you said is true, however i realice that the original migration uses devise helpers and that's i think is why the migration was avoiding to define the type. I realice i could change create_table to change_table and I just update the problem. – Martin Nov 14 '10 at 03:19