Created
January 20, 2015 15:02
-
-
Save paulghaddad/125b3f4ca93891ec320d to your computer and use it in GitHub Desktop.
LevelUp 6: Can write a Rails migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Go back the rails project and write a migration, or paste a link to a migration you wrote. | |
Please see the second file on this Gist: sample_migration.rb. | |
2. Demonstrate how to add a unique index on two columns within a migration. | |
t.index([:column_1, :column_2], unique: true) | |
3. Explain the cases where you can or cannot modify an existing migration. | |
There are two cases where you can edit a migration. | |
- On a migration file where you have not run the migration. It's status should be "down". | |
- If the migration has been run, and it is the most recent migration, you can edit it as long as it hasn't been run in production. You first must rollback the migration, edit the migration file and then run rake db:migrate again. | |
It is not advisable to edit migrations older than the most recent migration file. Moreover, if the existing version of the migration has already been run on production machines, you should never modify existing migration files. Only if the migration has not been committed to version control or been propagated beyond the development machine should an edit occur. In all other cases, it is best to write a new migration that performs the required changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChangeBirthdayToBirthyearToArtists < ActiveRecord::Migration | |
def up | |
remove_column :artists, :birthday, :date | |
add_column :artists, :birthyear, :string | |
end | |
def down | |
remove_column :artists, :birthyear, :string | |
add_column :artists, :birthday, :date | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment