Home>

When I tried redeploying on Heroku,
I got an error. ..

to no file migration file
touch db/migrate/20200811115754_hoga.rb
I tried to replace the file with the command, but it didn't work...

Status Migration ID Migration Name
--------------------------------------------------
   (2.6ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
   up 20200805054129 Devise create users
   up 20200805061051 Create sns credentials
   up 20200808070314 Create books
   up 20200808070822 Create active storage tablesactive storage
   up 20200810072111 ********** NO FILE **********
   up 20200811115754 ********** NO FILE **********
   up 20200811131113 ********** NO FILE **********
  down 20210810072111 Create messages
  down 20210810072112 Create favorites

heroku run rails db:rollback
ActiveRecord::UnknownMigrationVersionError:

heroku run rails db:migrate
Index name'index_messages_on_book_id' on table'messages' already exists

heroku run rails db:migrate:reset was also useless. ..

ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your'production' database.
If i am sure I want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/tasks/database_tasks.rb:63:in `check_protected_environments!'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:15:in `block (2 levels) in<main>'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require'
/app/bin/rails:9:in `<main>'
Tasks: TOP =>db:migrate:reset =>db:drop =>db:check_protected_environments
(See full trace by running task with --trace)
schema.rb
 create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.integer "user_id"
    t.integer "book_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["user_id", "book_id"],

 name: "index_favorites_on_user_id_and_book_id", unique: true
  end
  create_table "messages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.text "text"
    t.bigint "book_id"
    t.bigint "user_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: falseend
rollback
heroku run rails db:rollback
Running rails db:rollback on ⬢ books-28291... up, run.1599 (Free)
DEPRECATION WARNING: Including LoggerSilence is deprecated and will be removed in Rails 6.1. Please use `ActiveSupport::LoggerSilence` instead (called from<main>at /app/config/application.rb:7)
   (2.1ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode,',STRICT_ALL_TABLES'),',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (3.7ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
   (2.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
   (2.0ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
rails aborted!
ActiveRecord::UnknownMigrationVersionError:
No migration with version number 20200811131113.
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1169:in `move'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/migration.rb:1047:in `rollback'
/app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/railties/databases.rake:225:in `block (2 levels) in<main>'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.6.0/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency'
/app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require'
/app/bin/rails:9:in `<main>'
Tasks: TOP =>db:rollback
  • Answer # 1

    I will describe it because the answer was derived from the question.

    Organize the problem
    Index name'index_messages_on_book_id' on table'messages' already exists


    It seems that an index named index_messages_on_book_id has already been pasted in the messages table.

    Is _book_id written multiple times in the file under db/migrate/?

    It was recognized that the file has not been migrated yet because the file name was rewritten, and tried to create the message table again.

    There is a possibility.

    Solution

    Please restore the file name and all contents of the file once.

    heroku run rails db:migrate:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1


    To

  • Answer # 2

    heroku run rails db:migrate:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1
    Is useful when using heroku