简体   繁体   English

如何在不初始化Rails的情况下从schema.rb创建数据库?

[英]How to create database from schema.rb without initializing Rails?

I am trying to create all my tables from schema.rb 我正在尝试从schema.rb创建所有表

I used the command: "rake db:schema:load" 我使用了命令:“rake db:schema:load”

However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty) 但是,这会失败,因为在我的一个初始化程序中,它引用的模型/表显然不存在(因为数据库为空)

I could comment out these lines, and then run schema:load again, but is there an alternative? 我可以注释掉这些行,然后运行schema:再次加载,但有替代方法吗?

Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. 可能最快的方法是将有问题的初始化程序移动到应用程序之外的临时目录,然后运行模式加载。 But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load: 但是,如果这不起作用,或者由于某种原因不是一个选项,你可以通过创建一个简单的rails rails app来进行架构加载来解决这个问题:

  1. Create a new rails app: rails new (app)-fixer 创建一个新的rails应用程序: rails new (app)-fixer
  2. Copy your gemfile (unless there are specific exceptions) to the fixer app. 将您的gemfile (除非有特定的例外)复制到修复程序应用程序。
  3. Copy your database.yml config to the fixer app. database.yml配置复制到修复程序应用程序。
  4. Copy your schema.rb file to the fixer app. schema.rb文件复制到修复程序应用程序。
  5. Do all appropriate "bundle install" commands as needed for your app. 根据您的应用需要执行所有适当的“捆绑安装”命令。
  6. Then run "rake db:drop db:create db:schema:load" 然后运行"rake db:drop db:create db:schema:load"

That will build up a new database from scratch, based on your current schema. 这将根据您当前的架构从头开始构建一个新数据库。

You can add a check for the table existance in your initializer. 您可以在初始化程序中添加对表存在的检查。

if TheModel.table_exists?
  // do something with the model
end

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

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