diff --git a/src/commands/pg/promote.ts b/src/commands/pg/promote.ts index f36f0edf62..d8e8a02a66 100644 --- a/src/commands/pg/promote.ts +++ b/src/commands/pg/promote.ts @@ -1,17 +1,31 @@ /* eslint-disable complexity */ +import type {pg} from '@heroku/heroku-cli-util' + import {Command, flags} from '@heroku-cli/command' import * as Heroku from '@heroku-cli/schema' import {color, utils} from '@heroku/heroku-cli-util' import {Args, ux} from '@oclif/core' import tsheredoc from 'tsheredoc' +import {parseAttachmentFactors} from '../../lib/data/parse-attachment-factors.js' +import {WaitStatus} from '../../lib/data/types.js' import {getRelease} from '../../lib/pg/fetcher.js' import {PgDatabase, PgStatus} from '../../lib/pg/types.js' import {nls} from '../../nls.js' const heredoc = tsheredoc +// Platform API is deprecating the raw namespace param, so Advanced-tier namespaces go out as namespace_config. Platform +// only returns the namespace it built from that config, so parsing its factors back out, with every value as a string, +// resolves to the same existing namespace. +function namespaceParams(namespace: null | string | undefined, isAdvanced: boolean) { + if (!isAdvanced || !namespace) return {namespace} + const factors = Object.entries(parseAttachmentFactors(namespace)).map(([key, value]) => [key, String(value)]) + return {namespace_config: Object.fromEntries(factors)} +} + export default class Promote extends Command { + static aliases = ['data:pg:promote'] static args = { database: Args.string({description: nls('pg:database:arg:description'), required: true}), } @@ -30,6 +44,8 @@ export default class Promote extends Command { const {database} = args const dbResolver = new utils.pg.DatabaseResolver(this.heroku) const attachment = await dbResolver.getAttachment(app, database) + // data-api rejects the legacy /client/v11 wait_status and follower endpoints for Advanced-tier databases + const isAdvanced = utils.pg.isAdvancedDatabase(attachment.addon) ux.action.start(`Ensuring an alternate alias for existing ${color.datastore('DATABASE_URL')}`) const {body: attachments} = await this.heroku.get(`/apps/${app}/addon-attachments`) const current = attachments.find(a => a.name === 'DATABASE') @@ -51,12 +67,19 @@ export default class Promote extends Command { // The current add-on occupying the DATABASE attachment has no // other attachments. In order to promote this database without // error, we can create a secondary attachment, just-in-time. + let currentIsAdvanced = false + if (current.namespace) { + // The attachment list doesn't include the add-on's plan, which tells us its tier + const {body: currentAddon} = await this.heroku.get(`/addons/${current.addon?.id}`) + currentIsAdvanced = utils.pg.isAdvancedDatabase(currentAddon) + } + const {body: backup} = await this.heroku.post('/addon-attachments', { body: { addon: {name: current.addon?.name}, app: {name: app}, confirm: app, - namespace: current.namespace, + ...namespaceParams(current.namespace, currentIsAdvanced), }, }) ux.action.stop(color.attachment(backup.name + '_URL')) @@ -64,14 +87,24 @@ export default class Promote extends Command { } if (!force) { - const {body: status} = await this.heroku.get(`/client/v11/databases/${attachment.addon.id}/wait_status`, { - hostname: utils.pg.host(), - }) - if (status['waiting?']) { + let status: WaitStatus + if (isAdvanced) { + ({body: status} = await this.heroku.get(`/data/postgres/v1/${attachment.addon.id}/wait_status`, { + hostname: utils.pg.host(), + })) + } else { + const {body} = await this.heroku.get(`/client/v11/databases/${attachment.addon.id}/wait_status`, { + hostname: utils.pg.host(), + }) + status = {message: body.message, waiting: body['waiting?']} + } + + if (status.waiting) { + const waitCommand = isAdvanced ? 'heroku data:pg:wait' : 'heroku pg:wait' ux.error(heredoc(` Database cannot be promoted while in state: ${status.message} - Promoting this database can lead to application errors and outage. Please run ${color.code('heroku pg:wait')} to wait for database to become available. + Promoting this database can lead to application errors and outage. Please run ${color.code(waitCommand)} to wait for database to become available. To ignore this error, you can pass the --force flag to promote the database and risk application issues. `)) @@ -89,12 +122,19 @@ export default class Promote extends Command { app: {name: app}, confirm: app, name: as || 'DATABASE', - namespace: attachment.namespace || null, + ...namespaceParams(attachment.namespace || null, isAdvanced), }, }) ux.action.stop() const currentPooler = attachments.find(a => a.namespace === 'connection-pooling:default' && a.addon?.id === current?.addon?.id && a.name === 'DATABASE_CONNECTION_POOL') - if (currentPooler) { + if (currentPooler && isAdvanced) { + // Advanced-tier databases have no connection-pooling:default namespace, so reattaching would leave an empty attachment + ux.warn(heredoc(` + ${color.attachment(currentPooler.name + '_URL')} still points to ${color.addon(current?.addon?.name as string)}. + + ${color.code('heroku pg:promote')} doesn't reattach connection pooling to Advanced-tier databases. + `)) + } else if (currentPooler) { ux.action.start('Reattaching pooler to new leader') await this.heroku.post('/addon-attachments', { body: { @@ -108,18 +148,30 @@ export default class Promote extends Command { ux.action.stop() } - const {body: promotedDatabaseDetails} = await this.heroku.get(`/client/v11/databases/${attachment.addon.id}`, { - hostname: utils.pg.host(), - }) - if (promotedDatabaseDetails.following) { - const unfollowLeaderCmd = `heroku pg:unfollow ${attachment.addon.name}` - ux.warn(heredoc(` - Your database has been promoted but it is currently a follower database in read-only mode. + if (isAdvanced) { + const {pool} = parseAttachmentFactors(attachment.namespace) + if (pool && pool !== 'leader') { + ux.warn(heredoc(` + Your database has been promoted but ${color.attachment(attachment.name)} connects to the ${color.name(pool)} follower pool in read-only mode. + + To make ${color.datastore('DATABASE_URL')} writable, promote an attachment that uses the ${color.name('leader')} pool. + Use ${color.code(`heroku data:pg:attachments ${attachment.addon.name} -a ${app}`)} to list the attachments on the database. + `)) + } + } else { + const {body: promotedDatabaseDetails} = await this.heroku.get(`/client/v11/databases/${attachment.addon.id}`, { + hostname: utils.pg.host(), + }) + if (promotedDatabaseDetails.following) { + const unfollowLeaderCmd = `heroku pg:unfollow ${attachment.addon.name}` + ux.warn(heredoc(` + Your database has been promoted but it is currently a follower database in read-only mode. - Promoting a database with ${color.code('heroku pg:promote')} doesn't automatically unfollow its leader. + Promoting a database with ${color.code('heroku pg:promote')} doesn't automatically unfollow its leader. - Use ${color.code(unfollowLeaderCmd)} to stop this follower from replicating from its leader (${color.datastore(promotedDatabaseDetails.leader as string)}) and convert it into a writable database. - `)) + Use ${color.code(unfollowLeaderCmd)} to stop this follower from replicating from its leader (${color.datastore(promotedDatabaseDetails.leader as string)}) and convert it into a writable database. + `)) + } } const {body: formation} = await this.heroku.get(`/apps/${app}/formation`) diff --git a/test/unit/commands/pg/promote.unit.test.ts b/test/unit/commands/pg/promote.unit.test.ts index 3456b45ba5..59a4fd13b2 100644 --- a/test/unit/commands/pg/promote.unit.test.ts +++ b/test/unit/commands/pg/promote.unit.test.ts @@ -6,6 +6,7 @@ import tsheredoc from 'tsheredoc' import Cmd from '../../../../src/commands/pg/promote.js' import * as fixtures from '../../../fixtures/addons/fixtures.js' +import {addon as advancedAddon, essentialAddon} from '../../../fixtures/data/pg/fixtures.js' const heredoc = tsheredoc @@ -395,11 +396,13 @@ describe('pg:promote when argument is a credential attachment', function () { namespace: 'credential:hello', }, { - addon: {name: addon.name}, + addon: {id: addon.id, name: addon.name}, name: 'DATABASE', namespace: 'credential:goodbye', }, ]) + .get(`/addons/${addon.id}`) + .reply(200, addon) .post('/addon-attachments', { addon: {name: addon.name}, app: {name: 'myapp'}, @@ -897,3 +900,196 @@ describe('pg:promote when promoted database is a follower', function () { expect(stderr).to.include('Your database has been promoted but it is currently a follower') }) }) + +function resolveToAdvanced(namespace: null | string) { + nock('https://api.heroku.com') + .post('/actions/addon-attachments/resolve') + .reply(200, [{addon: advancedAddon, name: 'ADVANCED', namespace}]) +} + +function promoteAdvanced(namespaceParams: {namespace: null} | {namespace_config: Record}) { + return nock('https://api.heroku.com') + .post('/addon-attachments', { + addon: {name: advancedAddon.name}, + app: {name: 'myapp'}, + confirm: 'myapp', + name: 'DATABASE', + ...namespaceParams, + }) + .reply(201) +} + +describe('pg:promote when database is Advanced-tier', function () { + const currentLeader = {addon: {id: 'c23b5a31-3c0a-4c49-9a2b-2ad9b2b4ab70', name: 'postgres-2'}, name: 'DATABASE', namespace: null} + const currentLeaderAlias = {...currentLeader, name: 'RED'} + + beforeEach(function () { + nock('https://api.heroku.com') + .get('/apps/myapp/formation') + .reply(200, []) + }) + + afterEach(function () { + nock.cleanAll() + }) + + it('checks the Advanced wait status and promotes the database', async function () { + resolveToAdvanced(null) + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + promoteAdvanced({namespace: null}) + const dataApi = nock('https://api.data.heroku.com') + .get(`/data/postgres/v1/${advancedAddon.id}/wait_status`) + .reply(200, {message: null, waiting: false}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + dataApi.done() + expectOutput(stderr, heredoc(` + Ensuring an alternate alias for existing ⛁ DATABASE_URL... RED_URL + Promoting ⛁ ${advancedAddon.name} to ⛁ DATABASE_URL on ⬢ myapp... done + `)) + }) + + it('errors and points to data:pg:wait when the Advanced database is waiting', async function () { + resolveToAdvanced(null) + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + nock('https://api.data.heroku.com') + .get(`/data/postgres/v1/${advancedAddon.id}/wait_status`) + .reply(200, {message: 'Modifying', waiting: true}) + + const {error} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + expect(ansis.strip(error!.message)).to.equal(heredoc(` + Database cannot be promoted while in state: Modifying + + Promoting this database can lead to application errors and outage. Please run heroku data:pg:wait to wait for database to become available. + + To ignore this error, you can pass the --force flag to promote the database and risk application issues. + `)) + }) + + it('skips the Advanced wait status when --force is present', async function () { + resolveToAdvanced(null) + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + const promoted = promoteAdvanced({namespace: null}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', '--force', 'DATABASE']) + + promoted.done() + expect(stderr).to.include('Promoting') + }) + + it('warns when the promoted attachment uses a follower pool', async function () { + resolveToAdvanced('pool:analytics|proxy:false|role:owner') + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + const promoted = promoteAdvanced({namespace_config: {pool: 'analytics', proxy: 'false', role: 'owner'}}) + nock('https://api.data.heroku.com') + .get(`/data/postgres/v1/${advancedAddon.id}/wait_status`) + .reply(200, {message: null, waiting: false}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + promoted.done() + expect(ansis.strip(stderr)).to.include('analytics follower pool') + }) + + it('does not warn when the promoted attachment uses the leader pool', async function () { + resolveToAdvanced('pool:leader|proxy:false|role:owner') + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + const promoted = promoteAdvanced({namespace_config: {pool: 'leader', proxy: 'false', role: 'owner'}}) + nock('https://api.data.heroku.com') + .get(`/data/postgres/v1/${advancedAddon.id}/wait_status`) + .reply(200, {message: null, waiting: false}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + promoted.done() + expect(stderr).not.to.include('Warning') + }) + + it('creates the alternate alias with namespace_config when the current DATABASE is a namespaced Advanced attachment', async function () { + nock('https://api.heroku.com') + .post('/actions/addon-attachments/resolve') + .reply(200, [{addon: essentialAddon, name: 'ESSENTIAL', namespace: null}]) + .get('/apps/myapp/addon-attachments') + .reply(200, [{ + addon: {id: advancedAddon.id, name: advancedAddon.name}, name: 'DATABASE', namespace: 'pool:analytics|proxy:false|role:owner', + }]) + .get(`/addons/${advancedAddon.id}`) + .reply(200, advancedAddon) + .post('/addon-attachments', { + addon: {name: advancedAddon.name}, + app: {name: 'myapp'}, + confirm: 'myapp', + namespace_config: {pool: 'analytics', proxy: 'false', role: 'owner'}, + }) + .reply(201, {name: 'GOLD'}) + .post('/addon-attachments', { + addon: {name: essentialAddon.name}, app: {name: 'myapp'}, confirm: 'myapp', name: 'DATABASE', namespace: null, + }) + .reply(201) + nock('https://api.data.heroku.com') + .get(`/client/v11/databases/${essentialAddon.id}/wait_status`) + .reply(200, {message: 'available', 'waiting?': false}) + .get(`/client/v11/databases/${essentialAddon.id}`) + .reply(200, {following: null}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + expectOutput(stderr, heredoc(` + Ensuring an alternate alias for existing ⛁ DATABASE_URL... GOLD_URL + Promoting ⛁ ${essentialAddon.name} to ⛁ DATABASE_URL on ⬢ myapp... done + `)) + }) + + it('does not reattach the connection pooler to an Advanced database', async function () { + resolveToAdvanced(null) + nock('https://api.heroku.com') + .get('/apps/myapp/addon-attachments') + .reply(200, [ + currentLeader, + currentLeaderAlias, + {...currentLeader, name: 'DATABASE_CONNECTION_POOL', namespace: 'connection-pooling:default'}, + ]) + promoteAdvanced({namespace: null}) + nock('https://api.data.heroku.com') + .get(`/data/postgres/v1/${advancedAddon.id}/wait_status`) + .reply(200, {message: null, waiting: false}) + + const {stderr} = await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + expect(stderr).not.to.include('Reattaching pooler') + expect(ansis.strip(stderr)).to.include('DATABASE_CONNECTION_POOL_URL still points to postgres-2') + }) + + it('keeps using the legacy endpoints for Essential-tier databases', async function () { + nock('https://api.heroku.com') + .post('/actions/addon-attachments/resolve') + .reply(200, [{addon: essentialAddon, name: 'ESSENTIAL', namespace: null}]) + .get('/apps/myapp/addon-attachments') + .reply(200, [currentLeader, currentLeaderAlias]) + .post('/addon-attachments', { + addon: {name: essentialAddon.name}, app: {name: 'myapp'}, confirm: 'myapp', name: 'DATABASE', namespace: null, + }) + .reply(201) + const dataApi = nock('https://api.data.heroku.com') + .get(`/client/v11/databases/${essentialAddon.id}/wait_status`) + .reply(200, {message: 'available', 'waiting?': false}) + .get(`/client/v11/databases/${essentialAddon.id}`) + .reply(200, {following: null}) + + await runCommand(Cmd, ['--app', 'myapp', 'DATABASE']) + + dataApi.done() + }) +})