Migration with Multiple Schema
Certain services in Central-instance will only be deployed in the central (Digit) namespace but their data needs to be stored in different schema belonging to different state namespace. The migrations of foretold services need to be run in all Required schemas(i e namespaces who want to make use of the centrally deployed services). To enable the above functionality the following changes are required.
1.Migrate.sh
This file is needed for building migration docker image is located in the DB folder of resources. eg: property-services/src/main/resources/db/migrate.sh . update the file with the following contents.
#!/bin/sh
baseurl=$DB_URL
echo "the baseurl : $DB_URL"
schemasetter="?currentSchema="
schemas=$SCHEMA_NAME
echo "the schemas : $schemas"
for schemaname in ${schemas//,/ }
do
echo "the schema name : ${baseurl}${schemasetter}${schemaname}"
flyway -url=${baseurl}${schemasetter}${schemaname} -table=$SCHEMA_TABLE -user=$FLYWAY_USER -password=$FLYWAY_PASSWORD -locations=$FLYWAY_LOCATIONS -baselineOnMigrate=true -outOfOrder=true -ignoreMissingMigrations=true migrate
done
2. values.yml
The DB configuration is by default provided in the common values.yml file in the helm chart and need not be edited in normal scenarios. Since we are injecting the multiple schema list for app-specific purposes all the DB-migration ENV variables should be overridden in the app-specific values.yml file.
#Values.yml of the specific service before multiple schema changes
# Init Containers Configs
initContainers:
dbMigration:
enabled: true
schemaTable: "property_services_schema"
image:
repository: "property-services-db"
#Values.yml of the specific service after multiple schema changes
https://github.com/egovernments/DIGIT-DevOps/blob/central-instance/deploy-as-code/helm/charts/municipal-services/property-services/values.yaml - the full updated sample file can be found here
# Init Containers Configs
initContainers:
dbMigration:
enabled: true
schemaTable: "property_services_schema"
image:
repository: "property-services-db"
env: |
{{- if index .Values "property-ismultischema-enabled" }}
- name: SCHEMA_NAME
value: {{ index .Values "property-schemas" | quote }}
{{- end }}
- name: "DB_URL"
valueFrom:
configMapKeyRef:
name: egov-config
key: db-url
- name: "SCHEMA_TABLE"
value: {{ .Values.initContainers.dbMigration.schemaTable | quote }}
- name: "FLYWAY_USER"
valueFrom:
secretKeyRef:
name: db
key: flyway-username
- name: "FLYWAY_PASSWORD"
valueFrom:
secretKeyRef:
name: db
key: flyway-password
- name: "FLYWAY_LOCATIONS"
valueFrom:
configMapKeyRef:
name: egov-config
key: flyway-locations
Only the - name: SCHEMA_NAME
variable needs to be added, other fields can be copied from the common values.yml as it is without any changes. The above file contains an if-else condition with two new variables
The "property-ismultischema-enabled"
Boolean value to check if the feature is enabled and "property-schemas"
to derive the value from respective environments YAML. This example of two variables should be named differently based on service-name to avoid conflicts.
3. Environments.yml
In the respective environment’s file add the two new variables, one for the if condition and the other field for actual values under the respective service names. line 6 and 7 are the sample values provided here.
eg :