Class: Mihari::Database

Inherits:
Object
  • Object
show all
Extended by:
Memist::Memoizable
Defined in:
lib/mihari/database.rb

Class Method Summary collapse

Class Method Details

.closeObject

Close DB connection(s)



202
203
204
205
206
# File 'lib/mihari/database.rb', line 202

def close
  return unless ActiveRecord::Base.connected?

  ActiveRecord::Base.clear_active_connections!
end

.connectObject

Establish DB connection



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mihari/database.rb', line 179

def connect
  return if ActiveRecord::Base.connected?

  case adapter
  when "postgresql", "mysql2"
    ActiveRecord::Base.establish_connection(Mihari.config.database)
  else
    ActiveRecord::Base.establish_connection(
      adapter: adapter,
      database: Mihari.config.database
    )
  end

  ActiveRecord::Base.logger = Logger.new($stdout) if development_env?

  migrate :up
rescue StandardError
  # Do nothing
end

.destroy!Object

Destory DB



211
212
213
214
215
# File 'lib/mihari/database.rb', line 211

def destroy!
  return unless ActiveRecord::Base.connected?

  migrate :down
end

.migrate(direction) ⇒ Object

DB migraration

Parameters:

  • direction (Symbol)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mihari/database.rb', line 157

def migrate(direction)
  ActiveRecord::Migration.verbose = false

  [
    InitialSchema,
    AddeSourceToArtifactSchema,
    EnrichmentsSchema,
    EnrichmentCreatedAtSchema,
    # v4.0
    RuleSchema,
    AddeMetadataToArtifactSchema,
    # v4.4
    AddYAMLToRulesSchema,
    # v4.5
    EnrichmentsV45Schema
  ].each { |schema| schema.migrate direction }
end