- Write a installer application in rails which will take database username, password and database name
- Creates brand new database
- Populate the database with schema and reference data.
- Updates database.yml file
- Reinitialize the rails activerecord connection context, so that user does not have to restart the application
# Create new database
ActiveRecord::Base.connection.create_database("rex_boy") #rex_boy new database name.
# Load new database with schema and data.
.........This you can do either by using writing Rake migrate tasks or manually write SQL's to load the schema and data, 1000's of ways to do this. I did using creating using migrations and invoking rake command from rails.
# Read and update the database.yml file with newly created database
## Read the Yml and update with new database name
@config = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) @config['development']['database']= "rex_boy"
## Now update the yml file back. basically write the database.yml back from @config object
File.open( "#{RAILS_ROOT}/config/database.yml", 'w' ) do |f| f << @config.to_yaml
# Now the interesting part to reintialize ActiveRecord connection context without restarting the server (Apache or WEBrick or Lighttpd..)
ActiveRecord::Base.configurations = @config ActiveRecord::Base.establish_connection ActiveRecord::Base.connection
This should get you very fancy in terms of building rails based installer application.