neonctl 2.37.1 → 2.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +15 -700
  2. package/bin/cli.js +3 -0
  3. package/package.json +8 -75
  4. package/dist/analytics.js +0 -168
  5. package/dist/api.js +0 -720
  6. package/dist/auth.js +0 -125
  7. package/dist/callback.html +0 -60
  8. package/dist/cli.js +0 -9
  9. package/dist/commands/api.js +0 -278
  10. package/dist/commands/auth.js +0 -214
  11. package/dist/commands/bootstrap.js +0 -481
  12. package/dist/commands/branches.js +0 -488
  13. package/dist/commands/bucket.js +0 -549
  14. package/dist/commands/checkout.js +0 -321
  15. package/dist/commands/config.js +0 -626
  16. package/dist/commands/connection_string.js +0 -172
  17. package/dist/commands/data_api.js +0 -284
  18. package/dist/commands/databases.js +0 -82
  19. package/dist/commands/deploy.js +0 -26
  20. package/dist/commands/dev.js +0 -698
  21. package/dist/commands/diff.js +0 -222
  22. package/dist/commands/env.js +0 -189
  23. package/dist/commands/functions.js +0 -373
  24. package/dist/commands/index.js +0 -62
  25. package/dist/commands/init.js +0 -73
  26. package/dist/commands/inspect.js +0 -65
  27. package/dist/commands/ip_allow.js +0 -137
  28. package/dist/commands/link.js +0 -1121
  29. package/dist/commands/neon_auth.js +0 -1028
  30. package/dist/commands/operations.js +0 -28
  31. package/dist/commands/orgs.js +0 -24
  32. package/dist/commands/projects.js +0 -413
  33. package/dist/commands/psql.js +0 -62
  34. package/dist/commands/roles.js +0 -65
  35. package/dist/commands/schema_diff.js +0 -151
  36. package/dist/commands/set_context.js +0 -29
  37. package/dist/commands/snapshots.js +0 -455
  38. package/dist/commands/status.js +0 -40
  39. package/dist/commands/user.js +0 -15
  40. package/dist/commands/vpc_endpoints.js +0 -134
  41. package/dist/config.js +0 -11
  42. package/dist/config_format.js +0 -72
  43. package/dist/context.js +0 -231
  44. package/dist/current_branch_fast_path.js +0 -55
  45. package/dist/dev/env.js +0 -244
  46. package/dist/dev/functions.js +0 -70
  47. package/dist/dev/inputs.js +0 -63
  48. package/dist/dev/runtime.js +0 -146
  49. package/dist/env.js +0 -36
  50. package/dist/env_file.js +0 -159
  51. package/dist/errors.js +0 -80
  52. package/dist/functions_api.js +0 -48
  53. package/dist/help.js +0 -146
  54. package/dist/index.js +0 -240
  55. package/dist/log.js +0 -18
  56. package/dist/parameters.gen.js +0 -480
  57. package/dist/pkg.js +0 -25
  58. package/dist/psql/cli.js +0 -53
  59. package/dist/psql/command/cmd_cond.js +0 -437
  60. package/dist/psql/command/cmd_connect.js +0 -820
  61. package/dist/psql/command/cmd_copy.js +0 -1035
  62. package/dist/psql/command/cmd_describe.js +0 -1815
  63. package/dist/psql/command/cmd_format.js +0 -948
  64. package/dist/psql/command/cmd_io.js +0 -2193
  65. package/dist/psql/command/cmd_lo.js +0 -393
  66. package/dist/psql/command/cmd_meta.js +0 -969
  67. package/dist/psql/command/cmd_misc.js +0 -187
  68. package/dist/psql/command/cmd_pipeline.js +0 -1148
  69. package/dist/psql/command/cmd_restrict.js +0 -171
  70. package/dist/psql/command/cmd_show.js +0 -766
  71. package/dist/psql/command/dispatch.js +0 -343
  72. package/dist/psql/command/inputQueue.js +0 -42
  73. package/dist/psql/command/shared.js +0 -71
  74. package/dist/psql/complete/filenames.js +0 -139
  75. package/dist/psql/complete/index.js +0 -104
  76. package/dist/psql/complete/matcher.js +0 -315
  77. package/dist/psql/complete/psqlVars.js +0 -249
  78. package/dist/psql/complete/queries.js +0 -493
  79. package/dist/psql/complete/rules.js +0 -2424
  80. package/dist/psql/core/common.js +0 -1253
  81. package/dist/psql/core/help.js +0 -576
  82. package/dist/psql/core/mainloop.js +0 -1360
  83. package/dist/psql/core/prompt.js +0 -439
  84. package/dist/psql/core/settings.js +0 -686
  85. package/dist/psql/core/sqlHelp.js +0 -1066
  86. package/dist/psql/core/startup.js +0 -846
  87. package/dist/psql/core/syncVars.js +0 -116
  88. package/dist/psql/core/variables.js +0 -287
  89. package/dist/psql/describe/formatters.js +0 -1330
  90. package/dist/psql/describe/processNamePattern.js +0 -270
  91. package/dist/psql/describe/queries.js +0 -2452
  92. package/dist/psql/describe/versionGate.js +0 -44
  93. package/dist/psql/index.js +0 -2030
  94. package/dist/psql/io/history.js +0 -299
  95. package/dist/psql/io/input.js +0 -120
  96. package/dist/psql/io/lineEditor/buffer.js +0 -325
  97. package/dist/psql/io/lineEditor/complete.js +0 -227
  98. package/dist/psql/io/lineEditor/filename.js +0 -159
  99. package/dist/psql/io/lineEditor/index.js +0 -893
  100. package/dist/psql/io/lineEditor/keymap.js +0 -745
  101. package/dist/psql/io/lineEditor/vt100.js +0 -363
  102. package/dist/psql/io/pgpass.js +0 -202
  103. package/dist/psql/io/pgservice.js +0 -194
  104. package/dist/psql/io/psqlrc.js +0 -422
  105. package/dist/psql/print/aligned.js +0 -1748
  106. package/dist/psql/print/asciidoc.js +0 -230
  107. package/dist/psql/print/crosstab.js +0 -463
  108. package/dist/psql/print/csv.js +0 -76
  109. package/dist/psql/print/html.js +0 -240
  110. package/dist/psql/print/json.js +0 -96
  111. package/dist/psql/print/latex.js +0 -379
  112. package/dist/psql/print/pager.js +0 -267
  113. package/dist/psql/print/troff.js +0 -240
  114. package/dist/psql/print/unaligned.js +0 -99
  115. package/dist/psql/print/units.js +0 -188
  116. package/dist/psql/scanner/slash.js +0 -515
  117. package/dist/psql/scanner/sql.js +0 -914
  118. package/dist/psql/scanner/stringutils.js +0 -394
  119. package/dist/psql/types/backslash.js +0 -1
  120. package/dist/psql/types/connection.js +0 -1
  121. package/dist/psql/types/index.js +0 -7
  122. package/dist/psql/types/printer.js +0 -1
  123. package/dist/psql/types/repl.js +0 -1
  124. package/dist/psql/types/scanner.js +0 -24
  125. package/dist/psql/types/settings.js +0 -1
  126. package/dist/psql/types/variables.js +0 -1
  127. package/dist/psql/wire/connection.js +0 -2857
  128. package/dist/psql/wire/copy.js +0 -108
  129. package/dist/psql/wire/notify.js +0 -59
  130. package/dist/psql/wire/pipeline.js +0 -521
  131. package/dist/psql/wire/protocol.js +0 -466
  132. package/dist/psql/wire/sasl.js +0 -294
  133. package/dist/psql/wire/tls.js +0 -602
  134. package/dist/storage_api.js +0 -147
  135. package/dist/test_utils/fixtures.js +0 -121
  136. package/dist/test_utils/oauth_server.js +0 -9
  137. package/dist/types.js +0 -1
  138. package/dist/utils/ai_gateway_notice.js +0 -180
  139. package/dist/utils/api_enums.js +0 -33
  140. package/dist/utils/auth.js +0 -5
  141. package/dist/utils/branch_notice.js +0 -22
  142. package/dist/utils/branch_picker.js +0 -103
  143. package/dist/utils/compute_units.js +0 -28
  144. package/dist/utils/config_diff.js +0 -185
  145. package/dist/utils/enrichers.js +0 -161
  146. package/dist/utils/esbuild.js +0 -158
  147. package/dist/utils/formats.js +0 -18
  148. package/dist/utils/git_diff.js +0 -90
  149. package/dist/utils/inspect_db.js +0 -153
  150. package/dist/utils/inspect_queries.js +0 -372
  151. package/dist/utils/middlewares.js +0 -20
  152. package/dist/utils/openapi.js +0 -114
  153. package/dist/utils/package_manager.js +0 -68
  154. package/dist/utils/point_in_time.js +0 -56
  155. package/dist/utils/psql.js +0 -120
  156. package/dist/utils/string.js +0 -5
  157. package/dist/utils/ui.js +0 -59
  158. package/dist/utils/zip.js +0 -4
  159. package/dist/writer.js +0 -97
@@ -1,488 +0,0 @@
1
- import { retryOnLock } from "../api.js";
2
- import { contextBranch, readContextFile } from "../context.js";
3
- import { log } from "../log.js";
4
- import { branchCreateRequest } from "../parameters.gen.js";
5
- import { EndpointType } from "../utils/api_enums.js";
6
- import { getComputeUnits } from "../utils/compute_units.js";
7
- import { branchIdFromProps, branchIdResolve, fillSingleProject, } from "../utils/enrichers.js";
8
- import { looksLikeBranchId, looksLikeLSN, looksLikeTimestamp, } from "../utils/formats.js";
9
- import { parsePointInTime } from "../utils/point_in_time.js";
10
- import { psql } from "../utils/psql.js";
11
- import { writer } from "../writer.js";
12
- import { parseSchemaDiffParams, schemaDiff } from "./schema_diff.js";
13
- export const BRANCH_FIELDS = [
14
- "name",
15
- "id",
16
- "current_state",
17
- "created_at",
18
- "expires_at",
19
- ];
20
- const BRANCH_FIELDS_RESET = [
21
- "name",
22
- "id",
23
- "default",
24
- "current_state",
25
- "created_at",
26
- "last_reset_at",
27
- ];
28
- export const command = "branches";
29
- export const describe = "Manage branches";
30
- export const aliases = ["branch"];
31
- export const builder = (argv) => argv
32
- .usage("$0 branches <sub-command> [options]")
33
- .options({
34
- "project-id": {
35
- describe: "Project ID",
36
- type: "string",
37
- },
38
- })
39
- .middleware(fillSingleProject)
40
- .middleware((args) => {
41
- // Provide alias for analytics
42
- args.branchId ?? (args.branchId = args.id);
43
- })
44
- .command("list", "List branches", (yargs) => yargs, (args) => list(args))
45
- .command("create", "Create a branch", (yargs) => yargs.options({
46
- name: branchCreateRequest["branch.name"],
47
- parent: {
48
- describe: "Parent branch name or id or timestamp or LSN. Defaults to the default branch",
49
- type: "string",
50
- },
51
- compute: {
52
- describe: "Create a branch with or without a compute. By default branch is created with a read-write compute. To create a branch without compute use --no-compute",
53
- type: "boolean",
54
- default: true,
55
- },
56
- type: {
57
- describe: "Type of compute to add",
58
- type: "string",
59
- implies: "compute",
60
- default: EndpointType.ReadWrite,
61
- choices: Object.values(EndpointType),
62
- },
63
- "suspend-timeout": {
64
- describe: "Duration of inactivity in seconds after which the compute endpoint is\nautomatically suspended. The value `0` means use the global default.\nThe value `-1` means never suspend. The default value is `300` seconds (5 minutes).\nThe maximum value is `604800` seconds (1 week).",
65
- type: "number",
66
- implies: "compute",
67
- default: 0,
68
- },
69
- cu: {
70
- describe: 'The number of Compute Units. Could be a fixed size (e.g. "2") or a range delimited by a dash (e.g. "0.5-3").',
71
- type: "string",
72
- implies: "compute",
73
- },
74
- psql: {
75
- type: "boolean",
76
- describe: "Connect to a new branch via psql",
77
- default: false,
78
- },
79
- fallback: {
80
- type: "boolean",
81
- describe: "Force the embedded TypeScript psql fallback (for testing)",
82
- default: false,
83
- hidden: true,
84
- },
85
- annotation: {
86
- type: "string",
87
- hidden: true,
88
- default: "{}",
89
- },
90
- "schema-only": {
91
- describe: "Create a schema-only branch. Requires exactly one read-write compute.",
92
- type: "boolean",
93
- default: false,
94
- },
95
- "expires-at": {
96
- describe: "Set an expiration date for the branch. Accepts a date string (e.g., 2024-12-31T23:59:59Z).",
97
- type: "string",
98
- requiresArg: true,
99
- },
100
- protected: {
101
- describe: branchCreateRequest["branch.protected"].description,
102
- type: "boolean",
103
- },
104
- }), (args) => create(args))
105
- .command("reset <id|name>", "Reset a branch", (yargs) => yargs.options({
106
- parent: {
107
- describe: "Reset to a parent branch",
108
- type: "boolean",
109
- default: false,
110
- },
111
- "preserve-under-name": {
112
- describe: "Name under which to preserve the old branch",
113
- },
114
- }), (args) => reset(args))
115
- .command("restore <target-id|name> <source>[@(timestamp|lsn)]", "Restores a branch to a specific point in time\n<source> can be: ^self, ^parent, or <source-branch-id|name>", (yargs) => yargs
116
- // we want to show meaningful help for the command
117
- // but it makes yargs to fail on parsing the command
118
- // so we need to fill in the missing args manually
119
- .middleware((args) => {
120
- args.id = args.targetId;
121
- args.pointInTime = args["source@(timestamp"];
122
- args.branchId = args.id; // for analytics
123
- })
124
- .usage("$0 branches restore <target-id|name> <source>[@(timestamp|lsn)]")
125
- .options({
126
- "preserve-under-name": {
127
- describe: "Name under which to preserve the old branch",
128
- },
129
- })
130
- .example([
131
- [
132
- "$0 branches restore main br-source-branch-123456",
133
- "Restores main to the head of the branch with id br-source-branch-123456",
134
- ],
135
- [
136
- "$0 branches restore main source@2021-01-01T00:00:00Z",
137
- "Restores main to the timestamp 2021-01-01T00:00:00Z of the source branch",
138
- ],
139
- [
140
- "$0 branches restore my-branch ^self@0/123456",
141
- "Restores my-branch to the LSN 0/123456 from its own history",
142
- ],
143
- [
144
- "$0 branches restore my-branch ^parent",
145
- "Restore my-branch to the head of its parent branch",
146
- ],
147
- ]), (args) => restore(args))
148
- .command("rename <id|name> <new-name>", "Rename a branch", (yargs) => yargs, (args) => rename(args))
149
- .command("set-default <id|name>", "Set a branch as default", (yargs) => yargs, (args) => setDefault(args))
150
- .command({
151
- command: "set-expiration <id|name>",
152
- describe: "Set an expiration date for the branch",
153
- builder: (yargs) => yargs.options({
154
- "expires-at": {
155
- describe: "Set a expiration date for the branch. If omitted, expiration will be removed. Format [RFC3339]: 2024-12-31T23:59:59Z",
156
- type: "string",
157
- requiresArg: false,
158
- },
159
- }),
160
- handler: (args) => setExpiration(args),
161
- })
162
- .command("add-compute <id|name>", "Add a compute to a branch", (yargs) => yargs.options({
163
- type: {
164
- type: "string",
165
- choices: Object.values(EndpointType),
166
- describe: "Type of compute to add",
167
- default: EndpointType.ReadOnly,
168
- },
169
- cu: {
170
- describe: 'The number of Compute Units. Could be a fixed size (e.g. "2") or a range delimited by a dash (e.g. "0.5-3").',
171
- type: "string",
172
- },
173
- name: {
174
- type: "string",
175
- describe: "Optional name of the compute",
176
- },
177
- }), (args) => addCompute(args))
178
- .command("delete <id|name>", "Delete a branch", (yargs) => yargs, (args) => deleteBranch(args))
179
- .command("get <id|name>", "Get a branch", (yargs) => yargs, (args) => get(args))
180
- .command({
181
- command: "schema-diff [base-branch] [compare-source[@(timestamp|lsn)]]",
182
- aliases: ["sd"],
183
- describe: "Compare the latest schemas of any two branches, or compare a branch to its own or another branch's history.",
184
- builder: (yargs) => {
185
- return yargs
186
- .middleware((args) => (args.compareSource =
187
- args["compare-source@(timestamp"]))
188
- .middleware(parseSchemaDiffParams)
189
- .options({
190
- database: {
191
- alias: "db",
192
- type: "string",
193
- description: "Name of the database for which the schema comparison is performed",
194
- },
195
- })
196
- .example([
197
- [
198
- "$0 branches schema-diff main br-compare-branch-123456",
199
- "Compares the main branch to the head of the branch with ID br-compare-branch-123456",
200
- ],
201
- [
202
- "$0 branches schema-diff main compare@2024-06-01T00:00:00Z",
203
- "Compares the main branch to the state of the compare branch at timestamp 2024-06-01T00:00:00.000Z",
204
- ],
205
- [
206
- "$0 branches schema-diff my-branch ^self@0/123456",
207
- "Compares my-branch to LSN 0/123456 from its own history",
208
- ],
209
- [
210
- "$0 branches schema-diff my-branch ^parent",
211
- "Compares my-branch to the head of its parent branch",
212
- ],
213
- [
214
- "$0 branches schema-diff",
215
- "If a branch is specified in 'set-context', compares this branch to its parent. Otherwise, compares the default branch to its parent.",
216
- ],
217
- ]);
218
- },
219
- handler: (args) => schemaDiff(args),
220
- });
221
- export const handler = (args) => {
222
- return args;
223
- };
224
- const list = async (props) => {
225
- const { data: { branches, annotations }, } = await props.apiClient.listProjectBranches({
226
- projectId: props.projectId,
227
- });
228
- // The branch pinned in the local context (.neon), so we can flag it as `[current]` — the
229
- // one commands target by default and that `neonctl env pull` would read. The context
230
- // stores the branch by name (preferred) or id, so match against either.
231
- const currentBranch = contextBranch(readContextFile(props.contextFile));
232
- writer(props).end(branches, {
233
- fields: BRANCH_FIELDS,
234
- renderColumns: {
235
- expires_at: (br) => br.expires_at || "never",
236
- // Word labels (not symbols) so they read clearly and match the existing `[anon]`.
237
- name: (br) => {
238
- const annotation = annotations[br.id];
239
- const isAnon = annotation?.value.anonymized;
240
- const labels = [];
241
- if (br.default) {
242
- labels.push("[default]");
243
- }
244
- if (br.protected) {
245
- labels.push("[protected]");
246
- }
247
- if (isAnon) {
248
- labels.push("[anon]");
249
- }
250
- if (currentBranch !== undefined &&
251
- (br.id === currentBranch || br.name === currentBranch)) {
252
- labels.push("[current]");
253
- }
254
- labels.push(br.name);
255
- return labels.join(" ");
256
- },
257
- },
258
- });
259
- };
260
- const create = async (props) => {
261
- const branches = await props.apiClient
262
- .listProjectBranches({ projectId: props.projectId })
263
- .then(({ data }) => data.branches);
264
- const parentProps = (() => {
265
- if (!props.parent) {
266
- const branch = branches.find((b) => b.default);
267
- if (!branch) {
268
- throw new Error("No default branch found");
269
- }
270
- return { parent_id: branch.id };
271
- }
272
- if (looksLikeLSN(props.parent)) {
273
- return { parent_lsn: props.parent };
274
- }
275
- if (looksLikeTimestamp(props.parent)) {
276
- return { parent_timestamp: props.parent };
277
- }
278
- if (looksLikeBranchId(props.parent)) {
279
- return { parent_id: props.parent };
280
- }
281
- const branch = branches.find((b) => b.name === props.parent);
282
- if (!branch) {
283
- throw new Error(`Branch ${props.parent} not found`);
284
- }
285
- return { parent_id: branch.id };
286
- })();
287
- // Validate schema-only branch requirements
288
- if (props.schemaOnly) {
289
- if (!props.compute) {
290
- throw new Error("Schema-only branches require a compute endpoint");
291
- }
292
- if (props.type !== EndpointType.ReadWrite) {
293
- throw new Error("Schema-only branches require a read-write compute endpoint");
294
- }
295
- }
296
- const { data } = await retryOnLock(() => props.apiClient.createProjectBranch(props.projectId, {
297
- branch: {
298
- name: props.name,
299
- ...parentProps,
300
- ...(props.schemaOnly ? { init_source: "schema-only" } : {}),
301
- ...(props["expires-at"]
302
- ? {
303
- expires_at: new Date(props["expires-at"]).toISOString(),
304
- }
305
- : {}),
306
- ...(props.protected !== undefined
307
- ? { protected: props.protected }
308
- : {}),
309
- },
310
- endpoints: props.compute
311
- ? [
312
- {
313
- type: props.type,
314
- suspend_timeout_seconds: props.suspendTimeout === 0
315
- ? undefined
316
- : props.suspendTimeout,
317
- ...(props.cu
318
- ? getComputeUnits(props.cu)
319
- : undefined),
320
- },
321
- ]
322
- : [],
323
- annotation_value: props.annotation
324
- ? JSON.parse(props.annotation)
325
- : undefined,
326
- }));
327
- const parent = branches.find((b) => b.id === data.branch.parent_id);
328
- if (parent?.protected) {
329
- log.warning("The parent branch is protected; a unique role password has been generated for the new branch.");
330
- }
331
- const out = writer(props);
332
- out.write(data.branch, {
333
- fields: BRANCH_FIELDS,
334
- title: "branch",
335
- emptyMessage: "No branches have been found.",
336
- });
337
- if (data.endpoints?.length > 0) {
338
- out.write(data.endpoints, {
339
- fields: ["id", "created_at"],
340
- title: "endpoints",
341
- emptyMessage: "No endpoints have been found.",
342
- });
343
- }
344
- if (data.connection_uris?.length) {
345
- out.write(data.connection_uris, {
346
- fields: ["connection_uri"],
347
- title: "connection_uris",
348
- emptyMessage: "No connection uris have been found",
349
- });
350
- }
351
- out.end();
352
- if (props.psql) {
353
- if (!data.connection_uris?.length) {
354
- throw new Error(`Branch ${data.branch.id} doesn't have a connection uri`);
355
- }
356
- const connection_uri = data.connection_uris[0].connection_uri;
357
- const psqlArgs = props["--"];
358
- await psql(connection_uri, psqlArgs, {
359
- mode: props.fallback ? "ts" : "auto",
360
- });
361
- }
362
- };
363
- const rename = async (props) => {
364
- const branchId = await branchIdFromProps(props);
365
- const { data } = await retryOnLock(() => props.apiClient.updateProjectBranch(props.projectId, branchId, {
366
- branch: {
367
- name: props.newName,
368
- },
369
- }));
370
- writer(props).end(data.branch, {
371
- fields: BRANCH_FIELDS,
372
- });
373
- };
374
- const setDefault = async (props) => {
375
- const branchId = await branchIdFromProps(props);
376
- const { data } = await retryOnLock(() => props.apiClient.setDefaultProjectBranch(props.projectId, branchId));
377
- writer(props).end(data.branch, {
378
- fields: BRANCH_FIELDS,
379
- });
380
- };
381
- const deleteBranch = async (props) => {
382
- const branchId = await branchIdFromProps(props);
383
- const { data } = await retryOnLock(() => props.apiClient.deleteProjectBranch(props.projectId, branchId));
384
- // A 204 (branch already gone) carries no body; only a 200 returns it.
385
- if (data) {
386
- writer(props).end(data.branch, {
387
- fields: BRANCH_FIELDS,
388
- });
389
- }
390
- };
391
- const get = async (props) => {
392
- const branchId = await branchIdFromProps(props);
393
- const { data } = await props.apiClient.getProjectBranch(props.projectId, branchId);
394
- writer(props).end(data.branch, {
395
- fields: BRANCH_FIELDS,
396
- });
397
- };
398
- const addCompute = async (props) => {
399
- const branchId = await branchIdFromProps(props);
400
- const computeName = props.name ? { name: props.name } : null;
401
- const { data } = await retryOnLock(() => props.apiClient.createProjectEndpoint(props.projectId, {
402
- endpoint: {
403
- branch_id: branchId,
404
- type: props.type,
405
- ...(props.cu ? getComputeUnits(props.cu) : undefined),
406
- ...computeName,
407
- },
408
- }));
409
- writer(props).end(data.endpoint, {
410
- fields: ["id", "host"],
411
- });
412
- };
413
- const reset = async (props) => {
414
- if (!props.parent) {
415
- throw new Error("Only resetting to parent is supported for now");
416
- }
417
- const branchId = await branchIdFromProps(props);
418
- const { data: { branch: { parent_id }, }, } = await props.apiClient.getProjectBranch(props.projectId, branchId);
419
- if (!parent_id) {
420
- throw new Error("Branch has no parent");
421
- }
422
- const { data } = await retryOnLock(() => props.apiClient.restoreProjectBranch(props.projectId, branchId, {
423
- source_branch_id: parent_id,
424
- preserve_under_name: props.preserveUnderName || undefined,
425
- }));
426
- writer(props).end(data.branch, {
427
- // need to reset types until we expose reset api
428
- fields: BRANCH_FIELDS_RESET,
429
- });
430
- };
431
- const restore = async (props) => {
432
- const targetBranchId = await branchIdResolve({
433
- branch: props.id,
434
- projectId: props.projectId,
435
- apiClient: props.apiClient,
436
- });
437
- const pointInTime = await parsePointInTime({
438
- pointInTime: props.pointInTime,
439
- targetBranchId,
440
- projectId: props.projectId,
441
- api: props.apiClient,
442
- });
443
- log.info(`Restoring branch ${targetBranchId} to the branch ${pointInTime.branchId} ${(pointInTime.tag === "lsn" && "LSN " + pointInTime.lsn) ||
444
- (pointInTime.tag === "timestamp" &&
445
- "timestamp " + pointInTime.timestamp) ||
446
- "head"}`);
447
- const { data } = await retryOnLock(() => props.apiClient.restoreProjectBranch(props.projectId, targetBranchId, {
448
- source_branch_id: pointInTime.branchId,
449
- preserve_under_name: props.preserveUnderName || undefined,
450
- ...(pointInTime.tag === "lsn" && { source_lsn: pointInTime.lsn }),
451
- ...(pointInTime.tag === "timestamp" && {
452
- source_timestamp: pointInTime.timestamp,
453
- }),
454
- }));
455
- const writeInst = writer(props).write(data.branch, {
456
- title: "Restored branch",
457
- fields: ["id", "name", "last_reset_at"],
458
- emptyMessage: "No branches have been restored.",
459
- });
460
- const parentId = data.branch.parent_id;
461
- if (props.preserveUnderName && parentId) {
462
- const { data } = await props.apiClient.getProjectBranch(props.projectId, parentId);
463
- writeInst.write(data.branch, {
464
- title: "Backup branch",
465
- fields: ["id", "name"],
466
- emptyMessage: "Backup branch has not been found.",
467
- });
468
- }
469
- writeInst.end();
470
- };
471
- const setExpiration = async (props) => {
472
- // Accept either branch name or id
473
- const branchId = await branchIdFromProps({
474
- ...props,
475
- id: props.branchId,
476
- });
477
- const expiresAt = typeof props.expiresAt === "string"
478
- ? new Date(props.expiresAt).toISOString()
479
- : null;
480
- const { data } = await retryOnLock(() => props.apiClient.updateProjectBranch(props.projectId, branchId, {
481
- branch: {
482
- expires_at: expiresAt,
483
- },
484
- }));
485
- writer(props).end(data.branch, {
486
- fields: BRANCH_FIELDS,
487
- });
488
- };