Class: NcsNavigator::Warehouse::DatabaseInitializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ncs_navigator/warehouse/database_initializer.rb

Overview

Performs configuration of the two DataMapper repositories used by the warehouse. If you need to access the warehouse from your application/script, do something like this first:

# Require the appropriate model version for the warehouse
# you're using
require 'ncs_navigator/warehouse/models/two_point_zero'
DatabaseInitializer.new(Configuration.new).set_up_repository

This sets up the :default DataMapper repository to point to the reporting database for your current NcsNavigator.env.

You can also set up connections to the working database if needed; see below.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DatabaseInitializer

Returns a new instance of DatabaseInitializer.



34
35
36
# File 'lib/ncs_navigator/warehouse/database_initializer.rb', line 34

def initialize(config)
  @configuration = config
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



30
31
32
# File 'lib/ncs_navigator/warehouse/database_initializer.rb', line 30

def configuration
  @configuration
end

Instance Method Details

#clone_working_to_reportingtrue, false

Replaces the reporting database with a clone of the working database. This method relies on the command line pg_dump and pg_restore commands. In addition, you must also have previously called #set_up_repository with :both as the argument.

Returns:

  • (true, false)

    whether the clone succeeded.

See Also:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ncs_navigator/warehouse/database_initializer.rb', line 148

def clone_working_to_reporting
  PostgreSQL::Pgpass.new.tap do |pgpass|
    pgpass.update params(:working)
    pgpass.update params(:reporting)
  end

  shell.say 'Cloning working schema into reporting schema...'

  dump_file = Tempfile.new('wh_clone_dump')

  dump_cmd = escape_cmd([
    configuration.pg_bin('pg_dump'),
    pg_params(params(:working)),
    '--format=custom',
    "--file=#{dump_file.path}",
    params(:working)['database']
  ].flatten)

  shell.clear_line_then_say(
    "Cloning working to reporting... dumping working database to temporary file")
  log.debug("Dump command: #{dump_cmd}")
  unless system(dump_cmd)
    shell.say(
      "\nClone from working to reporting failed. See above for detail.\n")
    log.error('Dump failed.')
    return false
  end

  list_file = Tempfile.new('wh_clone_list')
  list_cmd = escape_cmd([
    configuration.pg_bin('pg_restore'),
    pg_params(params(:working)),
    '--list',
    "--file=#{list_file.path}",
    dump_file.path
  ].flatten)

  shell.clear_line_then_say(
    "Cloning working to reporting... extracting dump contents")
  log.debug("List command: #{list_cmd}")
  unless system(list_cmd)
    shell.say(
      "\nClone from working to reporting failed. See above for detail.\n")
    log.error('Dump failed.')
    return false
  end

  shell.clear_line_then_say(
    "Cloning working to reporting... filtering content list")
  list_file.rewind
  selection_file = Tempfile.new('wh_clone_selection')
  list_file.read.split("\n").each do |line|
    if line =~ %r{#{params(:working)['username']}$}
      selection_file.puts line
    end
  end

  restore_cmd = escape_cmd([
    configuration.pg_bin('pg_restore'),
    pg_params(params(:reporting)),
    "--use-list=#{selection_file.path}",
    '--dbname', params(:reporting)['database'],
    dump_file.path
  ].flatten)

  drop_all(:reporting, :quiet => true)

  shell.clear_line_then_say(
    "Cloning working to reporting... loading filtered dump into reporting database")
  log.debug("Restore command: #{restore_cmd.inspect}")
  if system(restore_cmd)
    shell.clear_line_then_say("Successfully cloned working to reporting.\n")
    log.info('Clone succeeded.')
    return true
  else
    shell.say(
      "\nClone from working to reporting failed. See above for detail.\n")
    log.error('Dump failed.')
    return false
  end
ensure
  dump_file.unlink if dump_file
  list_file.unlink if list_file
  selection_file.unlink if selection_file
end

#replace_schema

This method returns an undefined value.

Drops and rebuilds the schema in the working database. The models for the version of the database you're working with must already be loaded and finalized. You must also have previously called #set_up_repository with :working or :both as the argument.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ncs_navigator/warehouse/database_initializer.rb', line 82

def replace_schema
  drop_all(:working)

  shell.say "Loading MDES models..."
  log.info "Initializing schema for MDES #{configuration.mdes.specification_version}"
  shell.clear_line_then_say("Creating schema in working database...")
  ::DataMapper.auto_migrate!

  shell.clear_line_then_say(
    "Added #{configuration.models_module.mdes_order.size} MDES tables.\n")

  create_no_pii_views
end

#set_up_repository(mode = :reporting, prefix = "mdes_warehouse")

This method returns an undefined value.

Configure the DataMapper repositories for the warehouse using the appropriate bcdatabase configurations.

Parameters:

  • mode (:reporting, :working, :both) (defaults to: :reporting)

    which repositories to set up. In general, the working database should only be needed internal to the warehouse's scripts. The :default repo will be the working repo unless only the reporting repo is configured in.



48
49
50
51
52
53
54
55
56
# File 'lib/ncs_navigator/warehouse/database_initializer.rb', line 48

def set_up_repository(mode=:reporting, prefix="mdes_warehouse")
  fail "Invalid mode #{mode.inspect}" unless [:reporting, :working, :both].include?(mode)
  modes = case mode
          when :both then [:working, :reporting]
          else [mode]
          end
  connect_one(modes.first, :default)
  modes.each { |m| connect_one(m, [prefix, m].join('_').to_sym) }
end