How can I position a new column on rails 4 migration? - ruby-on-rails

I have been trying to migrate a new change in the db schema and struggling to get it working.
I am trying to add a new column but after another column in the db. here is what I have been doing:
Dropping the column migration:
class RemoveMixFromProfiles < ActiveRecord::Migration
def change
remove_column :profiles, :mix
end
end
Adding the column back in but trying to get into certain position:
class AddMixToProfiles < ActiveRecord::Migration
def change
add_column :profiles, :mix, :string :after => :interview
end
end
When i run rake db:migrate in the schema it does not add it after interview column.
Does anyone know why?

You can't do this in a migration on Postresql. The only way I know of is to drop the database entirely and change the original migration where the table is created, then create the database again and run the migration. Then you can add the fields in the order you want. However, I don't recommend this.

Related

add index migration adding column instead of migration

I am trying to add a index to a column which already exists in my table by.
I want to add a index on item_id on product_images table
bundle exec rails generate migration AddIndexToProductImages item_id:integer:index
but the code i see in the migration file is
class AddIndexToProductImages < ActiveRecord::Migration
def change
add_column :product_images, :item_id, :integer
end
end
Not sure what could be causing this, can anyone help? Thanks.
Rails will not autogenerate the migration with content just for index
Edit the generated migration with following:
class AddIndexToProductImages < ActiveRecord::Migration
def change
add_index :product_images, :item_id
end
end
Following is the command to generate index migration File:
bundle exec rails generate migration AddIndexesToProductImages item_id:integer:index
In case it does not have proper command to add index, you can edit the generated file with the appropriate index you want to add. You will have to wring following in the change function.
def change
add_index :product_images, :item_id
end

How to add a new column in an existing table in Rails 5?

I want to add a new column in one of my table in Rails 5. I recently renamed a column by using the following way:
rails g migration ChangeJobsTable
then in 20160802104312_change_jobs_table.rb:
class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
rename_column :jobs, :skills, :skills1
end
end
then
rails db:migrate
It worked fine, but now if I want to also add a new column skills2, do I need to do it like this?
class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
add_column :jobs, :skills2
end
end
You indeed forgot the datatype. You can also do it via the console in the future:
rails g migration AddSkills2ToJobs skills2:string
You forgot to add datatype, below is the updated migration.
class ChangeJobsTable < ActiveRecord::Migration[5.0]
def change
add_column :jobs, :skills2, :string
end
end
This worked for me and you can verify after in the schema
rails g migration add_skills2_to_ChangeJobsTable skills2:string
rake db:migrate

Rails Migration Add_Index issue

I'm trying to add indexing to my users table for the email column. Typing rails g migration add_index_to_users_email generates the migration but the function is empty. The snake casing should be correct, so I'm at a loss as to why the migration is being created but the change function inside is empty.
I've also tried AddIndexToUsersName and the same issue arises.
Any direction on what the issue could be would be greatly appreciated. Only thing I can think of is that I'm using Postgres and not MySQL or SQLite, but that wouldn't matter would it?
As far as I know, migration generators only support addition and removal of columns, with a specified modifier. For example, if you wished to add a new string column phone to the users table, you could use the command
rails generate migration AddPhoneToUsers phone:string
Check the Rails Guides for column modifiers. You can try
rails generate migration AddIndexToUsers email:index
to add an index to the existing column. However, I am not sure if generators support column modification. You can write the migration yourself, assuming the email column already exists on the users table:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_index :users, :email
end
end
Have a look at:
http://guides.rubyonrails.org/active_record_migrations.html
The correct command is
rails g migration AddIndexToUsers email:string:index
This will generate:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_column :users, :email, :string
add_index :users, :email
end
end
Edit the migration file and delete the add_column line, then run the migration.

How to destroy columns mistakenly created by Paperclip in Ruby on Rails

As the title suggests, I accidentally add some random attachment columns to my model. Say I did rails generate paperclip portfolio href
How do I remove those columns created? rails destroy paperclip portfolio href doesnt seem to work!
For recent version rails 4++ Just create a migration Example:
rails g migration remove_attachment_photo_to_course
Then open the migration file
class RemoveAttachmentPhotoToCourse < ActiveRecord::Migration
def change
remove_attachment :courses, :photo
end
end
Then run, to update the table
rake db:migrate
Where:
photo is the name of paperclip attachment you want to remove
courses is the name of table name
I have created a migration file and change the content to:
class RemoveAttachmentHrefToPortfolios < ActiveRecord::Migration
def self.up
remove_attachment :portfolios, :href
end
def self.down
remove_attachment :portfolios, :href
end
end
The problem is it is not the elegant and correct way to do it. I hope someone can improve this answer..
Normally for a migration you run rake db:rollback. Since this was a generator, you can just manually edit the table and drop the columns.
alter table portfolio drop column href_content_type;
alter table portfolio drop column href_file_name;
-- ... etc for each of the Paperclip columns.
You could also create a migration to drop the columns, but that would be overkill unless your whole team also ran the generator or you checked in schema.rb and others ran it also.
Here is an example migration for removing columns:
class RemoveMagazineFromBooks < ActiveRecord::Migration
def change
# This is not used ANYWHERE...
if column_exists? :refinery_books, :magazine
remove_column :refinery_books, :magazine
remove_index :refinery_books, :magazine if index_exists? :refinery_books, :magazine
end
end
end
Notice the test to check if the column is there. Some people may not have run the previous migration and will not have the column.
This comes from the Paperclip documentation:
remove_attachment :portfolios, :href
This will rollback the latest changes to the DB schema:
rake db:rollback
After that, simply delete the migration file.
This is presuming that you didn't commit any of the changes/migrations to a shared repository.
$ rails generate migration RemoveColumnsFromModel portfolio href
should make you a migration that will remove two columns "href" and "portfolio" from some model's table. The migration will look something like:
class RemoveColumnsFromUser < ActiveRecord::Migration
def change
remove_column :users, :foo, :string
remove_column :users, :bar, :string
end
end

How to update database schema without losing original data

Hi guys I would like to know if there's a way not to lose the data if you're trying to rollback your migration just to update the schema ? For example after running rake db:migrate, and after few rounds of data inserted, you want to add in a new attribute to the schema.
So my question is how can i add the new attribute without losing my previous record ? is it possible to do so ? Because all this while i just did by running rake db:rollback STEP= ... and lost all the data i've generated. Just wondering.
Thanks for helping
From:
BC2
if you have an existing table and want to add new attribute in existing table then simple write stand alone migration.
for ex: you have a students table with attribute name, roll_no ... and now u want to add 'address' attribute in students table
$ rails generate migration AddAddressToStudents address:string
will generate
class AddAddressToStudents < ActiveRecord::Migration
def change
add_column :students, :address, :string
end
end
then simply run "rake db:migrate"
You don't need to rollback to update the schema. Just write a new migration to update the existing table.
For example, to add a field to your user table without destroying anything, write a migration like:
class AddFieldsToUser < ActiveRecord::Migration
def change
change_table :users do |t|
t.date :birthday # add new field
t.remove :first # remove a field
t.rename :gender, :sex # rename a field
end
end
end
See here for more info: http://guides.rubyonrails.org/migrations.html#changing-tables

Resources