-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdebug_connect.rb
More file actions
105 lines (72 loc) · 2.88 KB
/
debug_connect.rb
File metadata and controls
105 lines (72 loc) · 2.88 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
###
# for changes
# see https://www.bigbinary.com/blog/rails-6-changed-activerecord-base-configurations-result-to-an-object
#
# https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/DatabaseConfigurations.html
require 'active_record'
require 'logutils'
puts
puts ActiveRecord::VERSION::STRING
puts
def connect( config={} )
LogUtils::Logger.root.level = :debug
logger = LogUtils::Logger.root
if config.empty? # use/try DATABASE_URL from environment
logger.debug "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"
db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///pluto.db' )
if db.scheme == 'postgres'
config = {
adapter: 'postgresql',
host: db.host,
port: db.port,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: 'utf8'
}
else # assume sqlite3
config = {
adapter: db.scheme, # sqlite3
database: db.path[1..-1] # pluto.db (NB: cut off leading /, thus 1..-1)
}
end
end # if config.nil?
logger.info 'db settings:'
logger.info config.pretty_inspect
### for dbbrowser and other tools add to ActiveRecord
if ActiveRecord::Base.configurations.nil? # todo/check: can this ever happen? remove?
logger.debug "ActiveRecord configurations nil - set to empty hash"
ActiveRecord::Base.configurations = {} # make it an empty hash
end
## todo/fix: remove debug? option - why? why not?
## (just) use logger level eg. logger.debug
logger.debug 'ar configurations (before):'
logger.debug ActiveRecord::Base.configurations.pretty_inspect
configs = ActiveRecord::Base.configurations
###
## undefined method `[]' for #<ActiveRecord::DatabaseConfigurations:0x000001be2558fe78 @configurations=[]>
## before 6+
## pp configs['pluto']
## configs['pluto'] = config
pp configs.configs_for(env_name: 'pluto' )
logger.debug 'ar configurations (after):'
logger.debug ActiveRecord::Base.configurations.pretty_inspect
ActiveRecord::Base.establish_connection( config )
## checkd default - possible?
pp ActiveRecord::Base.connection_db_config
## results in:
# #<ActiveRecord::DatabaseConfigurations::HashConfig:0x000001361c442d68
# @configuration_hash={:adapter=>"sqlite3", :database=>":memory:"},
# @env_name="default_env",
# @name="primary">
#
# note - autoadded env_name is default_env
# and name is primary
logger.debug 'ar configurations (after establish_connection):'
logger.debug ActiveRecord::Base.configurations.pretty_inspect
end # method connect
## try connect
config = { adapter: 'sqlite3',
database: ':memory:'}
connect( config )
puts "bye"