Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise migration on existing model

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!

like image 910
Martin Avatar asked Nov 14 '10 03:11

Martin


2 Answers

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
like image 169
jamuraa Avatar answered Sep 30 '22 15:09

jamuraa


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.

like image 35
Fábio Batista Avatar answered Sep 30 '22 16:09

Fábio Batista