mysql - rake db :migrate, 源和目标

标签 mysql ruby rake migrate

我是 ruby​​ 和 mysql 新手。

我被告知执行以下两个命令:

  1. mysql -u root;然后创建数据库sd

  2. rake db:migrate

sd 数据库在创建时为空。在我运行第二个命令后,SD 中充满了项目。

我想知道 rake 如何知道目的地是 sd 以及来源​​是什么。

我知道 db/migrate 文件夹下有一些脚本,所以我猜 rake 知道这些新创建的脚本的目的地是谁(我假设,因为我是 ruby​​ 新手)。但是来源呢?

谢谢!

最佳答案

如您所知,数据源通常由 db/migrate/*.rb 文件控制。

但是他们可能通过 Rakefile 或 lib/tasks/*.rake 文件将另一个任务挂接到 db:migrate 上,因此“rake db:migrate”也可能运行一些额外的任务。添加种子信息的常见任务是 rake db:seed 任务,它通常运行 db/seeds.rb 。

当我使用 db/seeds.rb 时,我通常将种子数据放在 db/fixtures/*.yml 中,但其他人可能有不同的位置。

在较新的rails上,您还可以使用rake db:create来创建数据库(假设database.yml中的用户具有足够的权限)。 rake -T db 会告诉你 wnat 任务已用 db 命名,例如:

$ rake -T db
rake db:create          # Create the database from DATABASE_URL or 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 using DATABASE_URL or 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:seed_fu         # Loads seed data for the current environment.
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:test:seed       # seed the test database
rake db:version         # Retrieves the current schema version number

关于mysql - rake db :migrate, 源和目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030461/

相关文章:

ruby - 解析 Ruby 时查找数字模式

ruby-on-rails - Rake Capistrano - 如何传递动态参数来执行 shell 命令

ruby - 在 Ruby 1.9.3 中使用 %x 调用 pwd 是否存在任何已知问题?

ruby-on-rails - 删除数据库表中的所有记录

ruby-on-rails - 使用 rake for a rails 引擎将示例数据添加到数据库

php - 使用来自两个 mysql 查询的数据填充选择下拉列表

php - 使用表单将数据插入到 php 表中

php - 使用 PHP 将复选框数组中的数据插入 MySQL 表

jquery - 显示来自 MySQL 的数据,用于通过 jQuery 动态更改内容

ruby - 在 ruby​​ 中 cat/grep 和 cut 到输出文件的最有效和最简单的方法是什么