Skip to content

Commit

Permalink
SPSH-595: Datenbankschema Migrationen (#495)
Browse files Browse the repository at this point in the history
* add command for migration, add migrations pckg to project, add tests

* adjust docs

* adjust README

* add migrate up command

* split db-migration commands in different console-files

* adjust migration docu

* add initial migration

* trigger pipeline run

* make migration available in cluster

* add migration docu for DEV and TEST

* change migration config

* fix typo README
  • Loading branch information
DPDS93CT authored May 22, 2024
1 parent 7ec1d59 commit f28c782
Show file tree
Hide file tree
Showing 19 changed files with 2,157 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ web_modules/
# config files
config/secrets.json

# migration files only used for (integration-)tests
test-migrations/

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ You can also use another profile, if you want to initialize the db while startin
* Configuration conventions can be found [here](./docs/config.md)
* Authentication guides can be found [here](./docs/auth.md)
* Developer notes can be found [here](./docs/developer-notes.md)
* Seeding notes can be found [here](./docs/seeding.md)
* Migration notes can be found [here](./docs/migration.md)

## Testing Guides
* Help on how to test API with Insomnia can be found at [API manuell lokal testen mit Insomnia](./docs/test-api-with-insomnia.md)
Expand Down
2 changes: 1 addition & 1 deletion charts/dbildungs-iam-server/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"LDAP": {
"URL": "ldap://dbildungs-iam-server-ldap",
"BIND_DN": "cn=admin,dc=spsh,dc=de",
"BIND_DN": "cn=admin,dc=schule-sh,dc=de",
"PASSWORD": "admin"
},
"DATA": {
Expand Down
33 changes: 33 additions & 0 deletions charts/dbildungs-iam-server/migrations/Migration20240515131039.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions charts/dbildungs-iam-server/templates/backend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ spec:
automountServiceAccountToken: false
{{- if .Values.backend.dbseeding.enabled }}
initContainers:

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.

Check warning on line 23 in charts/dbildungs-iam-server/templates/backend-deployment.yaml

View workflow job for this annotation

GitHub Actions / scan_helm / Kics Helm Chart Scan

[MEDIUM] Container Running With Low UID

Check if containers are running with low UID, which might cause conflicts with the host's user table.
- name: "{{ template "common.names.name" . }}-db-migrate"
image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.backend.image.pullPolicy | default "Always" }}
securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
command: [ "node", "dist/src/console/main.js", "db", "migration-apply" ]
env:
{{- include "dbildungs-iam-server-backend-envs" . | indent 12 }}
{{- if .Values.backend.extraEnvVars }}
{{ toYaml .Values.backend.extraEnvVars | nindent 12 }}
{{- end }}
envFrom:
- configMapRef:
name: {{ template "common.names.name" . }}
volumeMounts:
{{- toYaml .Values.backend.volumeMounts | nindent 12 }}
- mountPath: /app/dist/migrations
name: migration-volume
readOnly: true
resources: {{- toYaml .Values.backend.resources | nindent 12 }}
- name: "{{ template "common.names.name" . }}-db-init"
image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.backend.image.pullPolicy | default "Always"}}
Expand Down Expand Up @@ -90,6 +109,9 @@ spec:
- name: secret-volume
secret:
secretName: {{ default .Values.auth.existingSecret .Values.auth.secretName }}
- name: migration-volume
configMap:
name: {{ template "common.names.name" . }}-backend-migration
{{if .Values.backend.dbseeding.enabled }}
- name: seeding-volume
configMap:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "common.names.name" . }}-backend-migration
namespace: {{ template "common.names.namespace" . }}
labels:
{{- include "common.labels" . | nindent 4 }}
data:
{{ (.Files.Glob "migrations/*").AsConfig | indent 2 }}
2 changes: 1 addition & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"LDAP": {
"URL": "ldap://localhost",
"BIND_DN": "cn=admin,dc=spsh,dc=de",
"BIND_DN": "cn=admin,dc=schule-sh,dc=de",
"PASSWORD": "admin"
},
"DATA": {
Expand Down
51 changes: 51 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Migration via Mikro-Orm Extension

## Commands

Run `npm run db:migration-create` to create a new migration

Run `npm run db:migration-apply` apply the latest version of migration to the database, latest migration files MUST BE available in `./dist/migrations`.
You can ensure this by starting the app via `npm run start`.

Run `npm run db:migration-init` to create a new initial migration (not necessary anymore, initial migration has been done)
The execution of migration-init can cause following error with current configuration:
```bash
...
[Error: ENOENT: no such file or directory, open '.../dbildungs-iam-server/migrations/.snapshot-dbildungs-iam-server.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '...dbildungs-iam-server/migrations/.snapshot-dbildungs-iam-server.json'
}
```

To be able to execute migration-init, temporally adjust `path` in migrations-configuration of console.module.ts
```typescript
extensions: [Migrator],
migrations: {
tableName: 'mikro_orm_migrations'
path: './dist/migrations',
pathTs: './migrations',
```
## Execution
### Noteworthy
- The parameter `disableForeignKeys` has to be false for execution in cluster, permissions are not sufficient.
- Local the usage of `disableForeignKeys: true` is possible.
### On branched enviroments
`npm run db:migration-apply` is run via pipeline for the individual environments.
### On DEV and TEST
Like with some other tasks regarding database, migration on DEV and TEST is easier to apply, if tables or whole schema can be dropped beforehand.
## Files
Migration files are stored in ./migrations
## Tests
For testing purposes of the migration-console another directory `./test-migrations` is created during test execution.
This directory is deleted before test-execution to avoid errors regarding existing migration-files, it is also included in the [.gitignore](./../.gitignore)
Loading

0 comments on commit f28c782

Please sign in to comment.