how to avoid a faulty rollback migration in ruby? - ruby-on-rails

i have a bug in a "self.drop" in a migration such that I cannot roll back past that migration. how can i start from scratch and build up from migration 001? also, is there a way to do this without losing my data (it's just testing, but still...)

You can comment all statements in self.down migration and rollback to previous db version.
Then apply changes by hand using a gui/web db client to match db schema before migration.
After you will be able to run migration again and your data will not be lost.

rake db:drop
rake db:create
rake db:migrate
It will reset your database and run all migrations. If you don't want to lose your data you can save it using plugin yaml_db:
rake db:data:dump # stores all data in db/data.yml
...
rake db:data:load # loads db/data.yml to database
If you have an error in a migration you can edit it and then try to rollback.

I realise this is an old question, but imho still relevant. If you want to recreate your database from scratch, one does not rollback and rerun migration, one just does
rake db:setup
This will drop, create and fill the database with the current combined state of all the migrations. This is because, definitely in a large system, running all the migrations might no longer work. However, running the schema.rb will always work.
My advice would also be to avoid rollback of migrations as much as possible, unless when you just created a migration and want to make some addition/typo/fix, before having pushed the migration to your team or any deployment.

Related

Rake db:drop not working?

I have unseen duplicate relations and tables and I am trying to destroy my entire DB and reset out of pure frustration, and I can't even do that. When I run rake db:drop or reset, everything is still there in my schema and migrations. How do I kill this entire DB and build it over again?
While they are closely related, migration files/schema.rb are not the same thing as the database. rake db:drop doesn't delete the migration files or the schema because people may wish to easily recreate their entire database while maintaining the information on how the database was built (ie the migration files).
If you want to start from scratch, drop the database rake db:drop and manually delete the migration files (schema will be overwritten when you create your new migrations).
If you do
rake db:rollback STEP=100
That will revert the last 100 migrations (so take you to zero state)

How to get back the tables in SQLite

I'm beginner in Ruby on Rails. I dropped my table. But I have the files in the db/migrate folder. How can I get back my table from those migrate files?
The ideal way to do this, is using
rake db:setup
This will recreate the database and load the schema into the development database. With every migration rails keeps the current state of the database in schema.rb (or structure.sql), and it uses those to efficiently recreate the last state.
If you have pending migrations, you will have to do rake db:migrate, but this will take more time, since it will redo every step as before.
Also note in some cases it is not possible to run the migrations from the start again, and imho that is not the intention of the migrations.
Run rake db:migrate to get it back.
According to this question, you should be able to do the following:
rake db:create #-> creates DB based on the schema file
rake db:migrate #-> populates the db with the various changes in the migrations
If you follow these steps, with nathanvda's advice, you should be able to solve the issue you're seeing!

Ruby on Rails: collapsing all migrations into one

I am trying to collapse all of my migrations into one (just to try it out), using this blog advice: I personally prefer to collapse all the existing migrations into a single migration at the end of every release, by just copying the schema.rb into "001_collapsed_schema.rb". This way, you won't have these older migrations which need to be "maintained".
So I delete all my migrations, create a new migration file 001_collapsed_schema.rb and copy schema.rb into it. Then I try to run
rake db:migrate
and get this NameError: uninitialized constant CollapsedSchema error. What did I do wrong?
I don't think you are supposed to copy your schema.rb into your migration file but rather merge all different migration files into one.
This has the benefit of less migration files and quicker preparation of fresh DBs in the future (e.g. instead of having to go through 1000 migration files you go only through 100).
Maybe the author of the blog you mentioned is doing that as a way to keep different versions of her schema.rb, in which case you cannot execute them as migrations but rather rollback to them, if needed, by replacing your schema.rb with them and issuing:
rake db:schema:load # deletes all DB data!
See also the discussion in this relevant SO question: rake db:schema:load vs. migrations

How to rollback to beginning and recreate/rebuild new migrations

So this is my first real Ruby on Rails project. I've learned my lesson -- I didn't make all changes using migrations so things are a bit messed up.
What's the best way to start over with new migration files and rebuild the schema, etc? My project is too far along to rebuild the entire project, but not far enough along to where I care about losing the migrations I have thus far. I also don't mind losing the data in the database. I was trying to rollback to the beginning but some of it is failing.
I know this is a bad state to be in, but lesson learned.
EDIT:
I just deleted all the migrations files and rebuilt the schema file with db:schema:dump.
I assume this puts me in a clean state with my existing database, just lost migrations.
if you want to migrate some steps back you can
rake db:rollback STEP=2
That command will migrate your database 2 migrations back.
If you need more help with rake commands, jus type
rake -T
That command will list all the tasks you have in you application.
If you are not concerned about losing data then do
rake db:purge
It should just drop your database
Your schema.rb file should contain the actual schema from your database. You could use it as a starting point to create you migrations. You could create a new migration for each table with the :force => true parameter to overwrite the old table. Afterwards you could just delete the old migrations (you would probably also need to delete their entries from schema_migrations table).
Another options would be just updating the old migrations to match your current schema.

exactly what does rake db:migrate do?

Does rake db:migrate only add new migrations, or does it drop all migrations/changes and build everything new?
I think rake is throwing an error because it is trying to access a table attribute in migration 040 that was deleted in migration 042. somehow my DB and rake are out of synch and I want to fix them.
for you experts out there - is it common for rake to get out of synch with migrations? how can I avoid this (no, I do not hand-edit my schema or rake files).
When you use rails migrations, a table called schema_migrations is automatically created, which keeps track of what migrations have been applied, by storing the version number of each migration (this is the number that prefaces the migration name in the file name, ie db/migrate/_20090617111204__migration.rb). When you run rake db:migrate to migrate up, only migrations which have not been run previously (ie. their version is not contained in the table) will be run (for this reason, changing a migration that's already been executed will have no effect when running db:migrate). When migrating down, all versions found in schema_migrations that are greater than the version you are rolling back to will be undone.
Everytime you create a migration using scripts (like script/generate model ...) a new migration is added to the correct directory ready to be synched with the real database.
Actually rake db:migrate just checks which missing migrations still need to be applied to the database without caring about the previouse ones.
Of course if you modify the database using other ways is common to obtain out-of-synch things because as you said you can find yourself applying a migration to something that is changed underneath.
A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined. Caution: by doing so your data will be lost.

Resources