Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Please tell me if this is mad, but basically, I've created a custom rake task, and before it does its thing, it gives the user a warning message:

Warning, please back up your database before continuing

Just in case something does go wrong. But I thought this would be awesome:

Warning, please back up your database before continuing. Do you wish to create it now [Y/N]

and upon pressing Y, the existing database would be replicated.

I don't want to create to duplicate the tables, I literally want to create a new, pristine, isolated database so that their architecture is identical, just in-case something does go wrong.

I don't want any dependencies though, I only want to use libraries included with Rails. Could I have a simple example of how to create an empty database with RoR, if this is possible?

I can create a new database like this:

  require 'sqlite3'

  db = SQLite3::Database.new( "test.db" )

However, this will only work if the user is using sqlite3. I can detect which database they're using with

database_type = ActiveRecord::Base.connection.adapter_name #=> sqlite3

and then use a case statement to execute slightly different commands for each database type, but that seems a bit elaborate, and not very dynamic. (Every single database will have to be thought up before-hand).

So can I do it through Active Record?

share|improve this question
 
You know how migrations work in Rails? –  thorsten müller Oct 15 at 17:50
 
Yes, and that is an option. I'd prefer it to happen all in the rake task though. –  Crazy JIm Oct 15 at 19:01

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.