Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
beanieboi committed Feb 2, 2021
1 parent 41aff2f commit 42574ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
17 changes: 2 additions & 15 deletions commands/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,9 @@ function * run (context, heroku) {
? 'CASE WHEN length(query) <= 40 THEN query ELSE substr(query, 0, 39) || \'…\' END'
: 'query'

let newTotalExecTimeFieldQuery = `SELECT current_setting('server_version_num')::numeric >= 130000`
let newTotalExecTimeFieldRaw = yield pg.psql.exec(db, newTotalExecTimeFieldQuery)

// error checks
let newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")
if (newTotalExecTimeField.length != 6) {
throw new Error(`Unable to determine database version`)
}
newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")[2].trim()

if (newTotalExecTimeField != "t" && newTotalExecTimeField != "f") {
throw new Error(`Unable to determine database version, expected "t" or "f", got: "${newTotalExecTimeField}"`)
}

let newTotalExecTimeField = yield util.newTotalExecTimeField(db)
let totalExecTimeField = ``
if (newTotalExecTimeField == "t") {
if (newTotalExecTimeField) {
totalExecTimeField = "total_exec_time"
} else {
totalExecTimeField = "total_time"
Expand Down
17 changes: 2 additions & 15 deletions commands/outliers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,9 @@ function * run (context, heroku) {
}
}

let newTotalExecTimeFieldQuery = `SELECT current_setting('server_version_num')::numeric >= 130000`
let newTotalExecTimeFieldRaw = yield pg.psql.exec(db, newTotalExecTimeFieldQuery)

// error checks
let newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")
if (newTotalExecTimeField.length != 6) {
throw new Error(`Unable to determine database version`)
}
newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")[2].trim()

if (newTotalExecTimeField != "t" && newTotalExecTimeField != "f") {
throw new Error(`Unable to determine database version, expected "t" or "f", got: "${newTotalExecTimeField}"`)
}

let newTotalExecTimeField = yield util.newTotalExecTimeField(db)
let totalExecTimeField = ``
if (newTotalExecTimeField == "t") {
if (newTotalExecTimeField) {
totalExecTimeField = "total_exec_time"
} else {
totalExecTimeField = "total_time"
Expand Down
21 changes: 20 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ function * ensureNonStarterPlan (db) {
}
}

function * newTotalExecTimeField (db) {
let newTotalExecTimeFieldQuery = `SELECT current_setting('server_version_num')::numeric >= 130000`
let newTotalExecTimeFieldRaw = yield pg.psql.exec(db, newTotalExecTimeFieldQuery)

// error checks
let newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")
if (newTotalExecTimeField.length != 6) {
throw new Error(`Unable to determine database version`)
}
newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")[2].trim()

if (newTotalExecTimeField != "t" && newTotalExecTimeField != "f") {
throw new Error(`Unable to determine database version, expected "t" or "f", got: "${newTotalExecTimeField}"`)
}

return newTotalExecTimeField == "t"
}

module.exports = {
ensurePGStatStatement: co.wrap(ensurePGStatStatement),
ensureNonStarterPlan: co.wrap(ensureNonStarterPlan)
ensureNonStarterPlan: co.wrap(ensureNonStarterPlan),
newTotalExecTimeField: co.wrap(newTotalExecTimeField)
}

0 comments on commit 42574ae

Please sign in to comment.