1

I have a very large amount of migration files that need to run and some of them apparently are outdated. For example:

This fails:

  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      ...

With this error:

-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition

I assume because Devise was updated at the time of writing this.

What else can I do?

It turns out there's a schema file. Here's part of it:

ActiveRecord::Schema.define(version: 20160221211458) do

  create_table "activities", force: true do |t|
    t.integer  "user_id",                  null: false
    t.string   "target_type", default: "", null: false
    t.integer  "target_id",                null: false
    t.string   "action",      default: "", null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "activities", ["target_id"], name: "index_activities_on_target_id_and_target_type", using: :btree
  add_index "activities", ["user_id"], name: "index_activities_on_user_id", using: :btree

  create_table "addresses", force: true do |t|
  ...

What's the difference between doing bundle exec rake db:schema:load vs trying to run the migrations. Is there a difference between the two?

Also, this database is a mysql database:

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: mysql2
  database: arthouse_development
  username: root
  password:
  host: localhost
  port: 3306
  #socket: /tmp/mysql.sock

How do I see this database after running bundle exec rake db:schema:load

Jwan622
  • 11,015
  • 21
  • 88
  • 181

2 Answers2

1

This fails:

def self.up
 create_table(:users) do |t|
 t.database_authenticatable :null => false

I assume because Devise was updated at the time of writing this.

I need to know the error messages you get

It turns out there's a schema file:

In your schema I see that you have not users table, I think that you need to check your db mysql2 and see if you have something there.

What's the difference between doing bundle exec rake db:schema:load vs trying to run the migrations. Is there a difference between the two?

let's say I fork a github project and the schema file is already populated. With that command I load the schema so I do not run every migration but just create the tables like in the schema.

Migrations use scripts to populated the database, this scripts have function like add_column that modify the schema.rb file.

Every function from ActiveRecord:Migrations will modify the schema, this means maybe adding a column and later deleting. This conflict in migrations are cause by the incompatibility in this migrations, for example you add a column, then you need to reverse it, but later you add an index to that column, so the migration fails. More info at the following link

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.

You can think of each migration as being a new 'version' of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version. Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.

Solution

Your error

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition

Given your problem, you should find the solution hear

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x9dee690>

Community
  • 1
  • 1
Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • the schema file is just a segment of the schema file – Jwan622 Nov 02 '17 at 16:58
  • 1
    @Jwan622 notify me when you provide me more info – Fabrizio Bertoglio Nov 02 '17 at 16:59
  • 1
    @Jwan622 do you think this will work? https://stackoverflow.com/questions/10630768/undefined-method-database-authenticatable-for-activerecordconnectionadapte#10731177 – Fabrizio Bertoglio Nov 02 '17 at 17:13
  • this is dope dawg – Jwan622 Nov 02 '17 at 17:25
  • 1
    @Jwan622 Yes. It is. I would include a reference to that question and if no other problem comes out feel free to accept it. Thanks a lot. Fabrizio – Fabrizio Bertoglio Nov 02 '17 at 17:26
  • But in terms of trying to get a database up and running, why wouldn't people just use `bundle exec rake db:schema:load` instead of running the migrations which might be broken? – Jwan622 Nov 02 '17 at 17:44
  • 1
    @Jwan622 that does not make sense, what if they want to revert the db to a specific migration? It is not good. I never do that. I am against it. Migrations needs to work, but some people do like you say. They just run `rake db:schema:load` then they run the migration, they never migrate down. – Fabrizio Bertoglio Nov 02 '17 at 19:24
1

I also ran into this issue on my existing app as I tried to deploy to a new staging server.

Ended up being that the Devise gem had been updated to 2.1 and I hadn't fixed the migration to work correctly with the new version.

Be sure to read through their migration to 2.1 doc on their wiki -- https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.1

Also, be sure to read through the doc for how to correctly adjust existing migrations -- https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

Ronak Bhatt
  • 162
  • 1
  • 15