Repository navigation
feat(pg:promote): support pg:promote on Advanced-tier databases #4068
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
base: main
Are you sure you want to change the base?
Changes from all commits
b4e0e85
205bd30
e7da2cd
a63fc24
fca91e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Heroku.AddOnAttachment[]>(`/apps/${app}/addon-attachments`) | ||
| const current = attachments.find(a => a.name === 'DATABASE') | ||
|
|
@@ -51,27 +67,44 @@ 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<pg.ExtendedAddon>(`/addons/${current.addon?.id}`) | ||
| currentIsAdvanced = utils.pg.isAdvancedDatabase(currentAddon) | ||
| } | ||
|
|
||
| const {body: backup} = await this.heroku.post<Heroku.AddOnAttachment>('/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')) | ||
| } | ||
| } | ||
|
|
||
| if (!force) { | ||
| const {body: status} = await this.heroku.get<PgStatus>(`/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<WaitStatus>(`/data/postgres/v1/${attachment.addon.id}/wait_status`, { | ||
| hostname: utils.pg.host(), | ||
| })) | ||
| } else { | ||
| const {body} = await this.heroku.get<PgStatus>(`/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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With Advanced not supporting server-side connection pooling at the moment, we could recommend the customer to detach the pool config var.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prob should run through cx too, I'll draft something up |
||
| `)) | ||
| } 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<PgDatabase>(`/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<PgDatabase>(`/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<Heroku.Formation[]>(`/apps/${app}/formation`) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use this opportunity to get a CX pass for this older sentence?
E.g., as an idea:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'll run it by cx