|
| 1 | +# db-migrator Helm chart |
| 2 | + |
| 3 | +Reusable Helm chart that runs [`raoptimus/db-migrator`](https://hub.docker.com/r/raoptimus/db-migrator) |
| 4 | +as a Kubernetes `Job`. |
| 5 | + |
| 6 | +- **`release`** — applies all pending migrations atomically. Rendered as a Helm hook |
| 7 | + (`pre-install,pre-upgrade` by default), so the schema is ready **before** the application |
| 8 | + pods roll out. |
| 9 | +- **`rollback`** — reverts the latest release batch. Rendered as an opt-in plain `Job` |
| 10 | + (disabled by default), triggered explicitly. It is not a lifecycle hook, so it works the |
| 11 | + same under both plain Helm and [werf](https://werf.io) `converge`. |
| 12 | + |
| 13 | +The chart works standalone (`helm install`) and as a subchart dependency of an application |
| 14 | +chart. `INTERACTIVE` is always forced to `false` because a Job has no TTY. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- Kubernetes cluster and Helm 3. |
| 19 | +- A container image that contains your migration files at `migrator.path` |
| 20 | + (default `/migrations`). The base image ships **no** migrations — see |
| 21 | + [Providing migrations](#providing-migrations). |
| 22 | +- An existing `Secret` holding the database DSN. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +kubectl create secret generic db-migrator-dsn \ |
| 28 | + --from-literal=dsn='postgres://user:pass@postgres:5432/app?sslmode=disable' |
| 29 | + |
| 30 | +helm install my-migrations ./charts/db-migrator \ |
| 31 | + --set image.repository=myregistry/myapp-migrations \ |
| 32 | + --set image.tag=1.4.2 \ |
| 33 | + --set migrator.dsn.existingSecret=db-migrator-dsn |
| 34 | +``` |
| 35 | + |
| 36 | +## Use as a subchart dependency |
| 37 | + |
| 38 | +```yaml |
| 39 | +# Chart.yaml of your application chart |
| 40 | +dependencies: |
| 41 | + - name: db-migrator |
| 42 | + version: "0.1.0" |
| 43 | + repository: "oci://<registry>/charts" # or https://<repo>, or file://../charts/db-migrator |
| 44 | +``` |
| 45 | +
|
| 46 | +```yaml |
| 47 | +# values.yaml of your application chart (values go under the "db-migrator" key) |
| 48 | +db-migrator: |
| 49 | + image: |
| 50 | + repository: myregistry/myapp-migrations |
| 51 | + tag: "1.4.2" |
| 52 | + migrator: |
| 53 | + dsn: |
| 54 | + existingSecret: myapp-db |
| 55 | + secretKey: dsn |
| 56 | + path: /migrations |
| 57 | +``` |
| 58 | +
|
| 59 | +Then `helm dependency update && helm upgrade --install ...` — the `release` hook runs on |
| 60 | +every install/upgrade before the app pods start. |
| 61 | + |
| 62 | +## Rolling back |
| 63 | + |
| 64 | +`rollback` is opt-in. Enable it explicitly when you actually want to revert the latest batch: |
| 65 | + |
| 66 | +```bash |
| 67 | +# werf or plain Helm |
| 68 | +helm upgrade --install my-migrations ./charts/db-migrator \ |
| 69 | + --reuse-values --set rollback.enabled=true |
| 70 | +``` |
| 71 | + |
| 72 | +Under **plain Helm** you may instead wire rollback to `helm rollback` by turning the Job into |
| 73 | +a hook (this does not fire under werf converge): |
| 74 | + |
| 75 | +```bash |
| 76 | +helm install ... --set 'rollback.hookTypes={pre-rollback}' |
| 77 | +helm rollback my-migrations |
| 78 | +``` |
| 79 | + |
| 80 | +## Providing migrations |
| 81 | + |
| 82 | +The base image contains no migrations. Recommended: build your own image. |
| 83 | + |
| 84 | +```dockerfile |
| 85 | +FROM raoptimus/db-migrator:1.7.0 |
| 86 | +COPY ./migrations /migrations |
| 87 | +``` |
| 88 | + |
| 89 | +Alternatively, mount migrations without rebuilding the image via passthrough values, e.g. a |
| 90 | +ConfigMap: |
| 91 | + |
| 92 | +```yaml |
| 93 | +extraVolumes: |
| 94 | + - name: migrations |
| 95 | + configMap: |
| 96 | + name: myapp-migrations |
| 97 | +extraVolumeMounts: |
| 98 | + - name: migrations |
| 99 | + mountPath: /migrations |
| 100 | +migrator: |
| 101 | + path: /migrations |
| 102 | +``` |
| 103 | + |
| 104 | +`initContainers` is also passed through (e.g. for a git-sync sidecar populating an |
| 105 | +`emptyDir`). |
| 106 | + |
| 107 | +## Values |
| 108 | + |
| 109 | +| Key | Default | Description | |
| 110 | +|-----|---------|-------------| |
| 111 | +| `image.repository` | `raoptimus/db-migrator` | Image containing the migrations | |
| 112 | +| `image.tag` | `""` (→ `.Chart.AppVersion`) | Image tag | |
| 113 | +| `image.pullPolicy` | `IfNotPresent` | Image pull policy | |
| 114 | +| `imagePullSecrets` | `[]` | Image pull secrets | |
| 115 | +| `migrator.dsn.existingSecret` | `""` (**required**) | Secret holding the DSN | |
| 116 | +| `migrator.dsn.secretKey` | `dsn` | Key inside the Secret | |
| 117 | +| `migrator.path` | `/migrations` | `MIGRATION_PATH` | |
| 118 | +| `migrator.table` | `migration` | `MIGRATION_TABLE` | |
| 119 | +| `migrator.clusterName` | `""` | `MIGRATION_CLUSTER_NAME` (ClickHouse) | |
| 120 | +| `migrator.replicated` | `false` | `MIGRATION_REPLICATED` (ClickHouse) | |
| 121 | +| `migrator.maxConnAttempts` | `1` | `MAX_CONN_ATTEMPTS` | |
| 122 | +| `migrator.compact` | `false` | `COMPACT` | |
| 123 | +| `migrator.dryRun` | `false` | `DRY_RUN` | |
| 124 | +| `migrator.placeholderCustom` | `""` | `PLACEHOLDER_CUSTOM` | |
| 125 | +| `migrator.extraEnv` | `[]` | Extra env entries | |
| 126 | +| `migrator.extraEnvFrom` | `[]` | Extra `envFrom` sources | |
| 127 | +| `release.enabled` | `true` | Render the release hook Job | |
| 128 | +| `release.command` | `release` | Command (`release` or `up`) | |
| 129 | +| `release.hookTypes` | `[pre-install, pre-upgrade]` | Helm hook phases | |
| 130 | +| `release.weight` | `"5"` | Hook weight | |
| 131 | +| `release.deletePolicy` | `before-hook-creation` | Hook delete policy | |
| 132 | +| `release.annotations` | `{werf.io/fail-mode: FailWholeDeployProcessImmediately}` | Extra Job annotations | |
| 133 | +| `rollback.enabled` | `false` | Render the rollback Job (opt-in) | |
| 134 | +| `rollback.command` | `rollback` | Command | |
| 135 | +| `rollback.hookTypes` | `[]` | Empty = plain Job; `[pre-rollback]` = hook | |
| 136 | +| `rollback.weight` | `"5"` | Hook weight (when hookTypes set) | |
| 137 | +| `rollback.deletePolicy` | `before-hook-creation` | Hook delete policy | |
| 138 | +| `rollback.annotations` | `{}` | Extra Job annotations | |
| 139 | +| `backoffLimit` | `0` | Job `backoffLimit` | |
| 140 | +| `activeDeadlineSeconds` | `3600` | Job `activeDeadlineSeconds` | |
| 141 | +| `ttlSecondsAfterFinished` | `30` | Job TTL after completion | |
| 142 | +| `resources` | `{}` | Container resources | |
| 143 | +| `serviceAccount.create` | `false` | Create a ServiceAccount | |
| 144 | +| `serviceAccount.name` | `""` | ServiceAccount name | |
| 145 | +| `nodeSelector` / `tolerations` / `affinity` | `{}` / `[]` / `{}` | Scheduling | |
| 146 | +| `podSecurityContext` / `securityContext` | `{}` | Security contexts | |
| 147 | +| `initContainers` / `extraVolumes` / `extraVolumeMounts` | `[]` | Migration delivery passthrough | |
0 commit comments