Skip to content

Commit

Permalink
TRUNK-6058 : generate liquibase snapshots automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojLL committed Nov 13, 2023
1 parent 2d0dbe1 commit 4dc9131
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
133 changes: 133 additions & 0 deletions liquibase/scripts/generate_liquibase_snapshots.sh
@@ -0,0 +1,133 @@
#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
# the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
#
# Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
# graphic logo is a trademark of OpenMRS Inc.
#

function create_and_drop_database() {
# Run MySQL command with input from the specified SQL file
mysql -u "${1}" -p"${2}" < "liquibase/scripts/drop_openmrs_schema.sql"
mysql -u "${1}" -p"${2}" < "liquibase/scripts/create_openmrs_database.sql"

}

function run_openmrs_core_application() {
# Change directory to the specified root folder
# Run 'mvn clean install'
mvn clean install

# Change directory to the 'webapp' folder
cd webapp

# Remove the ~/.OpenMRS directory
rm -r ~/.OpenMRS

# Run 'mvn jetty:run'
mvn jetty:run &

sleep 20

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 13, 2023

I'm not sure the sleep should be part of this function. Let it be part of the script, so it's obvious where it's happening.

}

function open_web_browser_and_kill_java_server() {
# Open the installation URL in the default web browser
installation_url="http://localhost:8080/openmrs/initialsetup"
if command -v xdg-open &> /dev/null; then
xdg-open "$installation_url"
elif command -v open &> /dev/null; then
open "$installation_url"
else
echo "Unsupported operating system"
fi

# Optionally, you can add cleanup steps or additional actions here.

# Pause to keep the script running while the browser opens
read -p "Press Enter to continue..."

# Stop Jetty (assuming the installation process is complete)
killall java

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 13, 2023

I'd store the PID in run_openmrs_core_application and have that be the return value from there. Then we can use kill $OPENMRS_PID. Much cleaner.

This comment has been minimized.

Copy link
@ManojLL

ManojLL Nov 13, 2023

Author Owner

@ibacher i will look on it

cd ..
}

function generate_liquibase_snapshots_and_update() {
cd liquibase/
. scripts/create_liquibase_snapshots.sh "${1}" "${2}"
. scripts/fix_liquibase_snapshots.sh
cd ..
}

function move_updated_snapshots() {
mv liquibase/snapshots/liquibase-core-data-UPDATED-SNAPSHOT.xml api/src/main/resources/org/openmrs/liquibase/snapshots/core-data/"liquibase-core-data-$openMRS_version.x.xml"
mv liquibase/snapshots/liquibase-schema-only-UPDATED-SNAPSHOT.xml api/src/main/resources/org/openmrs/liquibase/snapshots/schema-only/"liquibase-schema-only-$openMRS_version.x.xml"
}

function create_new_liquibase_update_file() {
new_liquibase_file="liquibase-update-to-latest-${new_openmrs_version}.x.xml"
cp "api/src/main/resources/liquibase-update-to-latest-template.xml" "api/src/main/resources/org/openmrs/liquibase/updates/$new_liquibase_file"

database_updater_class="api/src/test/java/org/openmrs/util/DatabaseUpdaterDatabaseIT.java"
variable_name="CHANGE_SET_COUNT_FOR_GREATER_THAN_2_1_X"

# Increase the variable value by 1
sed -i "s/\($variable_name *= *\)[0-9]\+/\1$(( $(grep -o "$variable_name *= *[0-9]\+" "$database_updater_class" | awk '{print $NF}') + 1 ))/" "$database_updater_class"

insert_line=" <include file=\"org/openmrs/liquibase/updates/$new_liquibase_file\"/>"
sed -i -e '$!N;$!N;$i\'"$insert_line" "api/src/main/resources/liquibase-update-to-latest-from-1.9.x.xml"

}

function update_log_files_with_new_snapshots() {
sed -i 's/\(<include file="org\/openmrs\/liquibase\/snapshots\/schema-only\/liquibase-schema-only-\)\([^"]*\)\.x\.xml"\/>/\1'"$openMRS_version"'\.x.xml"\/>/' "api/src/main/resources/liquibase-schema-only.xml"
sed -i 's/\(<include file="org\/openmrs\/liquibase\/snapshots\/core-data\/liquibase-core-data-\)\([^"]*\)\.x\.xml"\/>/\1'"$openMRS_version"'\.x.xml"\/>/' "api/src/main/resources/liquibase-core-data.xml"
sed -i 's/\(<include file="org\/openmrs\/liquibase\/updates\/liquibase-update-to-latest-\)\([^"]*\)\.x\.xml"\/>/\1'"${new_openmrs_version}"'\.x.xml"\/>/' "api/src/main/resources/liquibase-update-to-latest.xml"
}

function update_snapshot_tests() {
change_log_java_file="api/src/main/java/org/openmrs/liquibase/ChangeLogVersions.java"
# Use sed to add the new version to the lists in the Java class
sed -i '/private static final List<String> SNAPSHOT_VERSIONS = Arrays.asList(/s/);/,\ "'"${openMRS_version}"'"\);/' "$change_log_java_file"

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 13, 2023

Does this mean we're now only keeping track of a single snapshot version?

This comment has been minimized.

Copy link
@ManojLL

ManojLL Nov 13, 2023

Author Owner

@ibacher No, we already have list of existing versions like [2.1, 2.2, 2.3...] , from this function new snapshot version append to the existing list.

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 13, 2023

Ok, so the follow-up question here (and my knowledge of sed is a bit rust, so I wasn't entire sure what this did) is if we run this every time, do we end up with something like [2.1, 2.2, 2.3, 2.4, ..., 2.7, 2.7, 2.7, 2.7]?

sed -i '/private static final List<String> UPDATE_VERSIONS = Arrays.asList(/s/);/,\ "'"${new_openmrs_version}"'"\);/' "$change_log_java_file"

sed -i "s/\(openmrs_version.txt=\).*/\1${new_openmrs_version}.0-SNAPSHOT/" "liquibase/scripts/fix_liquibase_snapshots.sh"

}

function echo_usage() {
echo "usage: . scripts/test_liquibase_snapshots.sh <username> <password>"
}

config_file="liquibase/scripts/openmrs_version.txt"

# Function to read the value of a key from the config file
read_config() {
key=$1
value=$(grep "^$key=" "$config_file" | cut -d '=' -f 2)
echo "$value"
}

# Function to update the value of a key in the config file
update_config() {
key=$1
new_value=$2
sed -i "s/^$key=.*/$key=$new_value/" "$config_file"
}

# Read the current openmrs_version from the config file
openMRS_version=$(read_config "openmrs_version")
new_openmrs_version=$(echo "$openMRS_version + 0.1" | bc)

create_and_drop_database "${1}" "${2}"
run_openmrs_core_application
open_web_browser_and_kill_java_server
generate_liquibase_snapshots_and_update "${1}" "${2}"
move_updated_snapshots
create_new_liquibase_update_file
update_log_files_with_new_snapshots
update_snapshot_tests

update_config "openmrs_version" "$new_openmrs_version"
11 changes: 11 additions & 0 deletions liquibase/scripts/openmrs_version.txt
@@ -0,0 +1,11 @@
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
# the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
#
# Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
# graphic logo is a trademark of OpenMRS Inc.
#

openmrs_version=2.7

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 13, 2023

Seems like it would be better to have this be a file that's generated from Maven, where we could just plug-in the actual version...

This comment has been minimized.

Copy link
@ManojLL

ManojLL Nov 14, 2023

Author Owner

@ibacher How this can generated from maven ?

This comment has been minimized.

Copy link
@ibacher

ibacher Nov 15, 2023

@ManojLL If you treat it as a Maven Resource you can apply filtering to it, which allows you to substitute Maven properties, so something like ${project.version} would get you the project version and something like:

openmrs_version=$(echo ${project.version} | sed "s/\([0-9]*\.[0-9]*\)\..*/\1/")

Albeit as a shell script, should get you the value you want.

0 comments on commit 4dc9131

Please sign in to comment.