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?