简体   繁体   English

rake db:迁移,源和目标

[英]rake db:migrate, source and destination

I'm new to ruby and mysql. 我是红宝石和mysql的新手。

I was told to execute two commands below: 我被告知执行以下两个命令:

  1. mysql -u root; then CREATE DATABASE sd

  2. rake db:migrate . rake db:migrate

The sd database was empty when created. sd数据库在创建时为空。 After I ran the 2nd command, sd is full of items. 运行第二个命令后,sd充满了所有项目。

I'm wondering how rake knows that the destination is sd and what the source is. 我想知道rake如何知道目的地是sd以及来源是什么。

I know that there are some scripts under db/migrate folder, so I guess rake knows who the destination is from such newly-created(I assume, 'cause I'm new to ruby) scripts. 我知道db / migrate文件夹下有一些脚本,所以我猜rake知道目标是来自于这些新创建的脚本(我想,因为我是ruby的新手)。 But how about the source? 但是来源呢?

Thanks! 谢谢!

It looks like you are trying to work with a Rails application. 看来您正在尝试使用Rails应用程序。 I would suggest looking at config/database.yml. 我建议看config / database.yml。 That file holds configuration data for test, development, and production environments. 该文件包含用于测试,开发和生产环境的配置数据。 By default, rails uses the development environment. 默认情况下,Rails使用开发环境。 You will likely find the answer in config/database.yml under development section. 您可能会在config / database.yml的“开发”部分下找到答案。

  • Duck

All information about your database is on the file config/database.yml . 有关数据库的所有信息都在文件config/database.yml This doc can help you: http://guides.rubyonrails.org/configuring.html#configuring-a-database 该文档可以为您提供帮助: http : //guides.rubyonrails.org/configuring.html#configuring-a-database

And you can change this: 您可以更改此:

mysql -u root; then CREATE DATABASE sd

for that: 为了那个原因:

rake db:create

The source of the data will normally be controlled by the db/migrate/*.rb files as you are aware of. 如您所知,数据源通常将由db / migrate / *。rb文件控制。

But they may have hooked another task onto db:migrate via the Rakefile or lib/tasks/*.rake files so "rake db:mirgrate" may also runs some extra tasks. 但是他们可能已经通过Rakefile或lib / tasks / *。rake文件将另一个任务挂接到db:migrate上,因此“ rake db:mirgrate”可能还会运行一些额外的任务。 A common task that adds seed information is the rake db:seed task, which normally runs db/seeds.rb . 添加种子信息的常见任务是rake db:seed任务,该任务通常运行db / seeds.rb。

When I use db/seeds.rb I typically put my seed data in db/fixtures/*.yml, but others may have different places. 当我使用db / seeds.rb时,我通常将种子数据放在db / fixtures / *。yml中,但其他位置可能有所不同。

on newer rails you can also use rake db:create to do the database creation (assuming the user in database.yml has sufficient privileges). 在较新的版本上,您还可以使用rake db:create进行数据库创建(假设database.yml中的用户具有足够的特权)。 rake -T db will tell you wnat tasks have been named with db in them, eg: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM