Ruby on rails rubyonrails中的rake函数

Ruby on rails rubyonrails中的rake函数,ruby-on-rails,rake,Ruby On Rails,Rake,我是RubyonRails的新手。我对rake有怀疑创建功能是创建一个新的数据库。之后,我们要运行一些命令,如 rake db:load rake db:data:load rake db:schema:load rake db:migrate rake db:seed 但是为什么我们要在创建数据库之后运行这个cmds,以及关于cmd的函数是什么 谢谢你的建议 您可以使用rake-T获取每个任务的描述: $ rake -T | grep db rake db:create

我是RubyonRails的新手。我对rake有怀疑<代码>创建功能是创建一个新的数据库。之后,我们要运行一些命令,如

rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed
但是为什么我们要在创建数据库之后运行这个cmds,以及关于cmd的函数是什么


谢谢你的建议

您可以使用
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
我知道你在问什么

编辑:

您可以在此处阅读有关迁移用途的更多信息:

编辑2:

rake db:migrate
允许您以“正常”的方式更新数据库模式:您可以创建一个新的迁移(阅读指南!)并添加一个新列,例如,添加索引、重命名列等。迁移允许您在“时间”内来回“旅行”——您可以运行迁移并在以后回滚它

生成新迁移时:

$rails g migration将_new _column _添加到_some _table
以后,您将能够运行
rake db:migrate
,以应用于所需的更改(当然,您必须编写生成的迁移的主体)

不过,我强烈建议您阅读指南:)

编辑3:

add_column:users,:price,:float
,例如,将
price
列添加到
users
表中,列的类型将是
float
float
不是存储货币相关物品的最佳方法!)。默认情况下,此列将为
NULL

编辑4:


有关运行了哪些迁移的信息存储在
schema_migrations
表中:首次运行迁移将在此表中添加一条新记录,其中包含此迁移的
version
(日期+文件名中的一些随机数)。回滚迁移将删除此记录。运行两次迁移不会产生任何效果。

您可以使用
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
我知道你在问什么

编辑:

您可以在此处阅读有关迁移用途的更多信息:

编辑2:

rake db:migrate
允许您以“正常”的方式更新数据库模式:您可以创建一个新的迁移(阅读指南!)并添加一个新列,例如,添加索引、重命名列等。迁移允许您在“时间”内来回“旅行”——您可以运行迁移并在以后回滚它

生成新迁移时:

$rails g migration将_new _column _添加到_some _table
以后,您将能够运行
rake db:migrate
,以应用于所需的更改(当然,您必须编写生成的迁移的主体)

不过,我强烈建议您阅读指南:)

编辑3:

add_column:users,:price,:float
,例如,将
price
列添加到
users
表中,列的类型将是
float
float
不是存储货币相关物品的最佳方法!)。默认情况下,此列将为
NULL

编辑4:


有关运行了哪些迁移的信息存储在
schema_migrations
表中:首次运行迁移将在此表中添加一条新记录,其中包含此迁移的
version
(日期+文件名中的一些随机数)。回滚迁移将删除此记录。运行两次迁移不会产生任何效果。

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

文字工作流程
  • 您可以创建一个空数据库
  • 您需要将第一个模型的数据表添加到数据库中。这是通过在
    db/migrate
  • 现在您需要第二个模型,因此您可以创建模型并在为您创建的
    db/migrate
    中编辑迁移文件
  • 更新数据库而不破坏任何现有数据的最简单方法是运行
    bundle exec rake db:migrate
    。这会将第二个迁移文件的内容添加到数据库中,并且不会损害现有数据
  • 创建项目后的工作流示例:
  • bundle exec rake db:create:all
  • bundle exec rails生成脚手架用户名:string电子邮件:string
  • bundle exec rake db:migrate
  • 在此阶段,
    bundle exec rails s
    并转到
    localhost:3000/users/new
    并创建一个新用户
  • bundle-exec-rails生成脚手架文章标题:字符串正文:文本
  • bundle exec rake db:migrate
  • 回到浏览器中,转到
    localhost:3000/users
    ,您仍然可以看到您创建的用户

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

    文字工作流程
  • 您可以创建一个空数据库
  • 您需要将第一个模型的数据表添加到数据库中。这是通过在
    db/migrate
  • 现在您需要第二个模型,因此您可以创建模型并在为您创建的
    db/migrate
    中编辑迁移文件
  • 更新数据库而不破坏任何现有数据的最简单方法是运行
    bundle exec rake db:migrate
    。这会将第二个迁移文件的内容添加到数据库中,并且不会损害现有数据
  • 创建项目后的工作流示例:
  • bundle exec rake db:create:all
  • bundle exec rails生成脚手架用户名:string电子邮件:string
  • bundle exec rake db:migrate
  • 在这