mysql - Rails migration appears completed, but rake still shows migration as pending -


i ran simple rails migration on large mysql2 db add column table:

class addmiddlenametoperson < activerecord::migration[5.0]   def change     add_column :person, :middle_name, :string   end end 

i disconnected server running rails app during migration. reconnected , checked migration status bundle exec rake db:migrate:status, showed down:

down    20170424182410  add middle name person 

i assume still running in background. left time, , using rails console verified person.middle_name accessible on objects. however, db:migrate:status still shows migration down, , if try run db:migrate again error:

mysql2::error: duplicate column name 'middle_name' 

so seems new column in database, , accessible through activerecord, rake db:migrate:status finds migration down , rake db:migrate attempts re-run it, unsuccessfully.

if production database (or other database important data) do not rake db:reset drop database , you'll lose everything; don't db:migrate:down drop middle_name column , you'll lose whatever middle names have.

first backup of database or @ least table you're working with.

second, connect database mysql cli tool , describe people;. information in question suggests see middle_name column in there doesn't hurt make sure you're connecting right database. if middle_name isn't there you're connecting wrong database somewhere, if there have migration issue clean up.

you database connection dropped before migration finished. migrations work in sequence:

  1. run migration update database.
  2. record migration's version number in schema_migrations table.
  3. regenerate db/schema.rb or db/structure.sql.

if 1 completes connection lost 2 never happens migration have run rails won't know it.

if no other environments need migration can delete migration , rake db:schema:dump or rake db:structure:dump fresh schema.rb or structure.sql. migrations temporary bits of code b deleting them after they've been run everywhere fine (and recommended), matters long term database's structure (which in db/schema.rb or db/structure.sql).

if other environments need run migration can manually patch schema_migrations table; connect database mysql cli tool , insert schema_migrations (version) values ('20170424182410');. rails know migration run , future rake db:migrate calls happy. you'd want refresh schema.rb (with rake db:schema:dump) or structure.sql (with rake db:structure:dump).


you have db/schema.rb file tracking database's structure (including version numbers of migrations have been run). if you'd use rake db:schema:dump regenerate it. if have db/structure.sql you'd use rake db:structure:dump.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -