我是ruby-on-rails的新手。我对瑞克持怀疑态度。create函数是创建一个新数据库。在那之后,我们想运行一些命令,比如
rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed但是为什么我们要在创建db之后运行这个cmd,以及about cmd的什么功能。
谢谢你的建议。
发布于 2012-12-08 21:25:04
您可以使用rake -T获取每个任务的描述:
$ rake -T | grep db
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql
rake db:version # Retrieves the current schema version number这就是你要问的吗?
编辑:
您可以在此处阅读有关迁移的更多信息:http://guides.rubyonrails.org/migrations.html
EDIT 2:
rake db:migrate允许您以一种“合理”的方式更新DB模式:您可以创建一个新的迁移(请阅读指南!)并添加新列,例如,添加索引、重命名列等-迁移允许您在“时间”中来回“旅行”-您可以运行迁移并稍后回滚。
当您生成新的迁移时:
$ rails g migration add_new_column_to_some_table您稍后将能够运行rake db:migrate来应用您想要的更改(当然,您必须编写生成的迁移的主体)。
我强烈建议您阅读指南:)
EDIT 3:
例如,add_column :users, :price, :float会将price列添加到users表中,列的类型将为float (float不是存储与金钱相关的东西的最佳方法!)。默认情况下,此列将为NULL。
EDIT 4:
关于运行了哪些迁移的信息存储在schema_migrations表中:第一次运行迁移将在此表中添加一条新记录,其中包含本次迁移的日期( version +文件名中的一些随机数)。回滚迁移将删除此记录。运行迁移两次不会有任何影响。
发布于 2012-12-08 22:31:37
简而言之,db:migrate不会破坏数据库中的现有数据。因此,通过运行迁移和rake任务,您所做更改将允许您的数据存在。
工作流程中的文字
create a empty database. db/migratedb/migrate中创建模型并编辑为您创建的迁移文件bundle exec rake db:migrate。这会将第二个迁移文件的内容添加到数据库中,而不会损害现有数据。创建项目后的工作流程示例:
在此阶段,转到bundle exec rails s并转到localhost:3000/users/new,然后在浏览器中创建一个新的user.
bundle exec rails generate scaffold Posts title:string body:text
bundle exec rake db:migrate
localhost:3000/users,您应该仍会看到您创建的用户。https://stackoverflow.com/questions/13777855
复制相似问题