从当前工作树状态重新初始化 Rails 个开发和测试数据库
Reinitialize Rails development and test databases from the current working tree state
当您使用 rails 和 运行 从 Rake 说 db:schema:dump
或 db:structure:dump
时,它将把您的开发数据库的状态写到模式文件中。当我们 运行 测试时,db:test:prepare
也会复制开发数据库的当前状态——这在某种程度上是有道理的,因为您可能正在开发一项需要对当前分支进行数据库调整的功能。
但是,我们经常需要将开发数据库重置为顺序执行所有迁移可以达到的状态,然后将测试数据库重置为符合该状态-然后写出schema/structure 可以提交到生产分支的源代码树。目前,我们使用 shell 命令的这个怪物来做到这一点:
bundle exec rake db:drop && bundle exec rake db:create && \
bundle exec rake log db:migrate > /tmp/miglog.log && \
bundle exec rake db:schema:dump && \
RAILS_ENV=test bundle exec rake db:drop && \
RAILS_ENV=test bundle exec rake db:create && bundle exec rake
db:test:prepare
这看起来很糟糕,绝对不可读而且非常慢,因为这里的每次调用都会重新启动 Rails env。
这就是我想知道的:我们可以从此命令中删除一些步骤吗?我们可以将它们折叠成手写的 Rake 任务吗?我认为这会很困难,因为 Rails 环境根据 RAILS_ENV
配置不同,但我希望至少删除 Rails env 重新加载。
你需要重置数据库我真的建议你使用这个:
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config)
然后
rake db:migrate
你还有一个重设数据库的 rake 任务:
rake db:reset
因为我已经发布了 here
至于测试,我强烈建议您使用工厂 (FactoryGirl)and use DatabaseCleaner。
您可以将其简化为
bundle exec rake db:migrate:reset
bundle exec rake db:test:prepare
重置任务 运行删除、创建、迁移。 运行 迁移无论如何都会转储架构(除非您已将 dump_schema_after_migration
更改为 false)
如您所知,test:prepare
步骤会转储和加载模式,还会进行清除(稍微取决于数据库适配器,但基本上会删除并重新创建数据库)。鉴于您刚刚转储了架构,您可以将其更改为
bundle exec rake db:migrate:reset
bundle exec rake db:test:load_schema
最后,db:test 中的任务基本上知道它们应该只 运行 针对测试数据库 - 不管 rails 环境如何,它们在需要时使用测试数据库,所以你可以只是做
bundle exec rake db:migrate:reset db:test:load_schema
最后,有些人还会告诉您 运行ning 从头开始迁移是异端邪说,最终的真实来源是 schema.rb(由您的生产环境生成)
当您使用 rails 和 运行 从 Rake 说 db:schema:dump
或 db:structure:dump
时,它将把您的开发数据库的状态写到模式文件中。当我们 运行 测试时,db:test:prepare
也会复制开发数据库的当前状态——这在某种程度上是有道理的,因为您可能正在开发一项需要对当前分支进行数据库调整的功能。
但是,我们经常需要将开发数据库重置为顺序执行所有迁移可以达到的状态,然后将测试数据库重置为符合该状态-然后写出schema/structure 可以提交到生产分支的源代码树。目前,我们使用 shell 命令的这个怪物来做到这一点:
bundle exec rake db:drop && bundle exec rake db:create && \
bundle exec rake log db:migrate > /tmp/miglog.log && \
bundle exec rake db:schema:dump && \
RAILS_ENV=test bundle exec rake db:drop && \
RAILS_ENV=test bundle exec rake db:create && bundle exec rake
db:test:prepare
这看起来很糟糕,绝对不可读而且非常慢,因为这里的每次调用都会重新启动 Rails env。
这就是我想知道的:我们可以从此命令中删除一些步骤吗?我们可以将它们折叠成手写的 Rake 任务吗?我认为这会很困难,因为 Rails 环境根据 RAILS_ENV
配置不同,但我希望至少删除 Rails env 重新加载。
你需要重置数据库我真的建议你使用这个:
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config)
然后
rake db:migrate
你还有一个重设数据库的 rake 任务:
rake db:reset
因为我已经发布了 here
至于测试,我强烈建议您使用工厂 (FactoryGirl)and use DatabaseCleaner。
您可以将其简化为
bundle exec rake db:migrate:reset
bundle exec rake db:test:prepare
重置任务 运行删除、创建、迁移。 运行 迁移无论如何都会转储架构(除非您已将 dump_schema_after_migration
更改为 false)
如您所知,test:prepare
步骤会转储和加载模式,还会进行清除(稍微取决于数据库适配器,但基本上会删除并重新创建数据库)。鉴于您刚刚转储了架构,您可以将其更改为
bundle exec rake db:migrate:reset
bundle exec rake db:test:load_schema
最后,db:test 中的任务基本上知道它们应该只 运行 针对测试数据库 - 不管 rails 环境如何,它们在需要时使用测试数据库,所以你可以只是做
bundle exec rake db:migrate:reset db:test:load_schema
最后,有些人还会告诉您 运行ning 从头开始迁移是异端邪说,最终的真实来源是 schema.rb(由您的生产环境生成)