rails // migrations or migrains?
Tired of using mysql clients? Click ... id, click ... int, click ... 11, (client crash!!) and so on? Why not just do all of your db creation, modification, and removal using ruby in one simple file?
That's what migrations are for. Here's a quick step through* on how to get migrations working in rails for the first timer:
In the migration file you can do cool things like creating tables, removing tables, adding columns, and the like. We're going to create a new table.
Great! Now what? Well now we want to create an actual database:
In terminal: mysql -u username -p -e "create database blog;"
Now just edit your database.yml to reflect the proper database, password, et cetera.
Next edit config/environment.rb and change this line (on line 39 for most basic installs):
# config.active_record.schema_format = :ruby
To this
config.active_record.schema_format = :ruby
Finally, back at the terminal again, type: rake migration
Watch the magic unfold...
If you don't like what it did you can always undo it. Just go back to your friendly terminal and type: rake migration VERSION=0
This allows for proper versioning. You can rake migration to any version you have. Anytime you need to add to your db or table, or remove for that matter, just make another migration. Eventually you'll find your self becoming more proficient with your db design and you'll find less versioning needed. But until that day, try this and throw your sql clients in the trash where they belong.
Hack away!
*This assumes that you are using OS X. If you are using windows simple replace terminal with command prompt.
That's what migrations are for. Here's a quick step through* on how to get migrations working in rails for the first timer:
- Open terminal
- Go to your rails project directory
- script/generate migration migration_name
- Open up your friendly editor (hint: textmate) and edit the db/migrate/001_migration_name.rb file: if you are using Text Mate you can simply type "mate db/migrate/001_migration_name.rb" to pull it up.
In the migration file you can do cool things like creating tables, removing tables, adding columns, and the like. We're going to create a new table.
class InitDbTables < ActiveRecord::Migration
def self.up
create_table :blogs do |table|
table.column :title, :string
table.column :description, :text
table.column :user_rating, :integer, :default => 0
table.column :active, :boolean, :default => true
table.column :created_by, :integer
table.column :created_on, :datetime
table.column :updated_on, :datetime
end
def self.down
drop_table :blogs
end
end
Great! Now what? Well now we want to create an actual database:
In terminal: mysql -u username -p -e "create database blog;"
Now just edit your database.yml to reflect the proper database, password, et cetera.
Next edit config/environment.rb and change this line (on line 39 for most basic installs):
# config.active_record.schema_format = :ruby
To this
config.active_record.schema_format = :ruby
Finally, back at the terminal again, type: rake migration
Watch the magic unfold...
If you don't like what it did you can always undo it. Just go back to your friendly terminal and type: rake migration VERSION=0
This allows for proper versioning. You can rake migration to any version you have. Anytime you need to add to your db or table, or remove for that matter, just make another migration. Eventually you'll find your self becoming more proficient with your db design and you'll find less versioning needed. But until that day, try this and throw your sql clients in the trash where they belong.
Hack away!
*This assumes that you are using OS X. If you are using windows simple replace terminal with command prompt.


1 Comments:
thanks.
- ben
Post a Comment
Links to this post:
Create a Link
<< Home