> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-db-upgrade.md).

# Upgrade a database

This document describes how to upgrade Cennso Database Operator and Cennso Database Instance. If you need to update the Cennso Database extension itself, read the [Installation](https://docs.cennso.com/cennso/cennso-db/latest/installation.md) document.

You can upgrade both Cennso Database Operator and Cennso Database Instance, or one at a time.

> [!IMPORTANT]
> For this moment, Cennso Database does not support the upgrade of the following sub-components:
>
> - Upgrade to a new major version of MySQL - make sure you do not set the **spec.mysql.image.tag** parameter of the `DBInstance` composition to a newer major version of the MySQL server during upgrade.
> - Upgrade to a new version of Operator - make sure you do not set the **spec.image** and **spec.chart** parameters of the `DBOperator` composition to a newer version of the Operator during upgrade.
>
> The upgrade process of the sub-components mentioned above is impossible because of absence of backward compatibility. If you need to upgrade these sub-components, you should create a new Cennso Database Instance with the desired version and migrate your data.

Before upgrading the database cluster, follow the general recommendations:

- When upgrading the Cennso Database Instance under load, ensure your Cennso Database Operator has an adequate **spec.replicationSyncWaitTimeOnUpgrade** value configured. It is recommended to check the replication lag before the upgrade procedure and set the **spec.replicationSyncWaitTimeOnUpgrade** to not less than `5`.

   > [!IMPORTANT]
   > **Deprecated:** This configuration option will be removed in the next release of the Cennso Database extension. Use **spec.binaryDonor** of the `DBInstance` Custom Resource instead. For more information, see [Enable `binary-donor`](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/binary-donor.md).
- To improve the performance of the upgrade process, you can set the **spec.mysql.cloneRequiredOnAsyncCluster** to `false`. This will skip the cloning process during the upgrade.

## Select the extension version

The Cennso Database extension package ships a Composition for every Custom Resource it provides, such as `DBOperator` and `DBInstance`. Crossplane keeps the history of each Composition as composition revisions. Installing a new version of the package updates the Compositions and, as a result, creates a new composition revision for each of them, labelled with `cennso.com/version` of that package. Installing a new package version does not change existing databases: each `DBOperator` and `DBInstance` Custom Resource stays on the revision it was created with until you move it. A Custom Resource created without **spec.compositionRevisionSelector** is pinned in the same way to the revision that was the newest at the time of its creation.

A Custom Resource chooses its revision with **spec.compositionRevisionSelector**. The revision in use is recorded in **spec.compositionRevisionRef** of the underlying composite resource. Whether a later change of the selector takes effect depends on the value of the **spec.compositionUpdatePolicy** parameter:

- `Manual` (default) - the selector is evaluated only once, when the Custom Resource is created. A later change of the label is accepted without an error but has no effect. To move the Custom Resource to another revision, set **spec.compositionRevisionRef.name** explicitly.
- `Automatic` - the selector is evaluated on every reconciliation, and the Custom Resource follows the newest revision that matches the label. Changing the label is enough to move it. Without a selector, an `Automatic` Custom Resource follows the newest revision after every package installation.

The `Manual` default protects production databases from unintended changes. With `Automatic`, make sure every package version carries a unique `cennso.com/version` label. Otherwise, rebuilding a package with the reused version label changes the Custom Resource without any changes on your side.

To list the installed composition revisions of the MySQL Custom Resources together with their versions, use the following command on the Management Cluster. The MySQL composite resource kinds start with `XDB`, which `grep` uses to exclude other resources:

```bash
kubectl get compositionrevisions.apiextensions.crossplane.io -L cennso.com/version | grep -E 'NAME|XDB'
```

## Upgrade Cennso Database Operator

You can upgrade the Cennso Database Operator using either the Cennso Management Console (CMC) or the CLI:

### Upgrade Cennso Database Operator (CMC)

### CMC

To update the Cennso Database Operator using CMC:

1. Go to **Cennso Management -> All resources** and choose the `DBOperator` tile.
2. Choose the `DBOperator` you want to upgrade.
3. Press the `Edit` button.
4. Go to the **Specification** tab and edit parameters you want to update.
5. If you want to use a different version of the Cennso Database extension, open the **Metadata** tab and adjust the extension version using the **Version** field in the **Composition** section. The **Version** field sets **spec.compositionRevisionSelector**, which has no effect on an existing `DBOperator` because **spec.compositionUpdatePolicy** is `Manual` by default. Open the **Manifest** tab and either set **spec.compositionRevisionRef.name** to the revision of the new version, or set **spec.compositionUpdatePolicy** to `Automatic`, as described in the [Select the extension version](#select-the-extension-version) section.
6. After you've edited all the parameters for the upgrade, press the `Update` button.
7. Open the `DBOperator` again and check **spec.compositionRevisionRef.name** in the **Manifest** tab. It must show the revision of the new version. The database operator Deployment is restarted in the Core Cluster only when the new revision renders it differently.

### Upgrade Cennso Database Operator (CLI)

### CLI

To update the Cennso Database Operator using CLI:

1. Open the manifest with the `DBOperator` Custom Resource which you used to deploy the Cennso Database Operator.

2. Edit the [parameters](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-operator.md) you want to change for the Cennso Database Operator upgrade.

3. If you want to use a different version of the Cennso Database extension, update the **spec.compositionRevisionSelector.matchLabels** in the manifest. This parameter should contain the `cennso.com/version` label with the version associated with the Cennso Database extension.

   To find the available composition revisions and their versions, use the following command:

   ```bash
   kubectl get compositionrevisions.apiextensions.crossplane.io -o jsonpath='{range .items[?(@.spec.compositeTypeRef.kind=="XDBOperator")]}{.metadata.name}{"\t"}{.metadata.labels.cennso\.com\/version}{"\n"}{end}'
   ```

   To update the composition revision associated with the Cennso Database extension, set the `cennso.com/version` label:

   ```yaml
   spec:
     compositionRevisionSelector:
       matchLabels:
         cennso.com/version: {CENNSO-DATABASE-EXTENSION-VERSION}
   ```

   On an existing `DBOperator`, the new label does not take effect because **spec.compositionUpdatePolicy** is `Manual` by default. If you did not change the default value, choose one of the following ways to move the `DBOperator` to the new revision, as described in the [Select the extension version](#select-the-extension-version) section:

   - Set the revision explicitly. Take the revision name for the new version from the output of the command above:

     ```yaml
     spec:
       compositionRevisionRef:
         name: {COMPOSITION-REVISION-NAME}
     ```

   - Set **spec.compositionUpdatePolicy** to `Automatic` to let the selector determine when to update the composition revision:

     ```yaml
     spec:
       compositionUpdatePolicy: Automatic
     ```

4. After you've edited all the parameters, save the manifest and apply it to the Management Cluster using the following command:

   ```bash
   kubectl apply -f cennso-database-operator.yaml -n {NAMESPACE}
   ```

5. Verify that the composite resource behind the `DBOperator` is on the expected revision and is ready:

   ```bash
   kubectl get xdboperators.db.contrib.cennso.com -l crossplane.io/claim-name={DBOPERATOR_NAME} -o custom-columns='NAME:.metadata.name,REVISION:.spec.compositionRevisionRef.name,SYNCED:.status.conditions[?(@.type=="Synced")].status,READY:.status.conditions[?(@.type=="Ready")].status'
   ```

   The **REVISION** column must show the name of the revision labelled with the new version. If it still shows the previous revision, the version label was changed without one of the options from step 3.

   A new revision only changes what differs from the previous one. The database operator Deployment is restarted in the Core Cluster only when the new revision renders it differently, so an unchanged Deployment after a successful switch is expected.

In emergency cases, when you need to upgrade as soon as possible, you can edit the live Custom Resource manifest currently deployed in your Management Cluster. To do this, execute the following steps:

> [!IMPORTANT]
> Changes made using `kubectl edit` are not preserved during redeployment or upgrade of the database operator. Make sure to reflect any modifications in your Custom Resource Definition.

1. Find the `DBOperator` you want to update in the Kubernetes namespace specified by `NAMESPACE` on the Management Cluster. You can list all `DBOperator` resources using the following command:

   ```bash
   kubectl get dboperators.db.contrib.cennso.com -n {NAMESPACE}
   ```

2. Open the `DBOperator` manifest of the Cennso Database Operator you want to update using the following command:

   ```bash
   kubectl edit dboperators.db.contrib.cennso.com {DBOPERATOR_NAME} -n {NAMESPACE}
   ```

3. Save the manifest and close the editor to apply the updated Custom Resource to your Management Cluster.

## Upgrade Cennso Database Instance

You can upgrade the Cennso Database Instance using either the Cennso Management Console (CMC) or the CLI:

### Upgrade Cennso Database Instance (CMC)

### CMC

To update the Cennso Database Instance using CMC:

1. Go to **Cennso Management -> All resources** and choose the `DBInstance` tile.
2. Choose the `DBInstance` you want to upgrade.
3. Press the `Edit` button.
4. Go to the **Specification** tab and edit parameters you want to update.
5. If you want to use a different version of the Cennso Database extension, open the **Metadata** tab and adjust the extension version using the **Version** field in the **Composition** section. The **Version** field sets **spec.compositionRevisionSelector**, which has no effect on an existing `DBInstance` because **spec.compositionUpdatePolicy** is `Manual` by default. Open the **Manifest** tab and either set **spec.compositionRevisionRef.name** to the revision of the new version, or set **spec.compositionUpdatePolicy** to `Automatic`, as described in the [Select the extension version](#select-the-extension-version) section.
6. After you've edited all the parameters for the upgrade, press the `Update` button.
7. Open the `DBInstance` again and check **spec.compositionRevisionRef.name** in the **Manifest** tab. It must show the revision of the new version. The `DBInstance` stays not ready while the composed resources with changed renderings are updated in the Core Cluster. This update may restart the related pods. The `DBInstance` becomes ready again when the upgrade is complete.

### Upgrade Cennso Database Instance (CLI)

### CLI

> [!IMPORTANT]
> MySQL instance can take a long time to get up and running after a restart. Before upgrading the database, make sure that the startup probes are configured with a `failureThreshold` value high enough for the MySQL restore procedure. For more information, see the [Cennso Database Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md) document.

To update the Cennso Database Instance using CLI:

1. Open the manifest with the `DBInstance` Custom Resource which you used to deploy the Cennso Database Instance.

2. Edit the [parameters](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md) you want to change for the Cennso Database Instance upgrade.

3. If you want to use a different version of the Cennso Database extension, update the **spec.compositionRevisionSelector.matchLabels** in the manifest. This parameter should contain the `cennso.com/version` label with the version associated with the Cennso Database extension.

   To find the available composition revisions and their versions, use the following command:

   ```bash
   kubectl get compositionrevisions.apiextensions.crossplane.io -o jsonpath='{range .items[?(@.spec.compositeTypeRef.kind=="XDBInstance")]}{.metadata.name}{"\t"}{.metadata.labels.cennso\.com\/version}{"\n"}{end}'
   ```

   To update the composition revision associated with the Cennso Database extension, set the `cennso.com/version` label:

   ```yaml
   spec:
     compositionRevisionSelector:
       matchLabels:
         cennso.com/version: {CENNSO-DATABASE-EXTENSION-VERSION}
   ```

   On an existing `DBInstance`, the new label does not take effect because **spec.compositionUpdatePolicy** is `Manual` by default. If you did not change the default value, choose one of the following ways to move the `DBInstance` to the new revision, as described in the [Select the extension version](#select-the-extension-version) section:

   - Set the revision explicitly. Take the revision name for the new version from the output of the command above:

     ```yaml
     spec:
       compositionRevisionRef:
         name: {COMPOSITION-REVISION-NAME}
     ```

   - Set **spec.compositionUpdatePolicy** to `Automatic` to let the selector determine when to update the composition revision:

     ```yaml
     spec:
       compositionUpdatePolicy: Automatic
     ```

4. After you've edited all the parameters, save the manifest and apply it to the Management Cluster using the following command:

   ```bash
   kubectl apply -f cennso-database-instance.yaml -n {NAMESPACE}
   ```

5. Verify that the composite resource behind the `DBInstance` is on the expected revision:

   ```bash
   kubectl get xdbinstances.db.contrib.cennso.com -l crossplane.io/claim-name={DBINSTANCE_NAME} -o custom-columns='NAME:.metadata.name,REVISION:.spec.compositionRevisionRef.name,SYNCED:.status.conditions[?(@.type=="Synced")].status,READY:.status.conditions[?(@.type=="Ready")].status'
   ```

   The **REVISION** column must show the name of the revision labelled with the new version. If it still shows the previous revision, the version label was changed without one of the options from step 3.

6. Wait for the upgrade to finish. After the switch, the composed resources whose rendering changed in the new revision are updated in the Core Cluster. Depending on the change, this restarts the related pods, such as the MySQL exporters, the `binary-donor` Deployments, or the MySQL instances themselves. The composite resource reports `READY` as `False` until its composed resources are updated and all become ready again. It then reports `READY` as `True`.

   While the MySQL instances are being restarted, watch the state of the MySQL cluster in the Core Cluster:

   ```bash
   kubectl get perconaservermysqls.ps.percona.com -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

   The upgrade is complete when the composite resource is `READY` and the **STATE** column of the MySQL cluster shows `ready`.

In emergency cases, when you need to upgrade as soon as possible, you can edit the live Custom Resource manifest currently deployed in your Management Cluster. To do this, execute the following steps:

> [!IMPORTANT]
> Changes made using `kubectl edit` are not preserved during redeployment or upgrade of the MySQL cluster. Make sure to reflect any modifications in your Custom Resource Definition.

1. Find the `DBInstance` you want to update in the Kubernetes namespace specified by `NAMESPACE` on the Management Cluster. You can list all `DBInstance` resources using the following command:

   ```bash
   kubectl get dbinstances.db.contrib.cennso.com -n {NAMESPACE}
   ```

2. Open the `DBInstance` manifest of the Cennso Database Instance you want to update using the following command:

   ```bash
   kubectl edit dbinstances.db.contrib.cennso.com {DBINSTANCE_NAME} -n {NAMESPACE}
   ```

3. Save the manifest and close the editor to apply the updated Custom Resource to your Management Cluster.

## Roll back an upgrade

A rollback to the previous version of the Cennso Database extension uses the same mechanism as the upgrade. The composition revisions of the previous package version stay available on the Management Cluster after a newer version is installed, so you do not need to reinstall the previous package.

To roll back a `DBOperator` or `DBInstance`, point it at the previous revision in the same way you moved it forward. If **spec.compositionUpdatePolicy** is set to `Automatic`, you can only set the previous version in the `cennso.com/version` label of the selector. Otherwise, you should set **spec.compositionRevisionRef.name** to the previous revision name.

Then, apply the manifest and verify the revision as described in the upgrade steps above.
