简体   繁体   中英

Is there a way to use schema.rb to create a database from inside Rails?

In a Rails 3.2.3 application, I have a need to allow an administrator to create a database and populate it with tables from schema.rb.

Just executing the code in schema.rb is ideal. Basically, I am looking for a way to do:

rake db:setup

without using "rake".

I have several things I'd also like to do, such as back up a database, and I'm wondering if there's a Rails DSL I can work with or if I just need to write the SQL and run it through ActiveRecord::Base.connection.execute.

Thanks for any help you can offer.

您可以尝试从您的应用运行它:

 system("rake db:setup")
rake db:setup
rake db:schema:load 

This will create a database (if not exists) and load a schema.rb into the database. If you don't want to use rake you can store schema as a raw sql and load it.

You can use backup gem for backups.

i do not know if i understand it right but

you can use seeds.rb so you can create table entries like:

Person.create(email: "tom@mail.com", firstname: "Tom", phone: "0049-0")

and then make a

rake db:seed

in the console

您可以使用'load“path / to / schema.rb”加载它,它将从当前连接打开创建数据库

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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