Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to revert a particular migration in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 564
    Comment on it


    Rails provides us the concept of migrations. Migrations allows us to alter the schema of our database at any time after it has been created.

    It helps user by not letting him/her write the entire SQL query & it uses Ruby DSL(Domain Specific language) instead of raw SQL queries.


    Once we have migrated all the migrations and we want to revert a particular migration, we can use the following syntax.

    rake db:migrate:down VERSION=VERSION_NUMBER
    

    Ex:-

    We migrate the following 3 migrations with the help of rake db:migrate command

    taran@taran-System-Product-Name:~/Rails_app/sample_app$ rake db:migrate
    == 20151116054305 CreateUsers: migrating ======================================
    -- create_table(:users)
       -> 0.1945s
    == 20151116054305 CreateUsers: migrated (0.1947s) =============================
    
    == 20151116060637 AddFieldsToUsers: migrating =================================
    -- add_column(:users, :name, :string)
       -> 0.5435s
    -- add_column(:users, :age, :integer)
       -> 0.3871s
    == 20151116060637 AddFieldsToUsers: migrated (0.9309s) ========================
    
    == 20151118055122 CreateOrders: migrating =====================================
    -- create_table(:orders)
       -> 0.3065s
    == 20151118055122 CreateOrders: migrated (0.3067s) ============================
    

    & now if we want to revert the migration which creates the Order table, then we can write :

    taran@taran-System-Product-Name:~/Rails_app/sample_app$ rake db:migrate:down VERSION=20151118055122
    == 20151118055122 CreateOrders: reverting =====================================
    -- drop_table(:orders)
       -> 0.0950s
    == 20151118055122 CreateOrders: reverted (0.1109s) ============================
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: