beerdb-models 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/beerdb/models.rb +67 -0
- data/lib/beerdb/version.rb +1 -1
- data/test/helper.rb +6 -39
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a3214a36bec05faea115c2cb1bcb5dd3dff5526
|
4
|
+
data.tar.gz: c98220129e209b2f6c7d2a89227eb2d721771200
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5319080d890054876e3cbae262b1e869a2c2aa24d1c29e76dab2337652f71333f9b9a24ebda39ef2eecf9f15e56a96d3ad0d4f4008a39289d44b3412cbe1b4b
|
7
|
+
data.tar.gz: 65fe65c742cf7aa322962e4f7197408f35fda43d6851a01630155c0ac60a90536dc178815f34e74c5ea01f29945806622c24b8c84b6084b79573f39c9153ffd3
|
data/lib/beerdb/models.rb
CHANGED
@@ -99,6 +99,73 @@ module BeerDb
|
|
99
99
|
end
|
100
100
|
|
101
101
|
|
102
|
+
def self.connect( config={} )
|
103
|
+
|
104
|
+
if config.empty?
|
105
|
+
puts "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"
|
106
|
+
|
107
|
+
db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///beer.db' )
|
108
|
+
|
109
|
+
if db.scheme == 'postgres'
|
110
|
+
config = {
|
111
|
+
adapter: 'postgresql',
|
112
|
+
host: db.host,
|
113
|
+
port: db.port,
|
114
|
+
username: db.user,
|
115
|
+
password: db.password,
|
116
|
+
database: db.path[1..-1],
|
117
|
+
encoding: 'utf8'
|
118
|
+
}
|
119
|
+
else # assume sqlite3
|
120
|
+
config = {
|
121
|
+
adapter: db.scheme, # sqlite3
|
122
|
+
database: db.path[1..-1] # beer.db (Note: cut off leading /, thus 1..-1)
|
123
|
+
}
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
puts "Connecting to db using settings: "
|
128
|
+
pp config
|
129
|
+
ActiveRecord::Base.establish_connection( config )
|
130
|
+
# ActiveRecord::Base.logger = Logger.new( STDOUT )
|
131
|
+
|
132
|
+
|
133
|
+
## if sqlite3 add (use) some pragmas for speedups
|
134
|
+
if config[:adapter] == 'sqlite3'
|
135
|
+
if config[:database] == ':memory:'
|
136
|
+
## do nothing for in memory database; no pragmas needed
|
137
|
+
puts "sqlite3 - no pragmas; using in memory database"
|
138
|
+
else
|
139
|
+
puts "sqlite3 - pragmas for speedup"
|
140
|
+
con = ActiveRecord::Base.connection
|
141
|
+
con.execute( 'PRAGMA synchronous=OFF;' )
|
142
|
+
con.execute( 'PRAGMA journal_mode=OFF;' )
|
143
|
+
con.execute( 'PRAGMA temp_store=MEMORY;' )
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end # method connect
|
147
|
+
|
148
|
+
|
149
|
+
def self.setup_in_memory_db
|
150
|
+
# Database Setup & Config
|
151
|
+
|
152
|
+
config = {
|
153
|
+
adapter: 'sqlite3',
|
154
|
+
database: ':memory:'
|
155
|
+
}
|
156
|
+
|
157
|
+
pp config
|
158
|
+
|
159
|
+
ActiveRecord::Base.logger = Logger.new( STDOUT )
|
160
|
+
## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
|
161
|
+
|
162
|
+
## Note: every connect will create a new empty in memory db
|
163
|
+
ActiveRecord::Base.establish_connection( config )
|
164
|
+
|
165
|
+
## build schema
|
166
|
+
BeerDb.create_all
|
167
|
+
end
|
168
|
+
|
102
169
|
end # module BeerDb
|
103
170
|
|
104
171
|
|
data/lib/beerdb/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -20,44 +20,11 @@ Brand = BeerDb::Model::Brand
|
|
20
20
|
Brewery = BeerDb::Model::Brewery
|
21
21
|
|
22
22
|
|
23
|
-
|
24
|
-
# Database Setup & Config
|
23
|
+
BeerDb.setup_in_memory_db()
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
}
|
25
|
+
## add some counties
|
26
|
+
AT = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
|
27
|
+
W = State.create!( key: 'w', title: 'Wien', country_id: AT.id )
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
ActiveRecord::Base.logger = Logger.new( STDOUT )
|
34
|
-
## ActiveRecord::Base.colorize_logging = false - no longer exists - check new api/config setting?
|
35
|
-
|
36
|
-
## NB: every connect will create a new empty in memory db
|
37
|
-
ActiveRecord::Base.establish_connection( db_config )
|
38
|
-
|
39
|
-
|
40
|
-
## build schema
|
41
|
-
BeerDb.create_all
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
def fillup_in_memory_db
|
46
|
-
## add some counties
|
47
|
-
|
48
|
-
at = Country.create!( key: 'at', title: 'Austria', code: 'AUT', pop: 0, area: 0 )
|
49
|
-
State.create!( key: 'w', title: 'Wien', country_id: at.id )
|
50
|
-
|
51
|
-
de = Country.create!( key: 'de', title: 'Germany', code: 'DEU', pop: 0, area: 0 )
|
52
|
-
State.create!( key: 'by', title: 'Bayern', country_id: de.id )
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
setup_in_memory_db()
|
57
|
-
fillup_in_memory_db()
|
58
|
-
|
59
|
-
AT = Country.find_by!( key: 'at' )
|
60
|
-
W = State.find_by!( key: 'w' )
|
61
|
-
|
62
|
-
DE = Country.find_by!( key: 'de' )
|
63
|
-
BY = State.find_by!( key: 'by' )
|
29
|
+
DE = Country.create!( key: 'de', title: 'Germany', code: 'DEU', pop: 0, area: 0 )
|
30
|
+
BY = State.create!( key: 'by', title: 'Bayern', country_id: DE.id )
|