Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a migration file from schema.rb

I'm looking to generate a migration file from the schema.rb. is it possible?

I have many migration files at the moment and would like to combine everything into one master migration file.

I also think i may have accidentally deleted a migration file at some point.

thanks for any help

like image 889
hanumanDev Avatar asked Mar 07 '11 20:03

hanumanDev


People also ask

How is schema rb generated?

It is a Ruby representation of your database; schema. rb is created by inspecting the database and expressing its structure using Ruby. It is database-agnostic (i.e. whether you use SQLite, PostgreSQL, MySQL or any other database that Rails supports, the syntax and structure will remain largely the same)

What is schema rb used for?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone.

How do you create a migration?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.


1 Answers

You could copy and paste schema.rb into a migration and back-date it (e.g. change the date) so that no existing databases will run it. After you create this migration you can delete all your old migrations.

I disagree with Andrew that you should never delete migrations. Migrations break unexpectedly all the time based on model classes changing and it is very non-trivial to fix them. Since I'm sure you are using version control, you can always look back in the history if you need them for reference.

like image 106
ghempton Avatar answered Sep 23 '22 16:09

ghempton