rake db:schema:load vs. migrations

前端 未结 7 1079
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 09:39

Very simple question here - if migrations can get slow and cumbersome as an app gets more complex and if we have the much cleaner rake db:schema:load to call in

相关标签:
7条回答
  • 2020-11-27 10:05

    I have already posted as a comment, but feels it is better to put the comments of the db/schema.rb file here:

    # Note that this schema.rb definition is the authoritative source for your
    # database schema. If you need to create the application database on another
    # system, you should be using db:schema:load, not running all the migrations
    # from scratch. The latter is a flawed and unsustainable approach (the more migrations
    # you'll amass, the slower it'll run and the greater likelihood for issues).
    #
    # It's strongly recommended that you check this file into your version control system.
    

    Actually, my experience is that it is better to put the migration files in git and not the schema.rb file...

    0 讨论(0)
  • 2020-11-27 10:07

    rake db:migrate setup the tables in the database. When you run the migration command, it will look in db/migrate/ for any ruby files and execute them starting with the oldest. There is a timestamp at the beginning of each migration filename.

    Unlike rake db:migrate that runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema.rbinto the database.

    You can find out more about rake database commands here.

    0 讨论(0)
  • 2020-11-27 10:12

    Migrations provide forward and backward step changes to the database. In a production environment, incremental changes must be made to the database during deploys: migrations provide this functionality with a rollback failsafe. If you run rake db:schema:load on a production server, you'll end up deleting all your production data. This is a dangerous habit to get into.

    That being said, I believe it is a decent practice to occasionally "collapse" migrations. This entails deleting old migrations, replacing them with a single migration (very similar to your schema.rb file) and updating the schema_migrations table to reflect this change. Be very careful when doing this! You can easily delete your production data if you aren't careful.

    As a side note, I strongly believe that you should never put data creation in the migration files. The seed.rb file can be used for this, or custom rake or deploy tasks. Putting this into migration files mixes your database schema specification with your data specification and can lead to conflicts when running migration files.

    0 讨论(0)
  • 2020-11-27 10:15

    Just stumbled across this post, that was long ago and didn't see the answer I was expecting.

    rake db:schema:load is great for the first time you put a system in production. After that you should run migrations normally.

    This also helps you cleaning your migrations whenever you like, since the schema has all the information to put other machines in production even when you cleaned up your migrations.

    0 讨论(0)
  • 2020-11-27 10:17

    As a user of other ORM's, it always seemed strange to me that Rails didn't have a 'sync and update' feature. ie, by using the schema file (which represents the entire, up-to-date schema), go through the existing DB structure and add/remove tables, columns, indexes as required.

    To me this would be a lot more robust, even if possibly a little slower.

    0 讨论(0)
  • 2020-11-27 10:22

    Migrations lets you add data to the db too. but db:schema:load only loads the schema .

    0 讨论(0)
提交回复
热议问题