- 20 May 2009 -
OpenMRS recently added a non-null uuid column to every table. All of our dbunit xml files needed to be updated to insert values for this attribute. I wrote the following shell script to loop over all xml files under the current folder and add a uuid attribute to all rows that didn’t have an attribute already. There is a bit of logic in there to ignore certain tables because those tables didn’t actually get any uuids.
You also can download the add_uuids script.
#!/bin/sh
# This script adds a uuid entry to each element in an xml file
# Author: Ben Wolfe - Apr 2009
# http://blog.eflow.org
date
path=`pwd`
# get a flat list of all files under the current directory
f=`ls -RlQ | awk -v PATH=$path '{ \
if ($1 ~ /.*:$/) \
path = substr($1,3,length($1)-4); \
else \
if ($0 ~ /^-/) \
printf("%s%s/%s\n", PATH, path, \
substr($0, match($0,"\".*\"")+1, RLENGTH-2) \
) \
}'`
# create a sed command that adds a generated uuid attribute to all elements that don't contain uuid:
# @UUIDGEN@ needs to get replaced later with the generated uuid
command="/uuid/!{s/\/>/ uuid=\"@UUIDGEN@\"\/>/}"
# ignore rows with <users, <patient, <user_role, <location_tag, <concept_word, <user_privilege, etc because they don't have a uuid column
for word in "<patient " "<user_role " "<location_tag " "<concept_word " "<concept_numeric " "<concept_derived " "<complex_obs " "<role_role " "<role_privilege " "<scheduled_task_config " "<scheduler_task_config_property " "<drug_order " "<concept_complex " "<concept_name_tag_map " "<cohort " "<cohort_member " "<location_tag_map " "<users "
do
command="/$word/!{$command}"
done
for i in $f
do
#ignore the .svn folder contents
if ! echo $i | grep .svn > /dev/null;
then
# get the file's extension
ext=`echo "$i" | awk -F . '{print $NF}'`
# only execute on xml files
case $ext in
xml)
echo acting on $i
# blank out the IFS variable so that the "while read line" doesn't delete leading whitespace
OIFS=$IFS
IFS=
while read line
do
uuid=`uuidgen`
newcommand=`echo $command | sed -e "s/@UUIDGEN@/$uuid/"`
# run the command on the current line
echo "$line" | sed -e "$newcommand" >> $i.new
done < $i
# reset the IFS variable to what it was before
IFS=$OIFS
mv -f "$i.new" "$i"
;;
*)
echo invalid extension: $ext
;;
esac
fi
done
echo "done"
date