Class: AwesomeExplain::Tasks::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_explain/tasks/db.rb

Class Method Summary collapse

Class Method Details

.buildObject



6
7
8
9
10
11
12
13
# File 'lib/awesome_explain/tasks/db.rb', line 6

def self.build
  ActiveRecord::Base.establish_connection(
    AwesomeExplain::Config.instance.db_config
  )

  adapter = AwesomeExplain::Config.instance.adapter&.to_sym
  adapter == :postgres ? build_postgres_db : build_sqlite3_db
end

.build_postgres_dbObject



26
27
28
29
30
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
56
57
58
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
# File 'lib/awesome_explain/tasks/db.rb', line 26

def self.build_postgres_db
  conn = PG.connect(
    dbname: 'postgres',
    host: postgres_host,
    user: postgres_username,
    password: postgres_password
  )
  sql = <<-SQL
    select exists(
      SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower('#{AwesomeExplain::Config::POSTGRES_DEV_DBNAME}')
    );
  SQL

  if conn.exec(sql).to_a.first.dig('exists') == 'f'
    conn.exec("CREATE DATABASE #{AwesomeExplain::Config::POSTGRES_DEV_DBNAME}")
  end

  connection = ActiveRecord::Base.establish_connection(
    AwesomeExplain::Config.instance.db_config
  ).connection

  ActiveRecord::Schema.define do
    unless connection.table_exists?(:stacktraces)
      connection.create_table :stacktraces do |t|
        t.column :stacktrace, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:sidekiq_workers)
      connection.create_table :sidekiq_workers do |t|
        t.column :worker, :string
        t.column :queue, :string
        t.column :jid, :string
        t.column :params, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:delayed_jobs)
      connection.create_table :delayed_jobs do |t|
        t.column :job, :string
        t.column :queue, :string
        t.column :jid, :string
        t.column :params, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:controllers)
      connection.create_table :controllers do |t|
        t.column :name, :string
        t.column :action, :string
        t.column :path, :string
        t.column :params, :string
        t.column :session_id, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:logs)
      connection.create_table :logs do |t|
        t.column :collection, :string
        t.column :app_name, :string
        t.column :source_name, :string
        t.column :operation, :string
        t.column :collscan, :integer
        t.column :command, :string
        t.decimal :duration, precision: 8, scale: 2
        t.column :session_id, :string
        t.column :lsid, :string

        t.column :sidekiq_args, :string
        t.column :stacktrace_id, :integer
        t.column :explain_id, :integer
        t.column :controller_id, :integer
        t.column :sidekiq_worker_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:sql_queries)
      connection.create_table :sql_queries do |t|
        t.column :table_name, :string
        t.column :schema_name, :string
        t.column :app_name, :string
        t.column :source_name, :string
        t.column :operation, :string
        t.column :query, :string
        t.column :binds, :string
        t.decimal :duration, precision: 8, scale: 2
        t.column :session_id, :string
        t.column :cached, :integer
        t.column :name, :string

        t.column :sidekiq_args, :string
        t.column :stacktrace_id, :integer
        t.column :sql_explain_id, :integer
        t.column :controller_id, :integer
        t.column :sidekiq_worker_id, :integer
        t.column :delayed_job_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:explains)
      connection.create_table :explains do |t|
        t.column :collection, :string
        t.column :source_name, :string
        t.column :command, :string
        t.column :collscan, :integer
        t.column :winning_plan, :string
        t.column :winning_plan_raw, :string
        t.column :used_indexes, :string
        t.decimal :duration, precision: 8, scale: 2
        t.column :documents_returned, :integer
        t.column :documents_examined, :integer
        t.column :keys_examined, :integer
        t.column :rejected_plans, :integer
        t.column :session_id, :string
        t.column :lsid, :string
        t.column :stacktrace_id, :integer
        t.column :controller_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:sql_explains)
      connection.create_table :sql_explains do |t|
        t.column :explain_output, :string
        t.column :stacktrace_id, :integer
        t.column :controller_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:transactions)
      connection.create_table :transactions do |t|
        t.column :params, :string
        t.column :format, :string
        t.column :method, :string
        t.column :ip, :string
        t.column :stash, :string
        t.column :status, :string
        t.column :view_runtime, :string

        t.column :controller_id, :integer
        t.timestamps
      end
    end
  end
end

.build_sqlite3_dbObject



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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/awesome_explain/tasks/db.rb', line 179

def self.build_sqlite3_db
  ActiveRecord::Schema.define do
    unless connection.table_exists?(:stacktraces)
      connection.create_table :stacktraces do |t|
        t.column :stacktrace, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:sidekiq_workers)
      connection.create_table :sidekiq_workers do |t|
        t.column :worker, :string
        t.column :queue, :string
        t.column :jid, :string
        t.column :params, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:delayed_jobs)
      connection.create_table :delayed_jobs do |t|
        t.column :job, :string
        t.column :queue, :string
        t.column :jid, :string
        t.column :params, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:controllers)
      connection.create_table :controllers do |t|
        t.column :name, :string
        t.column :action, :string
        t.column :path, :string
        t.column :params, :string
        t.column :session_id, :string
        t.timestamps
      end
    end

    unless connection.table_exists?(:logs)
      connection.create_table :logs do |t|
        t.column :collection, :string
        t.column :app_name, :string
        t.column :source_name, :string
        t.column :operation, :string
        t.column :collscan, :integer
        t.column :command, :string
        t.column :duration, :double
        t.column :session_id, :string
        t.column :lsid, :string

        t.column :sidekiq_args, :string
        t.column :stacktrace_id, :integer
        t.column :explain_id, :integer
        t.column :controller_id, :integer
        t.column :sidekiq_worker_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:sql_queries)
      connection.create_table :sql_queries do |t|
        t.column :table_name, :string
        t.column :schema_name, :string
        t.column :app_name, :string
        t.column :source_name, :string
        t.column :operation, :string
        t.column :query, :string
        t.column :binds, :string
        t.column :duration, :double
        t.column :session_id, :string
        t.column :cached, :integer
        t.column :name, :string

        t.column :sidekiq_args, :string
        t.column :stacktrace_id, :integer
        t.column :sql_explain_id, :integer
        t.column :controller_id, :integer
        t.column :sidekiq_worker_id, :integer
        t.column :delayed_job_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:explains)
      connection.create_table :explains do |t|
        t.column :collection, :string
        t.column :source_name, :string
        t.column :command, :string
        t.column :collscan, :integer
        t.column :winning_plan, :string
        t.column :winning_plan_raw, :string
        t.column :used_indexes, :string
        t.column :duration, :double
        t.column :documents_returned, :integer
        t.column :documents_examined, :integer
        t.column :keys_examined, :integer
        t.column :rejected_plans, :integer
        t.column :session_id, :string
        t.column :lsid, :string
        t.column :stacktrace_id, :integer
        t.column :controller_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:sql_explains)
      connection.create_table :sql_explains do |t|
        t.column :explain_output, :string
        t.column :stacktrace_id, :integer
        t.column :controller_id, :integer
        t.timestamps
      end
    end

    unless connection.table_exists?(:transactions)
      connection.create_table :transactions do |t|
        t.column :params, :string
        t.column :format, :string
        t.column :method, :string
        t.column :ip, :string
        t.column :stash, :string
        t.column :status, :string
        t.column :view_runtime, :string

        t.column :controller_id, :integer
        t.timestamps
      end
    end
  end
end

.drop_postgres_dbObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/awesome_explain/tasks/db.rb', line 15

def self.drop_postgres_db
  conn = PG.connect(
    dbname: 'postgres',
    host: postgres_host,
    user: postgres_username,
    password: postgres_password
  )

  conn.exec("DROP DATABASE #{AwesomeExplain::Config::POSTGRES_DEV_DBNAME}")
end

.postgres_hostObject



312
313
314
# File 'lib/awesome_explain/tasks/db.rb', line 312

def self.postgres_host
  AwesomeExplain::Config.instance.postgres_host
end

.postgres_passwordObject



320
321
322
# File 'lib/awesome_explain/tasks/db.rb', line 320

def self.postgres_password
  AwesomeExplain::Config.instance.postgres_password
end

.postgres_usernameObject



316
317
318
# File 'lib/awesome_explain/tasks/db.rb', line 316

def self.postgres_username
  AwesomeExplain::Config.instance.postgres_username
end