Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate pg:psql to oclif/core #2855

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/cli/src/commands/pg/psql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args} from '@oclif/core'
import {database} from '../../lib/pg/fetcher'
import {exec, execFile, interactive} from '../../lib/pg/psql'

export default class Psql extends Command {
static description = 'open a psql shell to the database';
static flags = {
command: flags.string({char: 'c', description: 'SQL command to run'}),
file: flags.string({char: 'f', description: 'SQL file to run'}),
credential: flags.string({description: 'credential to use'}),
app: flags.app({required: true}),
remote: flags.remote(),
};

static args = {
database: Args.string(),
};

static aliases = ['psql']

public async run(): Promise<void> {
const {flags, args} = await this.parse(Psql)
const {app, command, credential, file} = flags
const namespace = credential ? `credential:${credential}` : undefined
let db
try {
db = await database(this.heroku, app, args.database, namespace)
} catch (error) {
if (namespace && error instanceof Error && error.message === "Couldn't find that addon.") {
throw new Error("Credential doesn't match, make sure credential is attached")
}

throw error
}

console.error(`--> Connecting to ${color.yellow(db.attachment.addon.name)}`)
if (command) {
const output = await exec(db, command)
process.stdout.write(output)
} else if (file) {
const output = await execFile(db, file)
process.stdout.write(output)
} else {
await interactive(db)
}
}
}
53 changes: 53 additions & 0 deletions packages/cli/test/unit/commands/pg/psql.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {stdout, stderr} from 'stdout-stderr'
import {expect} from 'chai'
import * as sinon from 'sinon'
import * as proxyquire from 'proxyquire'
import runCommand from '../../../helpers/runCommand'
import * as psql from '../../../../src/lib/pg/psql'

const db = {
user: 'jeff', password: 'pass', database: 'mydb', port: 5432, host: 'localhost', attachment: {
addon: {
name: 'postgres-1',
}, config_vars: ['DATABASE_URL'], app: {name: 'myapp'},
},
}

const fetcher = {
database: () => db,
}
const {default: Cmd} = proxyquire('../../../../src/commands/pg/psql', {
'../../lib/pg/fetcher': fetcher,
})
describe('psql', function () {
let stub: sinon.SinonStub

afterEach(function () {
stub.restore()
})

it('runs psql', async function () {
stub = sinon.stub(psql, 'exec').returns(Promise.resolve(''))

await runCommand(Cmd, [
'--app',
'myapp',
'--command',
'SELECT 1',
])
expect(stdout.output).to.equal('')
expect(stderr.output).to.equal('--> Connecting to postgres-1\n')
})

it('runs psql with file', async function () {
stub = sinon.stub(psql, 'execFile').returns(Promise.resolve(''))
await runCommand(Cmd, [
'--app',
'myapp',
'--file',
'test.sql',
])
expect(stdout.output).to.equal('')
expect(stderr.output).to.equal('--> Connecting to postgres-1\n')
})
})
51 changes: 0 additions & 51 deletions packages/pg-v5/commands/psql.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/pg-v5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ exports.topics = [
]

exports.commands = flatten([
require('./commands/psql'),
require('./commands/repoint'),
require('./commands/wait'),
])

exports.host = require('./lib/host')
exports.fetcher = require('./lib/fetcher')
exports.psql = require('./lib/psql')
53 changes: 0 additions & 53 deletions packages/pg-v5/test/unit/commands/psql.unit.test.js

This file was deleted.

Loading