首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ruby-on-rails中的rake函数

ruby-on-rails中的rake函数
EN

Stack Overflow用户
提问于 2012-12-08 21:22:12
回答 2查看 613关注 0票数 0

我是ruby-on-rails的新手。我对瑞克持怀疑态度。create函数是创建一个新数据库。在那之后,我们想运行一些命令,比如

代码语言:javascript
复制
rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed

但是为什么我们要在创建db之后运行这个cmd,以及about cmd的什么功能。

谢谢你的建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-08 21:25:04

您可以使用rake -T获取每个任务的描述:

代码语言:javascript
复制
$ 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 +文件名中的一些随机数)。回滚迁移将删除此记录。运行迁移两次不会有任何影响。

票数 2
EN

Stack Overflow用户

发布于 2012-12-08 22:31:37

简而言之,db:migrate不会破坏数据库中的现有数据。因此,通过运行迁移和rake任务,您所做更改将允许您的数据存在。

工作流程中的文字

  1. create a empty database.
  2. 你会想要为你的第一个模型添加一个数据表到数据库中。这是通过在db/migrate
  3. Now中编辑文件来完成的您需要第二个模型,所以您在db/migrate中创建模型并编辑为您创建的迁移文件
  4. 更新数据库而不破坏任何现有数据的最简单方法是运行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

  • Back转到localhost:3000/users,您应该仍会看到您创建的用户。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13777855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档