1 .. This work is licensed under a Creative Commons Attribution
2 .. 4.0 International License.
3 .. http://creativecommons.org/licenses/by/4.0
5 Using Policy DB Migrator
6 ########################
8 Policy DB Migrator is a set of shell scripts used to
9 install the database tables required to run ONAP Policy Framework.
12 Currently the Istanbul versions of the PAP and API components require
13 ``db-migrator`` to run prior to initialization.
18 Policy DB Migrator is run as a docker container and consists of the following scripts:
29 ``prepare_upgrade.sh`` is included as part of the docker image and is used
30 to copy the upgrade sql files to the run directory.
31 This script takes one parameter: <SCHEMA NAME>.
33 ``prepare_downgrade.sh`` is included as part of the docker image and is used
34 to copy the downgrade sql files to the run directory.
35 This script takes one parameter: <SCHEMA NAME>.
37 ``db-migrator`` and ``db-migrator-pg`` are included as part of the docker image
38 and are used to run either the upgrade or downgrade operation for MariaDB or
39 PostgresSQL depending on user requirements.
40 These script can take up to four parameters:
51 - upgrade/downgrade/report
62 The container also consists of several sql files which are used to upgrade/downgrade
65 The following environment variables need to be set to enable ``db-migrator``
66 to run and connect to the database.
87 The following environment variables need to be set to enable ``db-migrator-pg``
88 to run and connect to the database.
114 Prior to upgrading the following script is run:
118 /opt/app/policy/bin/prepare_upgrade.sh <SCHEMA NAME>
120 This will copy the upgrade files from ``/home/policy/${SCRIPT_DIRECTORY}`` to ``$POLICY_HOME/etc/db/migration/<SCHEMA NAME>/${SCRIPT_DIRECTORY}/``
122 Each individual sql file that makes up that release will be run as part of the upgrade.
128 Prior to downgrading the following script is run:
131 /opt/app/policy/bin/prepare_downgrade.sh <SCHEMA NAME>
133 This will copy the downgrade files from ``/home/policy/${SCRIPT_DIRECTORY}`` to ``$POLICY_HOME/etc/db/migration/<SCHEMA NAME>/${SCRIPT_DIRECTORY}/``
135 Each individual sql file that makes up that release will be run as part of the downgrade.
142 /opt/app/policy/bin/db-migrator -s <SCHEMA NAME> -o upgrade -f 0800 -t 0900
148 /opt/app/policy/bin/db-migrator-pg -s <SCHEMA NAME> -o upgrade -f 0800 -t 0900
150 If the ``-f`` and ``-t`` flags are not specified, the script will attempt to run all available
151 sql files greater than the current version.
153 The script will return either 1 or 0 depending on successful completion.
160 /opt/app/policy/bin/db-migrator -s <SCHEMA NAME> -o downgrade -f 0900 -t 0800
166 /opt/app/policy/bin/db-migrator-pg -s <SCHEMA NAME> -o downgrade -f 0900 -t 0800
168 If the ``-f`` and ``-t`` flags are not specified, the script will attempt to run all available
169 sql files less than the current version.
171 The script will return either 1 or 0 depending on successful completion.
176 After every upgrade/downgrade ``db-migrator`` or ``db-migrator-pg`` runs the report operation
177 to show the contents of the db-migrator log table.
181 /opt/app/policy/bin/db-migrator -s <SCHEMA NAME> -o report
185 /opt/app/policy/bin/db-migrator-pg -s <SCHEMA NAME> -o report
187 Console output will also show the sql script command as in the example below:
191 upgrade 0100-jpapdpgroup_properties.sql
193 CREATE TABLE IF NOT EXISTS jpapdpgroup_properties (name VARCHAR(120) NULL, version VARCHAR(20) NULL,
194 PROPERTIES VARCHAR(255) NULL, PROPERTIES_KEY VARCHAR(255) NULL)
200 The migration schema contains two tables which belong to ``db-migrator``.
202 * schema_versions - table to store the schema version currently installed by ``db-migrator``
213 * policyadmin_schema_changelog - table which stores a record of each sql file that has been run
216 :widths: 10 40 10 10 10 20 10 20
228 - 0100-jpapdpgroup_properties.sql
234 - 2021-09-13 09:09:26
236 * ID: Sequence number of the operation
237 * script: name of the sql script which was run
238 * operation: operation type - upgrade/downgrade
239 * from_version: starting version
240 * to_version: target version
241 * tag: tag to identify operation batch
242 * success: 1 if script succeeded and 0 if it failed
243 * atTime: time script was run
246 Partial Upgrade/Downgrade
247 =========================
249 If an upgrade or downgrade ends with a failure status (success=0) the next time an upgrade
250 or downgrade is run it will start from the point of failure rather than re-run scripts
251 that succeeded. This allows the user to perform a partial upgrade or downgrade depending
252 on their requirements.
257 The script that runs ``db-migrator`` is part of the database configuration and is in the following directory:
261 oom/kubernetes/policy/resources/config/db_migrator_policy_init.sh
263 This script is mounted from the host file system to the policy-db-migrator container.
264 It is setup to run an upgrade by default.
268 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
269 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade
271 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report
276 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
277 /opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o upgrade
279 /opt/app/policy/bin/db-migrator-pg -s ${SQL_DB} -o report
282 The following table describes what each line does:
290 * - /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
291 - prepare the upgrade scripts for the <SQL_DB> schema
292 * - /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade
295 - assign the return code from db-migrator to a variable
296 * - /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report
297 - run the db-migrator report for the <SQL_DB> schema
299 - exit with the return code from db-migrator
301 To alter how ``db-migrator`` is run the first two lines need to be modified.
302 The first line can be changed to call either ``prepare_upgrade.sh`` or ``prepare_downgrade.sh``.
303 The second line can be changed to use different input parameters for ``db-migrator`` :
319 - current version (e.g. 0800)
322 - target version (e.g. 0900)
325 This is an example of how a downgrade from version 0900 to version 0800 could be run:
329 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
330 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
332 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report
335 Additional Information
336 ======================
337 If the target version of your upgrade or downgrade is the same as the current version,
338 no sql files are run.
340 If an upgrade is run on a database where tables already exist in the policy schema, the
341 current schema version is set to 0800 and only sql scripts from later versions are run.
344 It is advisable to take a backup of your database prior to running this utility.
345 Please refer to the mariadb documentation on how to do this.