I wrongly named a column hased_password
instead of hashed_password
.
How do I update the database schema, using migration to rename this column?
I wrongly named a column hased_password
instead of hashed_password
.
How do I update the database schema, using migration to rename this column?
Manually we can use the below method:
We can edit the migration manually like:
Open
app/db/migrate/xxxxxxxxx_migration_file.rb
Update
hased_password
tohashed_password
Run the below command
Then it will remove your migration:
It will add your migration with the updated change.
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
Under
Available Transformations
rename_column(table_name, column_name, new_column_name):
Renames a column but keeps the type and content.
If the column is already populated with data and live in production, I'd recommend a step by step approach, so as to avoid downtime in production while waiting for the migrations.
First I'd create a db migration to add columns with the new name(s) and populate them with the values from the old column name.
Then I'd commit just that change, and push the change into production.
Then once the commit has been pushed into production, I'd run.
Then I'd update all of the views/controllers that referenced the old column name to the new column name. Run through my test suite, and commit just those changes. (After making sure it was working locally and passing all tests first!)
Then I'd push that commit to production.
At this point you can remove the original column without worrying about any sort of downtime associated with the migration itself.
Then push this latest migration to production and run
bundle exec rake db:migrate
in the background.I realize this is a bit more involved of a process, but I'd rather do this than have issues with my production migration.
Open that migration file and modify that file as below(Do enter your original
table_name
)Generate a Ruby on Rails migration:
Insert code in the migration file (XXXXXfixcolumnname.rb):
IMO, in this case, better use
rake db:rollback
. Then edit your migration and again typerake db:migrate
. However, if you have data in the column you don't want to lose, then userename_column
.