How to generate migration files using schema.rb - ruby-on-rails

Suppose I have a Big and Ugly schema.rb, and no migrations, what is the best way of creating them, besides doing this with ctr+c/crt+v. Plus I need them to be considered as allready migrated :). Consider that this need to be generated in rails 2.3.5 env :(.

Generate a migration, then copy schema.rb code (without the ActiveRecord::Schema.define(:version => x) do and end) into the up or change method.

The only option you have is to generate the database from your schema.rb file using the command rake db:schema:load. Just be careful as rake db:schema:load will delete data on your production server.
If you look at the schema.rb file you will notice it only has the timestamp of the last migration, which is the version of the schema, and the commands required to replicate it. You can create a single migration from it, and make all the future changes from there.

Related

Schema file ruby on rails

I've dropped my table successfully from the database from the console, using
ActiveRecord::Migration.drop_table(:foo),
But for some reason the table data still shows in the schema file.
I would like a method to do this without having to manually remove the data, (Doesn't give a proper rails feeling to it.)
The schema file located in db/schema.rb was most likely generated the very last time you ran rake db:migrate. This is because rake db:migrate migrates your db through all of the migrations in your db/migrate folder, and then it calls the db:schema:dump task, which generates the schema file based on the current state of the database.
For your situation, after you have run ActiveRecord::Migration.drop_table(:foo) in the rails console, the current state of your database (now, no longer having the table foo) is not in sync with the schema.rb file that was generated when you last migrated.
What you want to do at this point is re-dump the schema based on the current state of the database, and you do this by running rake db:schema:dump. This will take the current state of your database (without the table foo) and generate your db/schema.rb file.
For more information on the schema file and its relationship to migrations, I would recommend looking at the Rails Guide on Active Record Migrations: Schema Dumping and You.
While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: link
First enter rails console:
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:foo)
Where :foo is the table name. And done, worked for me!

Build db:rake task from existing Database

I have a project that has had alot of editing to the postgresql database.
I am tring to find out is there a way to build a new db:rake file so i can rebuild the database on new server easily. Without manually editing the db:rake files.
thanks
Ruby 1.9.3
CentOS 6.4
Ruby on Rails 3
postgresql 9.3
For clarity, I am assuming you are referring to the migration files when you mention db:rake files.
In Rails, you don't want to rebuild the database using migration files. They can quickly become outdated, referring to things that no longer exist, etc. Instead, it is best practice to recreate the database using your schema.rb file instead. This is touched on an answer I wrote a while back. In short, the schema.rb file represents your database table structure; a snapshot, if you will. Note that it does not contain data, only the table structure.
In your scenario, in order to create, or ensure your schema.rb is up to date, you simply need to dump the schema, like so:
rake db:schema:dump
This regenerates the schema.rb file. Then, to rebuild the database from this file in another environment, simply reload the schema, like so:
rake db:schema:load
As stated above, the schema.rb does not contain any data from the tables, it merely represents the structure. If you want to preload the database with initial / default values, you will want to use the seed.rb file. Simply write your .create statements, and run it like so:
rake db:seed

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

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
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.
There's no need to do this. For new installations you should be running rake db:schema:load, not rake db:migrate, this will load the schema into the database, which is faster than running all the migrations.
You should never delete migrations, and certainly not combine them. As for accidentally deleting one, you should be using a version control system, such as Git.

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.

Resources