1+ import { color , utils } from '@heroku/heroku-cli-util'
12import { APIClient , Command , flags } from '@heroku-cli/command'
23import * as Heroku from '@heroku-cli/schema'
34import { 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
69import ConfirmCommand from '../../lib/confirmCommand.js'
7- import type { BackupTransfer , CredentialsInfo } from '../../lib/pg/types.js'
810import backupsFactory from '../../lib/pg/backups.js'
911
1012const 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 ( / ^ H E R O K U _ P O S T G R E S Q L _ / , '' )
33- . replace ( / _ U R L $ / , '' ) ,
34- url : formattedConfig [ attachment . name . toUpperCase ( ) + '_URL' ] ,
3534 attachment : {
3635 ...attachment ,
3736 addon,
3837 } ,
3938 confirm : app ,
39+ name : attachment . name . replace ( / ^ H E R O K U _ P O S T G R E S Q L _ / , '' )
40+ . replace ( / _ U R L $ / , '' ) ,
41+ url : formattedConfig [ attachment . name . toUpperCase ( ) + '_URL' ] ,
4042 }
4143}
4244
4345export 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.' )
0 commit comments