How to update database schema without losing original data - ruby-on-rails

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

Related

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 can I position a new column on rails 4 migration?

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.

One time change model attribute (column name) in Ruby on Rails

I created a model with an attribute "name" but I want to change it to "username". Everything I've read about database migrations involves creating a class or some complicated stuff. All I want to do is the equivalent of "UPDATE TABLE" in SQL. How do you run a one-time database migration to change this? I'm guessing it'd involve rails console and then some command?
First:
rails g migration rename_name_column_to_username
Then in the generated rename_name_column_to_username.rb migration file:
class RenameNameColumnToUsername < ActiveRecord::Migration
def self.up
rename_column :users, :name, :username
end
def self.down
rename_column :users, :username, :name
end
end
And then rake db:migrate
If you haven't committed the code that originally created the "name" column, you can just go in to the old migration file that created that column and change name to username and then regenerate the schema.
But if you have committed the code, you should create a separate migration file that renames name to username.
This is important to keep track of the versioning of your database. So you should never really use manual SQL (ALTER TABLE ...) to change the schema.
Run rails g migration RenameNameToUsername, which will create a new file in db/migrate.
Open that file, and add this into the self.up section:
rename_column :tablename, :name, :username
Then run rake db:migrate

Rails 3 adding fields to a table

ok i am a noob and i want to know how to add fields to a migration in rails 3, additionally i am using Typus and would like to know if i need to add these fields manually or can i just regenerate the typus and it will just pick the new fields up?
Thanks in advance
Robbie
Migrations are used to add fields to the database, not tell Rails about fields.
Rails will actually inspect the table for its fields, so if you have an existing table, you can create a model called spy.rb and it will know about all fields in spies
To use migrations, run rails generate migration AddScreenshotColumns. Then that file can become:
class AddScreenshotColumnsToTemplate < ActiveRecord::Migration
def self.up
add_column :templates, :screenshot_file_name, :string
end
def self.down
remove_column :templates, :screenshot_file_name
end
end

Migration issue in Ruby-on-rails

Hey guys, when I first begin a rails project, the model user was designed and created. After all the migration part, it successful created the table "users" at postgres.
Well, then after doing some changes during the project, I realized that was missing an attribute/new column at the table.
So what I did was delete the table users from postgres and add a new column at my first migration ruby class:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :password
t.string :email
t.string :authorization_token //this is the new attribute that I insert
t.datetime :created_at
t.datetime :updated_at
t.timestamps
end
end
def self.down
drop_table :users
end
end
So, when I run again the db:migrate hopping that a new user table will be created with the new attribute :authorization_token, it doesn't work, but with no errors.
(I know that I should not remove the table, there is another smart way to do it)
A tip for working with Rails -- do not hand modify your tables using SQL. When you saw the problem you should have written a new migration like #nruth showed. Running the rake:migrate command would have worked fine for you.
In this case since you've already deleted your 'users' table you now have the problem that your database schema is out of sync with what Rails thinks it is. To fix this problem you can either get the database schema to roughly match what Rails thinks it is by hand creating the 'users' table, running the down migration and then then the up migration. Or you can get Rails up to speed with the fact that the 'users' table no longer exists. Rails stores migration info in either a schema_info table (Rails < 2.1) or schema_migrations table (Rails >= 2.1). If you remove that table then Rails will think the schema does not exist and try to run all the up migrations and recreate the 'users' table for you again.
Lastly, over time you may accumulate a number of migrations that individually add a column or two that you forgot to include. If you haven't yet shipped or aren't in production yet, then you can write a migration that sort of baselines your table. It would look something like this:
class CreateBaselineUsers < ActiveRecord::Migration
def self.up
create_table :users, :force => true do |t|
t.string :name
...
This will forcibly drop the table and recreate it with all the attributes that you want.
Migrations are run once & stored in the database as having been used (take a look in the schema_migrations table). You could try using rake db:migrate:reset to re-run your initial migration, but it's better to just add new migrations (you won't want to blow away your database when it has data in it) as follows:
script/generate migration add_authorization_token_to_users authorization_token:string
which will generate something similar to the following:
class AddAuthorizationTokenToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.string :authorization_token //this is the new attribute that I insert
end
end
def self.down
remove_column :users, :authorization_token
end
end
To see how add/remove column, change_table, etc work, take a look at ActiveRecord::ConnectionAdapters::SchemaStatements at http://api.rubyonrails.org or http://guides.rubyonrails.org/migrations.html

Resources