Search code examples
ruby-on-railsrubypostgresqlrubygemsmigration

Best tactics to deal with failed migration using Ruby on Rails


I cloned an old project of mine (live on production) from GitHub to my new MacBook running macOS v11 (Big Sur). I wanted to work on new features and updates, but I keep getting some migration errors.

As far as I know, we should not delete any migration file. So what is the best practice to fix these errors?

  • Should I delete, edit the migration files that causes issue?
  • Should I delete the relevant gems along with migration files and install fresh?
  • Any other suggestion?

Command (as root):

bin/rails db:migrate RAILS_ENV=development

Output:

    ** Invoke db:migrate (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Invoke db:load_config (first_time)
    ** Execute db:load_config
    ** Execute db:migrate
    == 20171118122416 AddTaggingsCounterCacheToTags: migrating ====================
    -- add_column(:tags, :taggings_count, :integer, {:default=>0})
       -> 0.0521s
    rails aborted!
    StandardError: An error has occurred, this and all later migrations canceled:

    uninitialized constant AddTaggingsCounterCacheToTags::ActsAsTaggableOn

Solution

  • No, you should not delete migrations, as per Ruby on Rails' own suggestion you're probably better off running bin/rails db:schema:load when creating a new DB on an old project with already existing migrations

    From schema.rb:

    This file is the source Rails uses to define your schema when running bin/rails db:schema:load. When creating a new database, bin/rails db:schema:load tends to be faster and is potentially less error prone than running all of your migrations from scratch. Old migrations may fail to apply correctly if those migrations use external dependencies or application code.

    Note that Ruby on Rails db:setup does db:create, db:schema:load, and db:seed which is useful when setting up a project