From 23ae759cbc6821e41d2b73245aa981c49574a593 Mon Sep 17 00:00:00 2001 From: mimen Date: Wed, 13 Oct 2021 11:45:50 -0700 Subject: [PATCH] standard --fix --- commands/bloat.js | 12 ++++++------ commands/blocking.js | 10 +++++----- commands/cache_hit.js | 12 ++++++------ commands/calls.js | 24 ++++++++++++------------ commands/extensions.js | 12 ++++++------ commands/fdwsql.js | 14 +++++++------- commands/index_size.js | 14 +++++++------- commands/index_usage.js | 12 ++++++------ commands/locks.js | 18 +++++++++--------- commands/long_running_queries.js | 14 +++++++------- commands/mandelbrot.js | 12 ++++++------ commands/outliers.js | 28 ++++++++++++++-------------- commands/records_rank.js | 14 +++++++------- commands/seq_scans.js | 14 +++++++------- commands/stats_reset.js | 16 ++++++++-------- commands/table_indexes_size.js | 14 +++++++------- commands/table_size.js | 14 +++++++------- commands/total_index_size.js | 12 ++++++------ commands/total_table_size.js | 14 +++++++------- commands/unused_indexes.js | 14 +++++++------- commands/user_connections.js | 32 ++++++++++++++++---------------- commands/vacuum_stats.js | 14 +++++++------- index.js | 10 +++++----- lib/util.js | 20 ++++++++++---------- 24 files changed, 185 insertions(+), 185 deletions(-) diff --git a/commands/bloat.js b/commands/bloat.js index 1d02f14..8eb57a7 100644 --- a/commands/bloat.js +++ b/commands/bloat.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` WITH constants AS ( SELECT current_setting('block_size')::numeric AS bs, 23 AS hdr, 4 AS ma ), bloat_info AS ( @@ -70,7 +70,7 @@ FROM ORDER BY raw_waste DESC, bloat DESC ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -79,10 +79,10 @@ const cmd = { description: 'show table and index bloat in your database ordered by most wasteful', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'bloat'}, cmd) + Object.assign({ command: 'bloat' }, cmd) ] diff --git a/commands/blocking.js b/commands/blocking.js index eb966a7..887286d 100644 --- a/commands/blocking.js +++ b/commands/blocking.js @@ -22,8 +22,8 @@ WHERE NOT bl.granted ` function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let output = yield pg.psql.exec(db, query) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -32,10 +32,10 @@ const cmd = { description: 'display queries holding locks other queries are waiting to be released', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'blocking'}, cmd) + Object.assign({ command: 'blocking' }, cmd) ] diff --git a/commands/cache_hit.js b/commands/cache_hit.js index 8879eeb..93637f6 100644 --- a/commands/cache_hit.js +++ b/commands/cache_hit.js @@ -17,8 +17,8 @@ FROM pg_statio_user_tables; ` function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let output = yield pg.psql.exec(db, query) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -27,11 +27,11 @@ const cmd = { description: 'show index and table hit rate', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'cache-hit'}, cmd), - Object.assign({command: 'cache_hit', hidden: true}, cmd) + Object.assign({ command: 'cache-hit' }, cmd), + Object.assign({ command: 'cache_hit', hidden: true }, cmd) ] diff --git a/commands/calls.js b/commands/calls.js index d10a773..93a2a31 100644 --- a/commands/calls.js +++ b/commands/calls.js @@ -6,23 +6,23 @@ const pg = require('@heroku-cli/plugin-pg-v5') const util = require('../lib/util') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) yield util.ensurePGStatStatement(db) - let truncatedQueryString = context.flags.truncate + const truncatedQueryString = context.flags.truncate ? 'CASE WHEN length(query) <= 40 THEN query ELSE substr(query, 0, 39) || \'…\' END' : 'query' - let newTotalExecTimeField = yield util.newTotalExecTimeField(db) - let totalExecTimeField = `` + const newTotalExecTimeField = yield util.newTotalExecTimeField(db) + let totalExecTimeField = '' if (newTotalExecTimeField) { - totalExecTimeField = "total_exec_time" + totalExecTimeField = 'total_exec_time' } else { - totalExecTimeField = "total_time" + totalExecTimeField = 'total_time' } - let query = ` + const query = ` SELECT ${truncatedQueryString} AS qry, interval '1 millisecond' * ${totalExecTimeField} AS exec_time, to_char((${totalExecTimeField}/sum(${totalExecTimeField}) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time, @@ -32,7 +32,7 @@ FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usena ORDER BY calls DESC LIMIT 10 ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -41,13 +41,13 @@ const cmd = { description: 'show 10 queries that have longest execution time in aggregate', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], + args: [{ name: 'database', optional: true }], flags: [ - {name: 'truncate', char: 't', description: 'truncate queries to 40 characters'} + { name: 'truncate', char: 't', description: 'truncate queries to 40 characters' } ], - run: cli.command({preauth: true}, co.wrap(run)) + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'calls'}, cmd) + Object.assign({ command: 'calls' }, cmd) ] diff --git a/commands/extensions.js b/commands/extensions.js index efb0964..e2bb837 100644 --- a/commands/extensions.js +++ b/commands/extensions.js @@ -5,13 +5,13 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT * FROM pg_available_extensions WHERE name IN (SELECT unnest(string_to_array(current_setting('extwlist.extensions'), ','))) ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -20,10 +20,10 @@ const cmd = { description: 'list available and installed extensions', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'extensions'}, cmd) + Object.assign({ command: 'extensions' }, cmd) ] diff --git a/commands/fdwsql.js b/commands/fdwsql.js index f08fc79..f6e149f 100644 --- a/commands/fdwsql.js +++ b/commands/fdwsql.js @@ -33,10 +33,10 @@ ORDER BY c.relname; function * run (context, heroku) { const app = context.app - const {prefix, database} = context.args + const { prefix, database } = context.args - let db = yield pg.fetcher(heroku).database(app, database) - let addon = yield pg.fetcher(heroku).addon(app, database) + const db = yield pg.fetcher(heroku).database(app, database) + const addon = yield pg.fetcher(heroku).addon(app, database) yield util.ensureNonStarterPlan(addon) cli.log('CREATE EXTENSION IF NOT EXISTS postgres_fdw;') cli.log(`DROP SERVER IF EXISTS ${prefix}_db;`) @@ -58,12 +58,12 @@ const cmd = { needsApp: true, needsAuth: true, args: [ - {name: 'prefix'}, - {name: 'database', optional: true} + { name: 'prefix' }, + { name: 'database', optional: true } ], - run: cli.command({preauth: true}, co.wrap(run)) + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'fdwsql'}, cmd) + Object.assign({ command: 'fdwsql' }, cmd) ] diff --git a/commands/index_size.js b/commands/index_size.js index d700564..24eca06 100644 --- a/commands/index_size.js +++ b/commands/index_size.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT c.relname AS name, pg_size_pretty(sum(c.relpages::bigint*8192)::bigint) AS size FROM pg_class c @@ -19,7 +19,7 @@ GROUP BY c.relname ORDER BY sum(c.relpages) DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -28,11 +28,11 @@ const cmd = { description: 'show the size of indexes, descending by size', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'index-size'}, cmd), - Object.assign({command: 'index_size', hidden: true}, cmd) + Object.assign({ command: 'index-size' }, cmd), + Object.assign({ command: 'index_size', hidden: true }, cmd) ] diff --git a/commands/index_usage.js b/commands/index_usage.js index 4001634..4c1e451 100644 --- a/commands/index_usage.js +++ b/commands/index_usage.js @@ -18,8 +18,8 @@ SELECT relname, ` function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let output = yield pg.psql.exec(db, query) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -28,11 +28,11 @@ const cmd = { description: 'calculates your index hit rate (effective databases are at 99% and up)', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'index-usage'}, cmd), - Object.assign({command: 'index_usage', hidden: true}, cmd) + Object.assign({ command: 'index-usage' }, cmd), + Object.assign({ command: 'index_usage', hidden: true }, cmd) ] diff --git a/commands/locks.js b/commands/locks.js index de16315..18436b8 100644 --- a/commands/locks.js +++ b/commands/locks.js @@ -5,10 +5,10 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let truncatedQueryString = prefix => { - let column = `${prefix}query` + const truncatedQueryString = prefix => { + const column = `${prefix}query` if (context.flags.truncate) { return `CASE WHEN length(${column}) <= 40 THEN ${column} ELSE substr(${column}, 0, 39) || '…' END` } else { @@ -16,7 +16,7 @@ function * run (context, heroku) { } } - let query = ` + const query = ` SELECT pg_stat_activity.pid, pg_class.relname, @@ -33,7 +33,7 @@ function * run (context, heroku) { AND pg_stat_activity.pid <> pg_backend_pid() order by query_start; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -42,11 +42,11 @@ const cmd = { description: 'display queries with active locks', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - flags: [{name: 'truncate', char: 't', description: 'truncates queries to 40 charaters'}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + flags: [{ name: 'truncate', char: 't', description: 'truncates queries to 40 charaters' }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'locks'}, cmd) + Object.assign({ command: 'locks' }, cmd) ] diff --git a/commands/long_running_queries.js b/commands/long_running_queries.js index ff3f060..c773b74 100644 --- a/commands/long_running_queries.js +++ b/commands/long_running_queries.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT pid, now() - pg_stat_activity.query_start AS duration, @@ -22,7 +22,7 @@ ORDER BY now() - pg_stat_activity.query_start DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -31,11 +31,11 @@ const cmd = { description: 'show all queries longer than five minutes by descending duration', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'long-running-queries'}, cmd), - Object.assign({command: 'long_running_queries', hidden: true}, cmd) + Object.assign({ command: 'long-running-queries' }, cmd), + Object.assign({ command: 'long_running_queries', hidden: true }, cmd) ] diff --git a/commands/mandelbrot.js b/commands/mandelbrot.js index 518dfbd..dc3a485 100644 --- a/commands/mandelbrot.js +++ b/commands/mandelbrot.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` WITH RECURSIVE Z(IX, IY, CX, CY, X, Y, I) AS ( SELECT IX, IY, X::float, Y::float, X::float, Y::float, 0 FROM (select -2.2 + 0.031 * i, i from generate_series(0,101) as i) as xgen(x,ix), @@ -29,7 +29,7 @@ GROUP BY IY ORDER BY IY ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -38,10 +38,10 @@ const cmd = { description: 'show the mandelbrot set', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'mandelbrot'}, cmd) + Object.assign({ command: 'mandelbrot' }, cmd) ] diff --git a/commands/outliers.js b/commands/outliers.js index 4cbb3e0..0d52edd 100644 --- a/commands/outliers.js +++ b/commands/outliers.js @@ -6,7 +6,7 @@ const pg = require('@heroku-cli/plugin-pg-v5') const util = require('../lib/util') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) yield util.ensurePGStatStatement(db) @@ -15,7 +15,7 @@ function * run (context, heroku) { return } - let truncatedQueryString = context.flags.truncate + const truncatedQueryString = context.flags.truncate ? 'CASE WHEN length(query) <= 40 THEN query ELSE substr(query, 0, 39) || \'…\' END' : 'query' @@ -28,15 +28,15 @@ function * run (context, heroku) { } } - let newTotalExecTimeField = yield util.newTotalExecTimeField(db) - let totalExecTimeField = `` + const newTotalExecTimeField = yield util.newTotalExecTimeField(db) + let totalExecTimeField = '' if (newTotalExecTimeField) { - totalExecTimeField = "total_exec_time" + totalExecTimeField = 'total_exec_time' } else { - totalExecTimeField = "total_time" + totalExecTimeField = 'total_time' } - let query = ` + const query = ` SELECT interval '1 millisecond' * ${totalExecTimeField} AS total_exec_time, to_char((${totalExecTimeField}/sum(${totalExecTimeField}) OVER()) * 100, 'FM90D0') || '%' AS prop_exec_time, to_char(calls, 'FM999G999G999G990') AS ncalls, @@ -47,7 +47,7 @@ ORDER BY ${totalExecTimeField} DESC LIMIT ${limit} ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -56,15 +56,15 @@ const cmd = { description: 'show 10 queries that have longest execution time in aggregate', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], + args: [{ name: 'database', optional: true }], flags: [ - {name: 'reset', description: 'resets statistics gathered by pg_stat_statements'}, - {name: 'truncate', char: 't', description: 'truncate queries to 40 characters'}, - {name: 'num', char: 'n', description: 'the number of queries to display (default: 10)', hasValue: true} + { name: 'reset', description: 'resets statistics gathered by pg_stat_statements' }, + { name: 'truncate', char: 't', description: 'truncate queries to 40 characters' }, + { name: 'num', char: 'n', description: 'the number of queries to display (default: 10)', hasValue: true } ], - run: cli.command({preauth: true}, co.wrap(run)) + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'outliers'}, cmd) + Object.assign({ command: 'outliers' }, cmd) ] diff --git a/commands/records_rank.js b/commands/records_rank.js index 11503de..e47c753 100644 --- a/commands/records_rank.js +++ b/commands/records_rank.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT relname AS name, n_live_tup AS estimated_count @@ -17,7 +17,7 @@ ORDER BY n_live_tup DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -26,11 +26,11 @@ const cmd = { description: 'show all tables and the number of rows in each ordered by number of rows descending', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'records-rank'}, cmd), - Object.assign({command: 'records_rank', hidden: true}, cmd) + Object.assign({ command: 'records-rank' }, cmd), + Object.assign({ command: 'records_rank', hidden: true }, cmd) ] diff --git a/commands/seq_scans.js b/commands/seq_scans.js index 732d205..4fca0ad 100644 --- a/commands/seq_scans.js +++ b/commands/seq_scans.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT relname AS name, seq_scan as count FROM @@ -15,7 +15,7 @@ FROM ORDER BY seq_scan DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -24,11 +24,11 @@ const cmd = { description: 'show the count of sequential scans by table descending by order', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'seq-scans'}, cmd), - Object.assign({command: 'seq_scans', hidden: true}, cmd) + Object.assign({ command: 'seq-scans' }, cmd), + Object.assign({ command: 'seq_scans', hidden: true }, cmd) ] diff --git a/commands/stats_reset.js b/commands/stats_reset.js index d124770..23063e8 100644 --- a/commands/stats_reset.js +++ b/commands/stats_reset.js @@ -7,12 +7,12 @@ const util = require('../lib/util') function * run (context, heroku) { const app = context.app - const {database} = context.args + const { database } = context.args - let db = yield pg.fetcher(heroku).addon(app, database) + const db = yield pg.fetcher(heroku).addon(app, database) yield util.ensureNonStarterPlan(db) - let host = pg.host(db) - let rsp = yield heroku.put(`/client/v11/databases/${db.name}/stats_reset`, {host}) + const host = pg.host(db) + const rsp = yield heroku.put(`/client/v11/databases/${db.name}/stats_reset`, { host }) cli.log(rsp.message) } @@ -21,11 +21,11 @@ const cmd = { description: 'calls the Postgres functions pg_stat_reset()', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'stats-reset'}, cmd), - Object.assign({command: 'stats_reset', hidden: true}, cmd) + Object.assign({ command: 'stats-reset' }, cmd), + Object.assign({ command: 'stats_reset', hidden: true }, cmd) ] diff --git a/commands/table_indexes_size.js b/commands/table_indexes_size.js index 5e86359..6962478 100644 --- a/commands/table_indexes_size.js +++ b/commands/table_indexes_size.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT c.relname AS table, pg_size_pretty(pg_indexes_size(c.oid)) AS index_size FROM pg_class c @@ -18,7 +18,7 @@ AND c.relkind='r' ORDER BY pg_indexes_size(c.oid) DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -27,11 +27,11 @@ const cmd = { description: 'show the total size of all the indexes on each table, descending by size', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'table-indexes-size'}, cmd), - Object.assign({command: 'table_indexes_size', hidden: true}, cmd) + Object.assign({ command: 'table-indexes-size' }, cmd), + Object.assign({ command: 'table_indexes_size', hidden: true }, cmd) ] diff --git a/commands/table_size.js b/commands/table_size.js index e93afc6..f72b016 100644 --- a/commands/table_size.js +++ b/commands/table_size.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT c.relname AS name, pg_size_pretty(pg_table_size(c.oid)) AS size FROM pg_class c @@ -18,7 +18,7 @@ AND c.relkind='r' ORDER BY pg_table_size(c.oid) DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -27,11 +27,11 @@ const cmd = { description: 'show the size of the tables (excluding indexes), descending by size', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'table-size'}, cmd), - Object.assign({command: 'table_size', hidden: true}, cmd) + Object.assign({ command: 'table-size' }, cmd), + Object.assign({ command: 'table_size', hidden: true }, cmd) ] diff --git a/commands/total_index_size.js b/commands/total_index_size.js index 3f59440..46d62f3 100644 --- a/commands/total_index_size.js +++ b/commands/total_index_size.js @@ -14,8 +14,8 @@ AND c.relkind='i'; ` function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let output = yield pg.psql.exec(db, query) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -24,11 +24,11 @@ const cmd = { description: 'show the total size of all indexes in MB', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'total-index-size'}, cmd), - Object.assign({command: 'total_index_size', hidden: true}, cmd) + Object.assign({ command: 'total-index-size' }, cmd), + Object.assign({ command: 'total_index_size', hidden: true }, cmd) ] diff --git a/commands/total_table_size.js b/commands/total_table_size.js index 845cf9f..b845f06 100644 --- a/commands/total_table_size.js +++ b/commands/total_table_size.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT c.relname AS name, pg_size_pretty(pg_total_relation_size(c.oid)) AS size FROM pg_class c @@ -18,7 +18,7 @@ AND c.relkind='r' ORDER BY pg_total_relation_size(c.oid) DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -27,11 +27,11 @@ const cmd = { description: 'show the size of the tables (including indexes), descending by size', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'total-table-size'}, cmd), - Object.assign({command: 'total_table_size', hidden: true}, cmd) + Object.assign({ command: 'total-table-size' }, cmd), + Object.assign({ command: 'total_table_size', hidden: true }, cmd) ] diff --git a/commands/unused_indexes.js b/commands/unused_indexes.js index cd740f1..17ae055 100644 --- a/commands/unused_indexes.js +++ b/commands/unused_indexes.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` SELECT schemaname || '.' || relname AS table, indexrelname AS index, @@ -20,7 +20,7 @@ ORDER BY pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST, pg_relation_size(i.indexrelid) DESC; ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -34,11 +34,11 @@ where the planner will almost invariably select a sequential scan, but may not in the future as the table grows`, needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'unused-indexes'}, cmd), - Object.assign({command: 'unused_indexes', hidden: true}, cmd) + Object.assign({ command: 'unused-indexes' }, cmd), + Object.assign({ command: 'unused_indexes', hidden: true }, cmd) ] diff --git a/commands/user_connections.js b/commands/user_connections.js index 1264b32..4cf74c4 100644 --- a/commands/user_connections.js +++ b/commands/user_connections.js @@ -8,18 +8,18 @@ const util = require('../lib/util') function * run (context, heroku) { const app = context.app - const {database} = context.args + const { database } = context.args - let db = yield pg.fetcher(heroku).addon(app, database) + const db = yield pg.fetcher(heroku).addon(app, database) yield util.ensureNonStarterPlan(db) - let host = pg.host(db) + const host = pg.host(db) - let credentials = yield heroku.get(`/postgres/v0/databases/${db.name}/credentials`, { host: host }) - let defaultCredentials = _.filter(credentials, c => c.name === 'default') - let defaultUsers = _.flatMap(defaultCredentials, c => _.map(c.credentials, u => u.user)) + const credentials = yield heroku.get(`/postgres/v0/databases/${db.name}/credentials`, { host: host }) + const defaultCredentials = _.filter(credentials, c => c.name === 'default') + const defaultUsers = _.flatMap(defaultCredentials, c => _.map(c.credentials, u => u.user)) - let isDefaultUser = (user) => _.includes(defaultUsers, user) - let styledName = (user) => { + const isDefaultUser = (user) => _.includes(defaultUsers, user) + const styledName = (user) => { if (isDefaultUser(user)) { return 'default' } else { @@ -27,12 +27,12 @@ function * run (context, heroku) { } } - let rsp = yield heroku.get(`/client/v11/databases/${db.name}/user_connections`, {host}) - let connections = _.map(rsp.connections, function (v, k) { - return {credential: styledName(k), count: v} + const rsp = yield heroku.get(`/client/v11/databases/${db.name}/user_connections`, { host }) + const connections = _.map(rsp.connections, function (v, k) { + return { credential: styledName(k), count: v } }) - cli.table(connections, {columns: [ {key: 'credential', label: 'Credential'}, {key: 'count', label: 'Connections'} ]}) + cli.table(connections, { columns: [{ key: 'credential', label: 'Credential' }, { key: 'count', label: 'Connections' }] }) } const cmd = { @@ -40,11 +40,11 @@ const cmd = { description: 'returns the number of connections per credential', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'user-connections'}, cmd), - Object.assign({command: 'user_connections', hidden: true}, cmd) + Object.assign({ command: 'user-connections' }, cmd), + Object.assign({ command: 'user_connections', hidden: true }, cmd) ] diff --git a/commands/vacuum_stats.js b/commands/vacuum_stats.js index c2aaf42..cdef82e 100644 --- a/commands/vacuum_stats.js +++ b/commands/vacuum_stats.js @@ -5,9 +5,9 @@ const cli = require('heroku-cli-util') const pg = require('@heroku-cli/plugin-pg-v5') function * run (context, heroku) { - let db = yield pg.fetcher(heroku).database(context.app, context.args.database) + const db = yield pg.fetcher(heroku).database(context.app, context.args.database) - let query = ` + const query = ` WITH table_opts AS ( SELECT pg_class.oid, relname, nspname, array_to_string(reloptions, '') AS relopts @@ -48,7 +48,7 @@ FROM ORDER BY 1 ` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) process.stdout.write(output) } @@ -57,11 +57,11 @@ const cmd = { description: 'show dead rows and whether an automatic vacuum is expected to be triggered', needsApp: true, needsAuth: true, - args: [{name: 'database', optional: true}], - run: cli.command({preauth: true}, co.wrap(run)) + args: [{ name: 'database', optional: true }], + run: cli.command({ preauth: true }, co.wrap(run)) } module.exports = [ - Object.assign({command: 'vacuum-stats'}, cmd), - Object.assign({command: 'vacuum_stats', hidden: true}, cmd) + Object.assign({ command: 'vacuum-stats' }, cmd), + Object.assign({ command: 'vacuum_stats', hidden: true }, cmd) ] diff --git a/index.js b/index.js index 3c22ab4..72fae04 100644 --- a/index.js +++ b/index.js @@ -4,10 +4,10 @@ const _ = require('lodash') const path = require('path') const fs = require('fs') -let dir = path.join(__dirname, 'commands') +const dir = path.join(__dirname, 'commands') exports.commands = _.chain(fs.readdirSync(dir)) -.filter(f => path.extname(f) === '.js') -.map(f => require('./commands/' + f)) -.flatten() -.value() + .filter(f => path.extname(f) === '.js') + .map(f => require('./commands/' + f)) + .flatten() + .value() diff --git a/lib/util.js b/lib/util.js index 9439b15..85799ff 100644 --- a/lib/util.js +++ b/lib/util.js @@ -4,12 +4,12 @@ const co = require('co') const pg = require('@heroku-cli/plugin-pg-v5') function * ensurePGStatStatement (db) { - let query = ` + const query = ` SELECT exists( SELECT 1 FROM pg_extension e LEFT JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname='pg_stat_statements' AND n.nspname = 'public' ) AS available` - let output = yield pg.psql.exec(db, query) + const output = yield pg.psql.exec(db, query) if (!output.includes('t')) { throw new Error(`pg_stat_statements extension need to be installed in the public schema first. @@ -21,26 +21,26 @@ You can install it by running: function * ensureNonStarterPlan (db) { if (db.plan.name.match(/(dev|basic)$/)) { - throw new Error(`This operation is not supported by Hobby tier databases.`) + throw new Error('This operation is not supported by Hobby tier databases.') } } function * newTotalExecTimeField (db) { - let newTotalExecTimeFieldQuery = `SELECT current_setting('server_version_num')::numeric >= 130000` - let newTotalExecTimeFieldRaw = yield pg.psql.exec(db, newTotalExecTimeFieldQuery) + const newTotalExecTimeFieldQuery = 'SELECT current_setting(\'server_version_num\')::numeric >= 130000' + const newTotalExecTimeFieldRaw = yield pg.psql.exec(db, newTotalExecTimeFieldQuery) // error checks - let newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n") + let newTotalExecTimeField = newTotalExecTimeFieldRaw.split('\n') if (newTotalExecTimeField.length != 6) { - throw new Error(`Unable to determine database version`) + throw new Error('Unable to determine database version') } - newTotalExecTimeField = newTotalExecTimeFieldRaw.split("\n")[2].trim() + newTotalExecTimeField = newTotalExecTimeFieldRaw.split('\n')[2].trim() - if (newTotalExecTimeField != "t" && newTotalExecTimeField != "f") { + if (newTotalExecTimeField != 't' && newTotalExecTimeField != 'f') { throw new Error(`Unable to determine database version, expected "t" or "f", got: "${newTotalExecTimeField}"`) } - return newTotalExecTimeField == "t" + return newTotalExecTimeField == 't' } module.exports = {