ActiveRecord: autocreation of databases

This commit is contained in:
stanislaw 2012-08-03 07:12:48 +03:00 committed by Ben Mabey
parent d8be115e92
commit 6a80e36cf6
3 changed files with 59 additions and 5 deletions

View file

@ -4,10 +4,27 @@ require 'support/active_record/schema_setup'
module MySQL2Helper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def config
db_config['mysql2']
end
def create_db
establish_connection(config.merge(:database => nil))
ActiveRecord::Base.connection.drop_database config['database'] rescue nil
ActiveRecord::Base.connection.create_database config['database']
end
def establish_connection config = config
ActiveRecord::Base.establish_connection config
end
def active_record_mysql2_setup
ActiveRecord::Base.establish_connection db_config['mysql2']
create_db
establish_connection
load_schema
end

View file

@ -4,14 +4,31 @@ require 'support/active_record/schema_setup'
module MySQLHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def config
db_config['mysql']
end
def create_db
establish_connection(config.merge(:database => nil))
ActiveRecord::Base.connection.drop_database config['database'] rescue nil
ActiveRecord::Base.connection.create_database config['database']
end
def establish_connection config = config
ActiveRecord::Base.establish_connection config
end
def active_record_mysql_setup
ActiveRecord::Base.establish_connection db_config['mysql']
create_db
establish_connection
load_schema
end
def active_record_mysql_connection
def active_record_mysql_connection
ActiveRecord::Base.connection
end
end

View file

@ -6,12 +6,32 @@ module PostgreSQLHelper
# ActiveRecord::Base.logger = Logger.new(STDERR)
def config
db_config['postgres']
end
def create_db
@encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{config.inspect}"
end
end
def establish_connection config = config
ActiveRecord::Base.establish_connection(config)
end
def active_record_pg_setup
ActiveRecord::Base.establish_connection db_config['postgres']
create_db
establish_connection
load_schema
end
def active_record_pg_connection
def active_record_pg_connection
ActiveRecord::Base.connection
end
end