Skip to content

Commit ed5ec7c

Browse files
committed
Fixing more type discrepancies
1 parent 999503e commit ed5ec7c

5 files changed

Lines changed: 47 additions & 60 deletions

File tree

packages/cli/src/commands/pg/copy.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import {color, utils} from '@heroku/heroku-cli-util'
12
import {APIClient, Command, flags} from '@heroku-cli/command'
23
import * as Heroku from '@heroku-cli/schema'
34
import {Args, ux} from '@oclif/core'
4-
import {color, utils} from '@heroku/heroku-cli-util'
5+
6+
import type {NonAdvancedCredentialInfo} from '../../lib/data/types.js'
7+
import type {BackupTransfer} from '../../lib/pg/types.js'
58

69
import ConfirmCommand from '../../lib/confirmCommand.js'
7-
import type {BackupTransfer, CredentialsInfo} from '../../lib/pg/types.js'
810
import backupsFactory from '../../lib/pg/backups.js'
911

1012
const getAttachmentInfo = async function (heroku: APIClient, db: string, app: string) {
@@ -14,9 +16,9 @@ const getAttachmentInfo = async function (heroku: APIClient, db: string, app: st
1416
const conn = utils.pg.DatabaseResolver.parsePostgresConnectionString(db)
1517
const host = `${conn.host}:${conn.port}`
1618
return {
19+
confirm: conn.database || conn.host,
1720
name: conn.database ? `database ${conn.database} on ${host}` : `database on ${host}`,
1821
url: db,
19-
confirm: conn.database || conn.host,
2022
}
2123
}
2224

@@ -29,37 +31,39 @@ const getAttachmentInfo = async function (heroku: APIClient, db: string, app: st
2931
const formattedConfig = Object.fromEntries(Object.entries(config).map(([k, v]) => [k.toUpperCase(), v]))
3032

3133
return {
32-
name: attachment.name.replace(/^HEROKU_POSTGRESQL_/, '')
33-
.replace(/_URL$/, ''),
34-
url: formattedConfig[attachment.name.toUpperCase() + '_URL'],
3534
attachment: {
3635
...attachment,
3736
addon,
3837
},
3938
confirm: app,
39+
name: attachment.name.replace(/^HEROKU_POSTGRESQL_/, '')
40+
.replace(/_URL$/, ''),
41+
url: formattedConfig[attachment.name.toUpperCase() + '_URL'],
4042
}
4143
}
4244

4345
export default class Copy extends Command {
44-
static description = 'copy all data from source db to target'
45-
static help = 'at least one of the databases must be a Heroku PostgreSQL DB'
46-
static topic = 'pg'
4746
static args = {
48-
source: Args.string({required: true, description: 'config var exposed to the owning app containing the source database URL'}),
49-
target: Args.string({required: true, description: 'config var exposed to the owning app containing the target database URL'}),
47+
source: Args.string({description: 'config var exposed to the owning app containing the source database URL', required: true}),
48+
target: Args.string({description: 'config var exposed to the owning app containing the target database URL', required: true}),
5049
}
5150

51+
static description = 'copy all data from source db to target'
5252
static flags = {
53-
'wait-interval': flags.string(),
54-
verbose: flags.boolean(),
55-
confirm: flags.string(),
5653
app: flags.app({required: true}),
54+
confirm: flags.string(),
5755
remote: flags.remote(),
56+
verbose: flags.boolean(),
57+
'wait-interval': flags.string(),
5858
}
5959

60+
static help = 'at least one of the databases must be a Heroku PostgreSQL DB'
61+
62+
static topic = 'pg'
63+
6064
public async run(): Promise<void> {
61-
const {flags, args} = await this.parse(Copy)
62-
const {'wait-interval': waitInterval, verbose, confirm, app} = flags
65+
const {args, flags} = await this.parse(Copy)
66+
const {app, confirm, verbose, 'wait-interval': waitInterval} = flags
6367
const pgbackups = backupsFactory(app, this.heroku)
6468
const interval = Math.max(3, Number.parseInt(waitInterval || '0', 10)) || 3
6569

@@ -83,13 +87,13 @@ export default class Copy extends Command {
8387
ux.action.stop()
8488

8589
if (source.attachment) {
86-
const {body: credentials} = await this.heroku.get<CredentialsInfo>(
90+
const {body: credentials} = await this.heroku.get<NonAdvancedCredentialInfo[]>(
8791
`/postgres/v0/databases/${source.attachment.addon.name}/credentials`,
8892
{
89-
hostname: utils.pg.host(),
9093
headers: {
9194
Authorization: `Basic ${Buffer.from(`:${this.heroku.auth}`).toString('base64')}`,
9295
},
96+
hostname: utils.pg.host(),
9397
})
9498
if (credentials.length > 1) {
9599
ux.warn('pg:copy will only copy your default credential and the data it has access to. Any additional credentials and data that only they can access will not be copied.')
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
import {Command, flags} from '@heroku-cli/command'
2-
import {Args} from '@oclif/core'
31
import {hux, utils} from '@heroku/heroku-cli-util'
2+
import {Command, flags} from '@heroku-cli/command'
43
import * as Heroku from '@heroku-cli/schema'
5-
import type {CredentialInfo, CredentialsInfo} from '../../lib/pg/types.js'
4+
import {Args} from '@oclif/core'
5+
6+
import type {NonAdvancedCredentialInfo} from '../../lib/data/types.js'
7+
68
import {presentCredentialAttachments} from '../../lib/pg/util.js'
79
import {nls} from '../../nls.js'
810

911
export default class Credentials extends Command {
10-
static topic = 'pg'
12+
static args = {
13+
database: Args.string({description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}`}),
14+
}
15+
1116
static description = 'show information on credentials in the database'
1217
static flags = {
1318
app: flags.app({required: true}),
1419
remote: flags.remote(),
1520
}
1621

17-
static args = {
18-
database: Args.string({description: `${nls('pg:database:arg:description')} ${nls('pg:database:arg:description:default:suffix')}`}),
22+
static topic = 'pg'
23+
24+
protected isDefaultCredential(cred: NonAdvancedCredentialInfo): boolean {
25+
return cred.name === 'default'
1926
}
2027

2128
public async run(): Promise<void> {
22-
const {flags, args} = await this.parse(Credentials)
29+
const {args, flags} = await this.parse(Credentials)
2330
const {app} = flags
2431
const {database} = args
2532
const dbResolver = new utils.pg.DatabaseResolver(this.heroku)
2633
const {addon} = await dbResolver.getAttachment(app, database)
2734

28-
const {body: credentials} = await this.heroku.get<CredentialsInfo>(
35+
const {body: credentials} = await this.heroku.get<NonAdvancedCredentialInfo[]>(
2936
`/postgres/v0/databases/${addon.id}/credentials`,
3037
{
31-
hostname: utils.pg.host(),
3238
headers: {
3339
Authorization: `Basic ${Buffer.from(`:${this.heroku.auth}`).toString('base64')}`,
3440
},
41+
hostname: utils.pg.host(),
3542
},
3643
)
3744
const sortedCredentials = this.sortByDefaultAndName(credentials)
3845
const {body: attachments} = await this.heroku.get<Required<Heroku.AddOnAttachment>[]>(`/addons/${addon.id}/addon-attachments`)
3946

40-
const presentCredential = (cred: CredentialInfo): string => {
47+
const presentCredential = (cred: NonAdvancedCredentialInfo): string => {
4148
let credAttachments = [] as Required<Heroku.AddOnAttachment>[]
4249
if (cred.name === 'default') {
4350
credAttachments = attachments.filter(a => a.namespace === null)
@@ -60,16 +67,12 @@ export default class Credentials extends Command {
6067
})
6168
}
6269

63-
protected sortByDefaultAndName(credentials: CredentialsInfo) {
64-
return credentials.sort((a: CredentialInfo, b: CredentialInfo) => {
70+
protected sortByDefaultAndName(credentials: NonAdvancedCredentialInfo[]) {
71+
return credentials.sort((a: NonAdvancedCredentialInfo, b: NonAdvancedCredentialInfo) => {
6572
const isDefaultA = this.isDefaultCredential(a)
6673
const isDefaultB = this.isDefaultCredential(b)
6774

6875
return isDefaultB < isDefaultA ? -1 : (isDefaultA < isDefaultB ? 1 : a.name.localeCompare(b.name))
6976
})
7077
}
71-
72-
protected isDefaultCredential(cred: CredentialInfo): boolean {
73-
return cred.name === 'default'
74-
}
7578
}

packages/cli/src/commands/pg/credentials/url.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Args, ux} from '@oclif/core'
44
import tsheredoc from 'tsheredoc'
55
import {URL} from 'url'
66

7-
import type {CredentialInfo} from '../../../lib/pg/types.js'
7+
import type {NonAdvancedCredentialInfo} from '../../../lib/data/types.js'
88

99
import {nls} from '../../../nls.js'
1010

@@ -38,7 +38,7 @@ export default class Url extends Command {
3838
ux.error('Legacy Essential-tier databases do not support named credentials.')
3939
}
4040

41-
const {body: credInfo} = await this.heroku.get<CredentialInfo>(
41+
const {body: credInfo} = await this.heroku.get<NonAdvancedCredentialInfo>(
4242
`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(name)}`,
4343
{
4444
headers: {
@@ -52,11 +52,9 @@ export default class Url extends Command {
5252
ux.error(`Could not find any active credentials for ${name}`, {exit: 1})
5353
}
5454

55-
const creds = Object.assign({}, db, {
56-
database: credInfo.database, host: credInfo.host, port: credInfo.port,
57-
}, {
58-
user: activeCreds?.user, password: activeCreds?.password,
59-
})
55+
const creds = {
56+
...db, database: credInfo.database, host: credInfo.host, password: activeCreds?.password, port: credInfo.port, user: activeCreds?.user,
57+
}
6058
const connUrl = new URL(`postgres://${creds.host}/${creds.database}`)
6159
connUrl.port = creds.port.toString()
6260
if (creds.user && creds.password) {

packages/cli/src/lib/data/credentialUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {CredentialInfo} from './types.js'
22

33
// NEW This is new. something similar exists in core: packages/cli/src/commands/pg/credentials.ts:61
4-
// protected sortByDefaultAndName(credentials: CredentialsInfo) {
4+
// protected sortByDefaultAndName(credentials: CredentialInfo[]) {
55
// return credentials.sort((a, b) => {
66
// const isDefaultA = this.isDefaultCredential(a)
77
// const isDefaultB = this.isDefaultCredential(b)

packages/cli/src/lib/pg/types.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,6 @@ export type Link = {
195195
attachment_name: string,
196196
},
197197
}
198-
type CredentialState = 'enabling' | 'active' | 'revoking' | 'revoked' | 'archived'
199-
export type Credential = {
200-
user: string
201-
password: string
202-
state: CredentialState
203-
connections?: number | null
204-
}
205-
type CredentialStoreState = 'provisioning' | 'wait_for_provisioning' | 'active' | 'rotating' | 'rotation_completed' | 'revoking' | 'archived'
206-
export type CredentialInfo = {
207-
uuid: string
208-
name: string
209-
state: CredentialStoreState
210-
database: string
211-
host: string
212-
port: number
213-
credentials: Array<Credential>
214-
}
215-
export type CredentialsInfo = Array<CredentialInfo>
216198
export type MaintenanceApiResponse = {
217199
message: string,
218200
}

0 commit comments

Comments
 (0)