Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schema Migrations Table

In my Rails 4 app I would like to collapse my migration files into one large file (similar to schema.rb) as it's time to do some housekeeping but I'm not sure on how to access the table in the database that stores migration data so that when I run a migration I don't receive any errors/conflicts.

Question How can I access and delete the data in the table that stores migration data?

like image 831
tommyd456 Avatar asked Sep 30 '13 17:09

tommyd456


People also ask

What is schema migration table?

Show activity on this post. The schema_migrations table holds the revision numbers; with the last record being the most recently executed migration. You can just manipulate these records manually.

What is schema migration in SQL?

A schema migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version. Migrations are performed programmatically by using a schema migration tool.

What is the main advantage of performing database schema migrations?

What are the advantages of migration tools? Migrations are helpful because they allow database schemas to evolve as requirements change. They help developers plan, validate, and safely apply schema changes to their environments.


Video Answer


3 Answers

for fun, you can also manipulate these in the console by making a model class for them...

class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end 

then you can do SchemaMigration.all, SchemaMigration.last.delete, etc.

Really just a substitute for using SQL, and it is very rare that you would need to mess around at this low level… generally a bad idea but cool to see how to do it :)

like image 196
David Lowenfels Avatar answered Sep 19 '22 23:09

David Lowenfels


Another solution could be to access it through:

ActiveRecord::SchemaMigration

The answer given by David didn't work in my context.

like image 20
Peter Piper Avatar answered Sep 19 '22 23:09

Peter Piper


The schema_migrations table holds the revision numbers; with the last record being the most recently executed migration. You can just manipulate these records manually.

like image 27
steakchaser Avatar answered Sep 22 '22 23:09

steakchaser