a7fdbbb347602ed2c2ef66d16bae77187a06ca55
[policy/docker.git] / policy-db-migrator / src / main / docker / prepare_downgrade.sh
1 #!/bin/sh
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2021-2022 Nordix Foundation.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 if [ $# -lt 1 ]; then
21     echo "args: schema-name" >&2
22     exit 1
23 fi
24
25 export SCHEMA=$1
26 export POLICY_HOME=/opt/app/policy
27 export operation=downgrade
28
29 cd $POLICY_HOME
30
31 # Create schema directory for upgrade
32 mkdir -p $POLICY_HOME/etc/db/migration/${SCHEMA}/${SCRIPT_DIRECTORY}/
33 # Remove any files from previous operations
34 rm -rf $POLICY_HOME/etc/db/migration/${SCHEMA}/${SCRIPT_DIRECTORY}/* 2>/dev/null
35 # Copy files to downgrade directories
36 cd /home/policy/${SCRIPT_DIRECTORY} && find . -type f  -not -path '*/upgrade/*' -not -path '*/upgrade'  -print0  \
37    | cpio --null -pud $POLICY_HOME/etc/db/migration/${SCHEMA}/${SCRIPT_DIRECTORY}/
38
39 releases=$(find $POLICY_HOME/etc/db/migration/${SCHEMA}/${SCRIPT_DIRECTORY}/*/downgrade -type d | sort -u | rev | cut -f2 -d/ | rev)
40 for release in $releases
41 do
42         echo "Preparing $operation release version: $release"
43 done
44 echo "Done"