Module: Moose::Inventory::DB

Extended by:
DB
Included in:
DB
Defined in:
lib/moose_inventory/db/db.rb,
lib/moose_inventory/db/models.rb,
lib/moose_inventory/db/exceptions.rb

Overview

Module for DB-related functionality

Defined Under Namespace

Classes: Group, Groupvar, Host, Hostvar, MooseDBException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



19
20
21
# File 'lib/moose_inventory/db/db.rb', line 19

def db
  @db
end

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



21
22
23
# File 'lib/moose_inventory/db/db.rb', line 21

def exceptions
  @exceptions
end

#modelsObject (readonly)

Returns the value of attribute models.



20
21
22
# File 'lib/moose_inventory/db/db.rb', line 20

def models
  @models
end

Class Method Details

.initObject




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
# File 'lib/moose_inventory/db/db.rb', line 24

def self.init
  # If we allow init more than once, then the db connection is remade,
  # which changes Sequel:DATABASES[0], thereby invalidating the sequel
  # models.  This causes unexpected behavour. That is to say, because
  # of the way Sequel initializes models, this method is not idempotent.
  # In our single-shot application, this shouldn't be a problem. However,
  # our unit tests like to call init multiple times, which borks things.
  # So, we allow init only once, gated by whether @db is nil. In effect,
  # this means we pool the DB connection for the life of the application.
  # Again, not a problem for our one-shot app, but it may be an issue in
  # long-running code. Personally, I don't like this pooling regime - 
  # perhaps I'm not understanding how it's supposed to be used? 
  #
  # TODO: can the models be refreshed, to make then again valid? What if
  #       we "load" instead of "require" the models?
  # UPDATE: Nope, still borks even if we use a load.
  #
  # @db = nil            # <- fails for unit tests
  return unless @db.nil? # <- works for unit tests

  Sequel::Model.plugin :json_serializer
  connect
  create_tables

  # Make our models work
  Sequel::DATABASES[0] = @db
  require_relative 'models'
  # load( load_dir = File.join(File.dirname(__FILE__), "models.rb") )

  # For convenience
  @models = {}
  @models[:host]     = Moose::Inventory::DB::Host
  @models[:hostvar]  = Moose::Inventory::DB::Hostvar
  @models[:group]    = Moose::Inventory::DB::Group
  @models[:groupvar] = Moose::Inventory::DB::Groupvar

  @exceptions = {}
  @exceptions[:moose] = Moose::Inventory::DB::MooseDBException

end

.resetObject




89
90
91
92
93
94
# File 'lib/moose_inventory/db/db.rb', line 89

def self.reset
  fail('Database connection has not been established') if @db.nil?
  # @debug << 'reset'
  purge
  create_tables
end

.transactionObject




66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/moose_inventory/db/db.rb', line 66

def self.transaction
  fail('Database connection has not been established') if @db.nil?
  begin
    @db.transaction(savepoint: true) do
      yield
    end

  rescue @exceptions[:moose] => e
    warn 'An error occurred during a transaction, any changes have been rolled back.'

    if Moose::Inventory::Config._confopts[:trace] == true
      abort("ERROR: #{e}")
    else          
      abort("ERROR: #{e.message}")
    end

  rescue Exception => e
    warn 'An error occurred during a transaction, any changes have been rolled back.'
    raise e
  end
end