Class: Lexicon::Common::Database::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/lexicon/common/database/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, verbose: false) ⇒ Database

Returns a new instance of Database.



17
18
19
20
21
22
23
# 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

Instance Attribute Details

#search_pathArray<String> (readonly)

Returns:

  • (Array<String>)


15
16
17
# File 'lib/lexicon/common/database/database.rb', line 15

def search_path
  @search_path
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose

Parameters:

  • value

    the value to set the attribute verbose to.



13
14
15
# File 'lib/lexicon/common/database/database.rb', line 13

def verbose=(value)
  @verbose = value
end

Class Method Details

.connect(url) ⇒ Object



8
9
10
# File 'lib/lexicon/common/database/database.rb', line 8

def connect(url)
  new(PG.connect(url))
end

Instance Method Details

#copy_data(sql, &block) ⇒ Object



93
94
95
96
# 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, if_exists: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lexicon/common/database/database.rb', line 53

def drop_schema(name, cascade: false, if_exists: false)
  cascade = if cascade
              ' CASCADE'
            else
              ''
            end

  query <<~SQL
    DROP SCHEMA #{if_exists ? 'IF EXISTS ' : ''}"#{name}"#{cascade};
  SQL
end

#ensure_schema(name) ⇒ Object

Parameters:

  • name (#to_s)


79
80
81
82
83
# 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) ⇒ Object



85
86
87
88
89
90
91
# 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') ⇒ Object



65
66
67
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



69
70
71
72
73
74
75
76
# 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

#schema_exists?(schema_name) ⇒ Boolean

Parameters:

  • schema_name (#to_s)

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/lexicon/common/database/database.rb', line 112

def schema_exists?(schema_name)
  query(<<~SQL, schema_name).count > 0
    SELECT "schema_name"
    FROM "information_schema"."schemata"
    WHERE "schema_name" = $1
  SQL
end

#table_exists?(table, schema: nil) ⇒ Boolean

Parameters:

  • table (#to_s)
  • schema (#to_s | nil) (defaults to: nil)

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/lexicon/common/database/database.rb', line 101

def table_exists?(table, schema: nil)
  schema = search_path.first if schema.nil?

  query(<<~SQL, table, schema).any?
    SELECT table_name FROM information_schema.tables
    WHERE table_name = $1 AND table_schema = $2
  SQL
end

#transaction(&block) ⇒ Object



29
30
31
# File 'lib/lexicon/common/database/database.rb', line 29

def transaction(&block)
  connection.transaction(&block)
end

#verbose?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/lexicon/common/database/database.rb', line 25

def verbose?
  @verbose
end