Class: Purview::Databases::Base
Instance Attribute Summary collapse
Instance Method Summary
collapse
#logger, #logger_opts, #logger_type, #with_context_logging
#blank?, #coalesced, #filter_blank_values, #filter_nil_values, #present?, #timestamp, #with_timestamp, #zero?
#connect, #connection_opts, #with_new_connection, #with_new_or_existing_connection, #with_transaction
Constructor Details
#initialize(name, opts = {}) ⇒ Base
Returns a new instance of Base.
6
7
8
9
10
11
12
13
14
|
# File 'lib/purview/databases/base.rb', line 6
def initialize(name, opts={})
@name = name
@opts = opts
@tables = Set.new.tap do |result|
(opts[:tables] || []).each do |table|
table.database = self if result.add?(table)
end
end
end
|
Instance Attribute Details
Returns the value of attribute name.
4
5
6
|
# File 'lib/purview/databases/base.rb', line 4
def name
@name
end
|
Returns the value of attribute tables.
4
5
6
|
# File 'lib/purview/databases/base.rb', line 4
def tables
@tables
end
|
Instance Method Details
#baseline_table(table) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/purview/databases/base.rb', line 16
def baseline_table(table)
ensure_table_valid_for_database(table)
raise Purview::Exceptions::CouldNotBaseline.new(table) \
unless table_initialized?(table)
table_name = table_name(table)
with_context_logging("`baseline_table` for: #{table_name}") do
starting_timestamp = timestamp
with_table_locked(table, starting_timestamp) do
loop do
last_window = sync_table_without_lock(table, timestamp)
break if last_window.max > starting_timestamp
end
end
end
table_name
end
|
#create_index(index, opts = {}) ⇒ Object
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
|
# File 'lib/purview/databases/base.rb', line 33
def create_index(index, opts={})
ensure_index_valid_for_database(index)
table_opts = (opts)
table_name = table_name(index.table, table_opts)
index_opts = (opts)
index_columns = index.columns
index_name = index_name(
table_name,
index_columns,
index_opts
)
with_context_logging("`create_index` for: #{index_name}") do
with_new_or_existing_connection(opts) do |connection|
connection.execute(
create_index_sql(
table_name,
index_name,
index,
index_opts
)
)
end
end
index_name
end
|
#create_table(table, opts = {}) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/purview/databases/base.rb', line 59
def create_table(table, opts={})
ensure_table_valid_for_database(table)
ensure_table_metadata_exists_for_table(table)
table_opts = (opts)
table_name = table_name(table, table_opts)
with_context_logging("`create_table` for: #{table_name}") do
with_new_or_existing_connection(opts) do |connection|
connection.execute(
send(
create_table_sql_method_name(table, table_opts),
table_name,
table,
table_opts
)
)
if table_opts[:create_indices]
table.indices.each do |index|
create_index(
index,
:connection => connection,
:table => { :name => table_name }
)
end
end
end
end
table_name
end
|
#disable_table(table) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/purview/databases/base.rb', line 88
def disable_table(table)
ensure_table_valid_for_database(table)
table_name = table_name(table)
with_context_logging("`disable_table` for: #{table_name}") do
with_new_connection do |connection|
rows_affected = \
connection.execute(disable_table_sql(table)).rows_affected
raise Purview::Exceptions::CouldNotDisable.new(table) \
if zero?(rows_affected)
end
end
table_name
end
|
#drop_index(index, opts = {}) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/purview/databases/base.rb', line 102
def drop_index(index, opts={})
ensure_index_valid_for_database(index)
table_opts = (opts)
table_name = table_name(index.table, table_opts)
index_opts = (opts)
index_columns = index.columns
index_name = index_name(
table_name,
index_columns,
index_opts
)
with_context_logging("`drop_index` for: #{index_name}") do
with_new_or_existing_connection(opts) do |connection|
connection.execute(
drop_index_sql(
table_name,
index_name,
index,
index_opts
)
)
end
end
index_name
end
|
#drop_table(table, opts = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/purview/databases/base.rb', line 128
def drop_table(table, opts={})
ensure_table_valid_for_database(table)
ensure_table_metadata_absent_for_table(table)
table_opts = (opts)
table_name = table_name(table, table_opts)
with_context_logging("`drop_table` for: #{table_name}") do
with_new_connection do |connection|
connection.execute(
drop_table_sql(
table_name,
table,
table_opts
)
)
end
end
table_name
end
|
#enable_table(table, timestamp = timestamp) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/purview/databases/base.rb', line 147
def enable_table(table, timestamp=timestamp)
ensure_table_valid_for_database(table)
table_name = table_name(table)
with_context_logging("`enable_table` for: #{table_name}") do
with_new_connection do |connection|
rows_affected = \
connection.execute(enable_table_sql(table, timestamp)).rows_affected
raise Purview::Exceptions::CouldNotEnable.new(table) \
if zero?(rows_affected)
end
end
table_name
end
|
#initialize_table(table, timestamp = timestamp) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/purview/databases/base.rb', line 161
def initialize_table(table, timestamp=timestamp)
ensure_table_valid_for_database(table)
table_name = table_name(table)
with_context_logging("`initialize_table` for: #{table_name}") do
with_new_connection do |connection|
rows_affected = \
connection.execute(initialize_table_sql(table, timestamp)).rows_affected
raise Purview::Exceptions::CouldNotInitialize.new(table) \
if zero?(rows_affected)
end
end
table_name
end
|
#lock_table(table, timestamp = timestamp) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/purview/databases/base.rb', line 175
def lock_table(table, timestamp=timestamp)
ensure_table_valid_for_database(table)
table_name = table_name(table)
with_context_logging("`lock_table` for: #{table_name}") do
with_new_connection do |connection|
rows_affected = \
connection.execute(lock_table_sql(table, timestamp)).rows_affected
raise Purview::Exceptions::CouldNotLock.new(table) \
if zero?(rows_affected)
end
end
table_name
end
|
189
190
191
192
193
194
195
196
197
|
# File 'lib/purview/databases/base.rb', line 189
def sync
with_context_logging('`sync`') do
with_timestamp do |timestamp|
with_next_table(timestamp) do |table|
sync_table_with_lock(table, timestamp)
end
end
end
end
|
#sync_table(table) ⇒ Object
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/purview/databases/base.rb', line 199
def sync_table(table)
ensure_table_valid_for_database(table)
raise Purview::Exceptions::CouldNotSync.new(table) \
unless table_initialized?(table) && table_enabled?(table)
table_name = table_name(table)
with_context_logging("`sync_table` for: #{table_name}") do
sync_table_with_lock(table, timestamp)
end
table_name
end
|
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/purview/databases/base.rb', line 210
def table_metadata(table)
ensure_table_valid_for_database(table)
table_metadata = nil
table_name = table_name(table)
with_context_logging("`table_metadata` for: #{table_name}") do
with_new_connection do |connection|
row = connection.execute(table_metadata_sql(table)).rows.first
raise Purview::Exceptions::CouldNotFindTableMetadata.new(table) \
unless row
table_metadata = Purview::Structs::TableMetadata.new(row)
end
end
table_metadata
end
|
#unlock_table(table) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/purview/databases/base.rb', line 225
def unlock_table(table)
ensure_table_valid_for_database(table)
table_name = table_name(table)
with_context_logging("`unlock_table` for: #{table_name}") do
with_new_connection do |connection|
rows_affected = \
connection.execute(unlock_table_sql(table)).rows_affected
raise Purview::Exceptions::CouldNotUnlock.new(table) \
if zero?(rows_affected)
end
end
table_name
end
|