text: 'use strict'
  
  const co = require('co')
  const cli = require('heroku-cli-util')
  const pg = require('@heroku-cli/plugin-pg-v5')
  
  function * run (context, heroku) {
    const db = yield pg.fetcher(heroku).database(context.app, context.args.database)
  
    const query = `
  SELECT
    relname AS name,
    n_live_tup AS estimated_count
  FROM
    pg_stat_user_tables
  ORDER BY
    n_live_tup DESC;
  `
  
    const output = yield pg.psql.exec(db, query)
    process.stdout.write(output)
  }
  
  const cmd = {
    topic: 'pg',
    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))
  }
  
  module.exports = [
    Object.assign({ command: 'records-rank' }, cmd),
    Object.assign({ command: 'records_rank', hidden: true }, cmd)
  ]
  