when creating a table, i use script/generate migration tablename…and
when i am done with adding fields, i type rake migrate. now what do i do
if i want to add additional fields to the table? do i just make the
modifications to the file that was created in the db directory and then
rake migrate again? if so, i did, but my changes did not appear in the
table. or do i need to generate another migration table?
Migrate applies changes, moving the database from one state to the next,
until it is at the latest.
Migrations capture a set of changes in numbered files. When you do ‘rake
migrate’ those changes are applied to your database in sequential order
and
the final number is recorded in a table called ‘schema_version’. If you
run
‘rake migrate’ again, the schema version is compared to the highest
numbered
migration. If the numbers match then the database is considered to be at
the
latest schema version and no migrations are performed.
If you have previously run a migration and you want to add columns you
have
2 choices: 1) you can reverse the previous migration or 2) you can
create a
new migration.
I tend to do #1 if I am working on some changes and I simply forgot
something and I have not yet released the new version. #2 is the primary
mode of operation for migrations.
Reversing a migration assumes you have implemented the ‘down’ method. If
you
have done this then you simple say ‘rake migrate VERSION=[some version
number]’. For example, let’s say your latest migration is
005_add_project_table and that you have already done a ‘rake migrate’.
To
revert to 4 you would do this:
ok, so i think was incorrectly using rake migrate. for every new table i
created, i created a new migration and rake migrated it. nuts.
Shrug, that’s pretty much what I do.
and now id like to create a new profiles table, do i script/generate
migrations profiles? and then in self.up do add_column somecolumnname ?
afterwards type rake migrate?
Pretty much, apart from the fact that you don’t actually create a “new”
table, merely change the existing one. And I’d probably name the
migration akin to AddStuffToProfiles, but each to his own.
and now id like to create a new profiles table, do i script/generate
I’m assuming you meant to say you’d like to create a new FIELD in the
existing profiles table (you can’t have two tables named the same). If
so,
you’re right, create a new migration