0

I'm failing at finding out how to create a database from ActiveRecord if one doesn't already exist. I've looked at this: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#method-i-create_database, but it doesn't want to play nice, as far as I can tell... I'm not sure what I'm missing here. Is this even possible?

Thanks!

2
  • The question says without Rails, but you cite a Rails page. You don't explain quite what you've tried or what you mean by "it doesn't want to play nice." Commented May 7, 2012 at 3:20
  • I'm using Goliath and ActiveRecord. I couldn't be more specific at the time because I really didn't know what was going on. I apologize for being too brief before. Commented May 14, 2012 at 20:46

1 Answer 1

4

I managed to cobble together this thing that works with both heroku and on my local machine, and will create a database if one doesn't exist.

# ./lib/db_connect.rb

require 'erb'
require 'uri'
require 'em-synchrony/activerecord'

class DbConnect
  attr_accessor :config
  def initialize
    @db = URI.parse(ENV['DATABASE_URL'] || 'http://localhost')
    if @db.scheme == 'postgres' # This section makes Heroku work
      ActiveRecord::Base.establish_connection(
        :adapter  => @db.scheme == 'postgres' ? 'postgresql' : @db.scheme,
        :host     => @db.host,
        :username => @db.user,
        :password => @db.password,
        :database => @db.path[1..-1],
        :encoding => 'utf8'
      )
    else # And this is for my local environment
      environment = ENV['DATABASE_URL'] ? 'production' : 'development'
      @db = YAML.load(ERB.new(File.read('config/database.yml')).result)[environment]
      ActiveRecord::Base.establish_connection(@db)
      @config = ActiveRecord::Base.connection.pool.spec.config
    end
  end
end

# connect to the database
DbConnect.new

And here's a Rakefile that uses it.

# ./Rakefile

$: << './lib'
require 'db_connect'

db = DbConnect.new
config = db.config

desc 'create the database'
task :create_db do
  unless ENV['DATABASE_URL'].present?
    ActiveRecord::Base.establish_connection adapter:'postgresql',username:config[:username],password:config[:password], database: 'postgres'
    ActiveRecord::Base.connection.create_database config[:database]
  else
    raise StandardError, "You cannot do this on Heroku!"
  end
end

desc 'create the users table'
task :create_users_table do
  ActiveRecord::Schema.define(:version => 0) do
    create_table "users", :force => true do |t|
      t.string   "first_name"
      t.string   "last_name"
      t.datetime "created_at", :null => false
      t.datetime "updated_at", :null => false
    end
  end
end

Problem solved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.