I have a problem during migrating 3 models (Game,Page,Section), which is I created 3 models and forget to save it, then directly migrate it. After that, I created a new model called User, then save it, and this User model perfectly saved and migrated successfully. After that when checked schema, those 3 models did not have any fields and did not migrated.
So I did the rake db:rollback, it only rollback to User Model, but I want those 3 Models to be rollbacked. I cannot do new migration to put new values.
Q1 : So do I need to delete those 3 models and create new models?
If it is your personal project just to learn, you can drop your database and, modify migration files and then create and migrate it.
rake db:drop
modify migration files
rake db:create
rake db:migrate
But you will lose all you data in database, so it is very bad idea for future. Better learn how you can create another migrations and add columns you need for your tables.
Look here: add_column
Ot you can always rollback few migrations using rake db:rollback STEP=n
Related
I created a model for comments at the start of a project, but have now come to the realisation I need to create some polymorphic relations so I can use comments with a number of other models as well. Considering the the code I already have etc, I'm thinking it might be easier for me to just start again from scratch so I can build all the views/controllers etc in the correct way for my new polymorphic world.
I see that I can run rails destroy model comments to achieve this but I have two questions on that:
Will this delete the model, migrations AND the actual DB table?
What are the implications when I want to create a new model with the exact same name?
In order to completely remove all columns &
tables that migration has created you need to run:
rails db:migrate:down VERSION=012345678 (where 012345678 should be the version number of your migration)
.............................
rails destroy model Comments
will delete your Model, pending migration, tests and fixtures
So destroy it's the opposite of generate:
$ bin/rails destroy model Oops
invoke active_record
remove db/migrate/20120528062523_create_oops.rb
remove app/models/oops.rb
invoke test_unit
remove test/models/oops_test.rb
remove test/fixtures/oops.yml
And, you can now create a new Model with the same name, as there's no trace of your previous one :)
If you have already migrated the database after creating the model:
First, rollback the changes to the database:
rake db:migrate:down VERSION=20100905201547
where version is the timestamp identifying the migration. For example, if your migration file is called 20170411182948_create_comments.rb then your version parameter should be 20170411182948
Then run
rails destroy model comments
The first command will delete the table from the actual database. The second command will delete the model and the migration file. Make sure you run them in that order as the first command is dependent on the migration file to perform the rollback (which is deleted during the second command).
If you have have not migrated the database:
The table would not have been added to your database. You can go ahead and delete your model and migration files manually or use the destroy command.
You might need to remove the table thoroughly.
Run this :
sqlite3 db/development.sqlite3
Then :
sqlite> drop table table_name;
sqlite> .quit
I have a question regarding my migrations in rails.
Normally when i want to add a colum to a model for i dont make extra migrations but instead i perform this steps:
rake db:rollback
next i change the migration file in db/migrations and rerune:
rake db:migrate
The biggest problem is that when i do this i loose my data.
Previous i wrote migrations from the command line with for example
rake g migration Add_Column_House_to_Users house:string
The problem with this approach is that my db/migrations folder afterwards get very large and not very clear! I mean at the end i dont know wich variables the object has! Im not an expert in rails and would like to ask you how to keep the overview over the migrations!Thanks
Just a minor thought - I just use the file db/migrate/schema.rb to determine whats in the database as opposed to tracking through the migrations
You definitely shouldn't use db:rollback with a table with existing data.
I have a few production RonR apps with a ton of data and there are 100+ entries in the migrations table and adding new migrations to tweak tables is the rails way to do things. Not sure what you mean by lucid, but your schema and data model are going to change over time and that is ok and expected.
One tip. The migrations are great, but they are just the beginning, you can include complex logic as needed to fix your existing data (like so)
Changing data in existing table:
def up
add_column :rsvps, :column_name_id, :integer
update_data
end
def update_data
rsvps = Rsvp.where("other_column is not null")
rsvps.each do |rsvp|
invite = Blah.find(rsvp.example_id)
...
rsvp.save
end
end
Another tip: backup your production database often (should do this anyway), but use it to test all of your migrations before deploying. I run scripts like this all the time for local testing:
mysql -u root -ppassword
drop database mydatabase_dev;
create database mydatabase_dev;
use mydatabase_dev;
source /var/www/bak/mydatabase_backup_2013-10-04-16.28.06.sql
exit
rake db:migrate
When I ran bundle exec rake db:test:prepare I got the following:
rake aborted!
Multiple migrations have the name CreateMicroposts
To check the status of my migration files, I ran
rake db:migrate:status
And got:
Status Migration ID Migration Name
------- --------------- -----------------
up 20120616205407 Create users
up 20120622103932 Add index to users email
up 20120622114559 Add password digest to users
up 20120628095820 Add remember token to users
up 20120704123654 Add admin to users
down 20120706103254 Create microposts
up 20120707073410 Create microposts
As you can see, I have two migration files with the exact same names and the exact same code in them. It's only their statuses differ, i.e. Up and Down.
What does Up and Down signify?
And which one can I delete, if I have to?
The problem is that you have two different migration files containing the header
class CreateMicroposts< ActiveRecord::Migration
rake db:migrate:status does not check the status of your migration files. It tells you what migrations will be applied if you run rake db:migrate. The up/down labels are pretty much self-explanatory: it tells you whether the migration will be applied via the up method or the down method. The up method is ran when you migrate and the down when you rollback a migration. You can make some further reading about Rails migrations here.
up is the method called when "evolving" (ie migrating to a new schema), while down is the method called when "regressing" (ie migrating to an older schema version, because one of your changes doesn't suit you). db:migrate calls up, db:rollback calls down. In recent versions of rails, there's change that handles both at the same time.
As for the deletion... I don't do activerecord much these days, but I think you're free to do whatever you want with your files. I don't think deleting a duplicate file will do any harm, and if it does.. Well, you use source control, right ? :)
I have multiple migrations:
.....
create table
add column
add another column
remove other column
6 .....
Is there any ablity to change all these migrations to one new migration which will create the same database schema?
I think that you can change remove those migrations and create a new one, called for example "initial_schema", where you should put the content of the schema.rb composed by all your previous migrations.
Consider that rails generate a schema.rb for you when you run those migrations.
Then you need to drop all tables in your database and run again rake db:migrate.
I have 4 related migrations in my Rails app:
First 3 migrations create one table each in the self.up and and drops them in their respective self.down methods.
4th migration runs a rake task that loads data to all three (in self.up). I am thinking what to put in self.down of this migration to delete the rows from the 3 tables?
Am I doing it wrong? Probably should have created one migration where I create 3 tables and load the data in self.up and just drop all three tables in the self.down method?
This is wrong!
Migrations should be used only for migrating your Data Model not your Data.
Rails 2.3.4 adds 'seeds'. This is a file in db/seeds.rb contains ruby code to 'bootstrap' your database. This is a great way to create semi-static content for your database like categories, look-up tables or user accounts.
You can then load the seed data with a simple rake task
rake db:seed
There isn't really a right or wrong way to do this. Based on what you've done, the 4th migration should just unload the data in its down method. Each down should only undo the actions of the up.
Loading data in the migration that creates the table is certainly not required. If you have 3 tables that create has_many or belongs_to relationships then it would make sense to put the data in a separate migration so you can create the relationships and then use them in your data load.
All that aside, if you have a rake task to load data, why bother with a migration that runs that rake task? Just make running the rake task part of your install, or just use your rake task to load or unload some demonstration data.
Generally I keep my migrations schema focused, and I'd put any data loading in a rake task I call independently.