class Lexicon::Common::Database::Database
Attributes
connection[R]
@return [PG::Connection]
search_path[R]
@return [Array<String>]
verbose[W]
Public Class Methods
connect(url)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 8 def connect(url) new(PG.connect(url)) end
new(connection, verbose: false)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 17 def initialize(connection, verbose: false) @connection = connection @search_path = [] @verbose = verbose disable_notices unless verbose end
Public Instance Methods
copy_data(sql, &block)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 93 def copy_data(sql, &block) put_data = ->(d) { @connection.put_copy_data(d) } @connection.copy_data(sql) { block.call(put_data) } end
drop_schema(name, cascade: false)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 53 def drop_schema(name, cascade: false) cascade = if cascade ' CASCADE' else '' end query <<~SQL DROP SCHEMA "#{name}"#{cascade}; SQL end
ensure_schema(name)
click to toggle source
@param [#to_s] name
# File lib/lexicon/common/database/database.rb, line 79 def ensure_schema(name) query(<<~SQL) CREATE SCHEMA IF NOT EXISTS "#{name}"; SQL end
ensure_schema_empty(name)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 85 def ensure_schema_empty(name) query(<<~SQL) DROP SCHEMA IF EXISTS #{name} CASCADE; SQL ensure_schema(name) end
make_random_schema_name(prefix = 'lex')
click to toggle source
# File lib/lexicon/common/database/database.rb, line 65 def make_random_schema_name(prefix = 'lex') "#{prefix}_#{rand(0x100000000).to_s(36)}" end
on_empty_schema(base_path: [], &block)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 43 def on_empty_schema(base_path: [], &block) schema = make_random_schema_name prepend_search_path(schema, *base_path) do block.call(schema) end ensure drop_schema(schema, cascade: true) end
prepend_search_path(*parts, &block)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 33 def prepend_search_path(*parts, &block) return if block.nil? parts.each { |part| ensure_schema(part) } with_search_path(*parts, *search_path) do block.call end end
query(sql, *params, **_options)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 69 def query(sql, *params, **_options) pp sql if verbose? if params.any? @connection.exec_params(sql, params) else @connection.exec(sql) end end
transaction(&block)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 29 def transaction(&block) connection.transaction(&block) end
verbose?()
click to toggle source
# File lib/lexicon/common/database/database.rb, line 25 def verbose? @verbose end
Private Instance Methods
disable_notices()
click to toggle source
# File lib/lexicon/common/database/database.rb, line 103 def disable_notices query <<~SQL SET client_min_messages TO WARNING; SQL end
with_search_path(*path, &block)
click to toggle source
# File lib/lexicon/common/database/database.rb, line 109 def with_search_path(*path, &block) return if block.nil? begin saved_path = @search_path @search_path = path query <<~SQL SET search_path TO #{path.map { |part| "\"#{part}\"" }.join(', ')}; SQL result = block.call result ensure path = if saved_path.any? saved_path.map { |part| "\"#{part}\"" }.join(', ') else '" "' end query <<~SQL SET search_path TO #{path}; SQL @search_path = saved_path end end