> 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/concepts/cennso-db-schema.md).

# Cennso Database Schema

A Cennso Database Schema is represented by the `DBSchema` Custom Resource (CR). Applying this CR to a Management Cluster creates a Kubernetes Job that provisions a logical MySQL database inside an existing [Cennso Database Instance](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-instance.md).

> [!IMPORTANT]
> Before provisioning a logical database, the target `DBInstance` must be deployed, and its HAProxy Kubernetes Service must be reachable from the provisioning Job. The Job connects to the MySQL cluster through that Service and authenticates with the root credentials stored in the `ps-db-<dbInstanceName>-secrets` Kubernetes Secret.

The sections below provide information about configuration parameters that you can specify in the `DBSchema` CR. For more information on how to provision a logical database, see the [Create a database](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/database-creation.md) document.

## Kubernetes cluster secret reference

To deploy the Cennso Database Schema, you must specify a reference to the [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) so the Core Cluster is accessible. You can do this using the following configuration parameters:

```yaml
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: {CORE-CLUSTER-SECRET-NAMESPACE}
```

Set **spec.connectionSecretRef.name** to the name of the Core Cluster that contains the MySQL cluster where the database should be created. The **spec.connectionSecretRef.namespace** configuration parameter defines the Kubernetes Namespace on the Management Cluster where the Kubernetes secret to access this Core Cluster is stored.

## Target Database Instance reference

To select the MySQL cluster where the logical database is created, you must specify the `DBInstance` name and the Kubernetes Namespace on the Core Cluster where it is deployed:

```yaml
spec:
  namespace: {MYSQL-CLUSTER-NAMESPACE}
  dbInstanceName: {DB-INSTANCE-NAME}
```

The **spec.dbInstanceName** must match the **metadata.name** of an existing `DBInstance` claim. The schema-provisioning Job connects to the `ps-db-<dbInstanceName>-haproxy.<namespace>.svc:3306` Kubernetes Service and uses the credentials from the `ps-db-<dbInstanceName>-secrets` Kubernetes Secret.

## Database name, character set, and collation

> [!IMPORTANT]
> The **spec.collate** value must be compatible with the chosen **spec.characterSet**. Incompatible combinations are rejected by MySQL at apply time.

To define the logical database that should be created, use the following configuration parameters:

```yaml
spec:
  databaseName: {DATABASE-NAME}
  characterSet: utf8mb4
  collate: utf8mb4_0900_ai_ci
```

The **spec.databaseName** must be a valid MySQL identifier (alphanumeric characters and underscore, up to 64 characters).

The **spec.characterSet** configures the default character set for the logical database. The supported values are: 

- `utf8mb4`
- `utf8mb3`
- `latin1`
- `ascii`
- `binary` 

The default character set is `utf8mb4`.

The **spec.collate** configures the default collation for the logical database. The supported values are: 

- `utf8mb4_0900_ai_ci`
- `utf8mb4_unicode_ci` 
- `utf8mb4_general_ci`
- `utf8mb4_bin` 

The default value is `utf8mb4_0900_ai_ci`.

For more information about the character sets and collations, see the [official MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/charset.html).

## Job retries, lifetime, and deadline

To control how the schema-provisioning [Kubernetes Job](https://kubernetes.io/docs/concepts/workloads/controllers/job/) behaves on failure and after completion, use the following configuration parameters:

```yaml
spec:
  backoffLimit: 1
  ttlSecondsAfterFinished: 3600
  activeDeadlineSeconds: 600
  restartPolicy: OnFailure
```

The **spec.backoffLimit** configures the number of retries before the Job is marked as failed. For more information, see the [pod backoff failure policy](https://kubernetes.io/docs/concepts/workloads/controllers/job/#pod-backoff-failure-policy) documentation.

The **spec.ttlSecondsAfterFinished** configures how long a finished Job (either `Complete` or `Failed`) is retained before it is automatically cleaned up. If omitted, the Job is not deleted automatically. For more information, see the [TTL mechanism for finished Jobs](https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/) documentation.

The **spec.activeDeadlineSeconds** configures a wall-clock deadline, measured from the Job start time, after which the Job is terminated and marked as failed. The default value is `600` seconds.

The **spec.restartPolicy** configures the restart policy of the Job Pod. The supported values are `OnFailure` and `Never`. The default value is `OnFailure`. For more information, see the [pod lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy) documentation.

## Image and pull secret

You can configure the image of the schema-provisioning Job and the Kubernetes Secret used to pull it with the following configuration options:

```yaml
spec:
  image:
    registry: registry.cennso.com/cennso/mysql-server-client
    tag: v8.0.0
  imagePullSecretRef:
    name: cennso-pull-secret
```

The **spec.imagePullSecretRef.name** refers to a Kubernetes Secret that must exist on the Core Cluster in the same Kubernetes Namespace as the Job.
