RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12214

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.

Upvotes: 3

Views: 5236

Answers (4)

sirfilip
sirfilip

Reputation: 1095

You can load it with 'load "path/to/schema.rb" and it will create the database from the current connection opened

Upvotes: 1

user1402147
user1402147

Reputation: 111

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: "[email protected]", firstname: "Tom", phone: "0049-0")

and then make a

rake db:seed

in the console

Upvotes: 0

NARKOZ
NARKOZ

Reputation: 27911

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.

Upvotes: 2

thesis
thesis

Reputation: 2565

You can try to run it from your app:

 system("rake db:setup")

Upvotes: 5

Related Questions