-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.rb
49 lines (43 loc) · 1.28 KB
/
database.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'uri'
require 'slop'
require 'mysql2'
require 'postgresql'
require 'sqlite3'
require 'active_record'
# Parses the config parameters for the database configuration and
# access credentials, as well as options. This code snipped was taken
# from https://gist.github.com/pricees/9630464 and slightly adapted.
def database_configuration
uri = URI.parse(OPTIONS[:credentials])
qs = uri.query ? Hash[URI::decode_www_form(uri.query)] : {}
ui = uri.userinfo ? uri.userinfo.split(':') : Array.new { nil }
ports = {
postgres: 5432,
mysql: 3306,
sqlite: nil
}.with_indifferent_access
encodings = {
postgres: "utf-8",
mysql: "utf8",
sqlite: "utf-8"
}.with_indifferent_access
adapters = {
postgres: "postgresql",
mysql: "mysql2",
sqlite: "sqlite3"
}.with_indifferent_access
{
encoding: qs["encoding"] || encodings[uri.scheme] ,
adapter: adapters[uri.scheme],
host: uri.host,
port: uri.port || ports[uri.scheme],
database: uri.path[1..-1],
username: ui.first,
password: ui.last,
reconnect: qs["reconnect"] || true,
pool: qs["pool"] || 5,
sslverify: OPTIONS.ssl?
}
end
# Establish a database connection
ActiveRecord::Base.establish_connection(database_configuration)