Module: DbAgent

Defined in:
lib/db_agent.rb,
lib/db_agent/seeder.rb,
lib/db_agent/webapp.rb,
lib/db_agent/db_handler.rb,
lib/db_agent/table_orderer.rb,
lib/db_agent/viewpoint/base.rb,
lib/db_agent/db_handler/mssql.rb,
lib/db_agent/db_handler/mysql.rb,
lib/db_agent/viewpoint/delegate.rb,
lib/db_agent/viewpoint/typecheck.rb,
lib/db_agent/db_handler/postgresql.rb

Defined Under Namespace

Modules: Viewpoint Classes: DbHandler, Seeder, TableOrderer, Webapp

Constant Summary collapse

VERSION =

Current version of DbAgent

"3.0.0"
ROOT_FOLDER =

Root folder of the project structure

if ENV['DBAGENT_ROOT_FOLDER']
  _!(ENV['DBAGENT_ROOT_FOLDER'])
else
  Path.backfind('.[Gemfile]') or raise("Missing Gemfile")
end
LOGGER =

Logger instance to use

Logger.new(STDOUT)

Class Method Summary collapse

Class Method Details

._!(path) ⇒ Object

Simply checks that a path exists of raise an error



13
14
15
16
17
# File 'lib/db_agent.rb', line 13

def self._!(path)
  Path(path).tap do |p|
    raise "Missing #{p.basename}." unless p.exists?
  end
end

.default_configObject

What database configuration to use for normal access



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
# File 'lib/db_agent.rb', line 31

def self.default_config
  cfg = {
    adapter:  ENV['DBAGENT_ADAPTER']  || 'postgres',
    port:     ENV['DBAGENT_PORT']     || 5432,
    database: ENV['DBAGENT_DB']       || 'suppliers-and-parts',
    user:     ENV['DBAGENT_USER']     || 'dbagent',
    password: ENV['DBAGENT_PASSWORD'] || 'dbagent',
    test:     false
  }

  # Favor a socket approach if specified, otherwise fallback to
  # host with default to postgres
  if socket = ENV['DBAGENT_SOCKET']
    cfg[:socket] = socket
  else
    cfg[:host] = ENV['DBAGENT_HOST'] || 'localhost'
  end

  # Set a logger if explicitly requested
  if ENV['DBAGENT_LOGSQL'] == 'yes'
    cfg[:loggers] = [LOGGER]
  end

  cfg
end

.default_handlerObject



67
68
69
70
71
72
73
# File 'lib/db_agent.rb', line 67

def self.default_handler
  DbHandler.factor({
    config: default_config,
    superconfig: default_superconfig,
    root: ROOT_FOLDER
  })
end

.default_superconfigObject

What database configuration to use for superuser access



58
59
60
61
62
63
64
65
# File 'lib/db_agent.rb', line 58

def self.default_superconfig
  cfg = default_config
  cfg.merge({
    user:     ENV['DBAGENT_SUPER_USER']     || cfg[:user],
    database: ENV['DBAGENT_SUPER_DB']       || cfg[:database],
    password: ENV['DBAGENT_SUPER_PASSWORD'] || cfg[:password]
  })
end