I want to delete a table in my rails app but i cannot use rollback, because i created this table a long time ago, and i have a LOT of other tables created since that one.
How is supposed to be named a drop table migration file and is there a way to genrate it with rails generate?
Create one more migration to drop the table. The class should have the method
def self.up
drop_table :table_name
end
Be careful as you will not be able to rollback to get all the data you will lose while dropping the table.
Related
Question regarding migration:
I had a table a few months ago called payment
then we wrote a migration and dropped it
now I want that payment table again
I generated a migration but when I migrate it actually drops and does not create payments.
Please, can someone give me direction? TA
It depends on how you dropped your table.
How Migration Works
Whenever migration is created, it has its own timestamp & when you run rake db:migrate, it will run up method (create table in your case) & store timestamp of that migration in schema_migrations table.
You can check whether payments table present or not in your database by using following,
ActiveRecord::Base.connection.tables.include? 'payments'
case - If you have dropped table not by rails migration (by console or external source), Then schema_migrations will not clear timestamp of migration which create payments table (running down method can clear timestamp). but adding new migration for creating payments table with new timestamp will not affect you.
And if still it does not help, it all depends on what you have written in your 'create payment table' migration file, method invoked inside it must be change or up (additional info).
I'm using Rails and Postgres. I currently have a Like table created. I need to drop this table and re-create it. Here's what I've done so far.
1) Rename create_likes migration file to create_likes_old so there is no conflict with file names within migrations.
2) Change the name of the table inside the migration from create_table :likes to create_table :likes_old.
3) Generate new create_likes migration that creates a table called Like.
Currently I'm running into the following issue when I run rake db:migrate:
PG::DuplicateTable: ERROR: relation "likes" already exists
This makes it seems like the Like table was never renamed to LikeOld. What's wrong and how do I fix this?
Old migrations don't run when you change their content, and you should not rename them. Even if they did run, changing the create_table :likes to create_table :old_likes cannot possibly change the name of an existing table. That isn't what create_table does. At best, re-running that migration will now cause a new table to be created called old_likes, with no content, while leaving your old likes table unaffected. In actuality re-running that migration will simply fail, as it will attempt to undo the migration first, dropping the table old_likes which does not yet exist.
What you need to do is create a new migration called rename_likes_to_old_likes, which actually renames the table, using rename_table. Then, either:
delete the old migration entirely, so you can introduce a new migration with the same name
OR
create a new migration with a unique name such as create_new_likes_table or the like, and introduce your new table there.
I'm a SQL pro and a rails newbie.
I'm having trouble understanding how best to use the rails database abstraction. Things I can do from a SQL command line in seconds seems overtly laborious in rails.
For example:
I generated a scaffold with several models and then tried to generate migrations to add associates to those models.
The generated migrations used 'create table' which won't work because the table already exists.
So I can either drop and recreate the table or use SQL 'ALTER TABLE' statements in the migration which makes me think I should have just created the database model by hand in the first place.
What are the benefits of using the rails data abstraction as apposed to doing the SQL with modeling tools and just using schema:dump and schema:load?
Well there are many reasons why you want to use migrations over than just writing your create table and ALTER TABLE in the console.
1.When I add a new table to column to a table there is a record that it happened and the rest of the developers on the team will know about it on the next commit with out having to send out email with complicated instructions to the ones that are not SQL pro
2.When you use a migration and you want to change databases from MySql to Postgres or anything else all you have to do is change the connection script
you can rollback your changes
Example:
class AddSsl < ActiveRecord::Migration
def up
add_column :accounts, :ssl_enabled, :boolean, default: true
end
def down
remove_column :accounts, :ssl_enabled
end
end
there are much more that you can do with migrations.
I recommenced looking at
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
I wanted to know what is the right way to update a Model.
for example, suppose I want to change a name of a data member from likes to numOfLikes.
this variable appears in the Model itself, but also in schema.rb and in db\migrate\XXX.rb
I changed manually those files and got this error:
undefined method `numOfLikes' for # Topic:0x3442d88
So, what is the right way doing it? (I am also asking about deleting a data member or adding one)
The best way is to run a migration to rename the column, which will update the schema.rb file.
You shouldn't be editing schema.rb directly, and the migration file only runs commands on the database, not affect the model if changed once run.
Schema.rb represents the state of the database schema, it doesn't control or change it by changing the contents of the file alone.
A new migration that contains:
rename_column :table_name, :likes, :numOfLikes
This will rename the column in the database, and will dump the database schema into schema.rb with the new attribute name.
I'm looking for some advise on the following:
You have an app live with customers and real data
While developing new features, you need to add a column to lets say the projects table
This new column is a UID of some type which is generating by the model using a before_save
This all works fine for new projects moving forward. But all existing projects are nil for that column and everything breaks.
How do you handle his in the Rails world?
Thanks
You could simply create a rake task that pulls in all projects without a UID and and one to each project.
After you run the migration run the task. All of your projects should now have a UID.
I think this should be handled within the migration script, rather than a Rake task.
If I understand correctly, it'll only ever need to be performed once, at the time the column is added to historical records. In my mind, a migration script shouldn't leave the app with a broken data set. Migrations are designed for more than just schema changes.
Here's an example:
def self.up
change_table :projects do |t|
t.integer 'new_column'
end
Project.reset_column_information
Project.all.each do |project|
project.new_column = some_value
project.save
end
end
The reset_column_information method makes Rails aware of the new column you just added.