0

Migrate a Database Cluster to a New Region

by
Published Dec 20, 2024

To migrate a database cluster to a new region, send a `PUT` request to `/v2/databases/$DATABASE_ID/migrate`. The body of the request must specify a `region` attribute. A successful request will receive a 202 Accepted status code with no body in response. Querying the database cluster will show that its `status` attribute will now be set to `migrating`. This will transition back to `online` when the migration has completed.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Migrate a Database Cluster to a New Region
7
 * To migrate a database cluster to a new region, send a `PUT` request to
8
`/v2/databases/$DATABASE_ID/migrate`. The body of the request must specify a
9
`region` attribute.
10

11
A successful request will receive a 202 Accepted status code with no body in
12
response. Querying the database cluster will show that its `status` attribute
13
will now be set to `migrating`. This will transition back to `online` when the
14
migration has completed.
15

16
 */
17
export async function main(
18
  auth: Digitalocean,
19
  database_cluster_uuid: string,
20
  body: { region: string },
21
) {
22
  const url = new URL(
23
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/migrate`,
24
  );
25

26
  const response = await fetch(url, {
27
    method: "PUT",
28
    headers: {
29
      "Content-Type": "application/json",
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40