Wednesday, 14 March 2012

bash script to extract schema from database object list file (for Sybase)

#!/bin/bash

if [ ! -n "$ROOTDIR" ]
then
export ROOTDIR=`dirname $_|sed 's/\/bin//'`
if [ $ROOTDIR = "." ] ; then  export ROOTDIR=`pwd|sed 's/\/bin//'` ; fi
if [ $ROOTDIR = "bin" ] ; then  export ROOTDIR="." ; fi
fi

#echo functions ROOTDIR=$ROOTDIR

if [ ! -n "$SCRIPT_NAME" ]
then
export SCRIPT_NAME=`basename $0`
fi
if [ ! -n "$LOG" ]
then
  export LOG=$ROOTDIR/logs/$SCRIPT_NAME.`date +"%Y%m%d"`
  touch $LOG
fi

logex()
{
echo `date +"%m/%d %H:%M_%S:"` $* | tee -a $LOG
}

usage()
{
cat << EOF
usage: $0 [options]

This script extract database object list from given file.
Using ddlgen and object list file to generate database schema.
Generation order and/or object dependance can be easily controlled this way.
OPTIONS:
   -h      Show this message
   -S      Database Server Name from interface file
   -U      Database User
   -P      Database User Password
   -O      Object List File Name without .txt extension (Database Name/Object Type/Object Name)
   -T      DB ticket number, e.g. 1234. TRADINGDB- will be added to the number

For example:
extract_schema.sh -Slocal_sybase -Usa  -OObjectList -T1234
or
extract_schema.sh -Slocal_sybase -Usa -Ppassword -OObjectList -T1678
EOF
}

#trap 'rm /tmp/*.$$ 1>/dev/null 2>&1' EXIT INT QUIT KILL TERM

logex start
# get variable values from arguments
while getopts ":h:S:U:P:O:T:" OPTION
do
    case $OPTION in
    h)
        usage
        exit 1
        ;;
    S) 
        DatabaseServer=$OPTARG
        ;;
        U)
        DatabaseUser=$OPTARG
        ;;
    P)
        DatabasePassword=$OPTARG
        ;;
    O)
        ObjectList=$OPTARG
        ;;
    T)
        Ticket=$OPTARG
        ;;
    ?)
        usage
        exit 1
        ;;
    esac
done

#logex ObjectList = $ObjectList
#logex DatabaseServer = $DatabaseServer
#logex DatabaseUser = $DatabaseUser
#logex DatabasePassword = $DatabasePassword
#logex Ticket = $Ticket

ObjectListFile="$ROOTDIR/schemes/$ObjectList"
logex file to process: $ObjectListFile.txt

rm -f $ObjectListFile.sql
rm -f $ObjectListFile.txt.tmp

# remove lines begin with #
grep -v ^\# $ObjectListFile.txt > $ObjectListFile.txt.tmp
LineNumber=0
FormattedLineNumber="000"
FileName=""
Seperator="_"
SingFilePath=""
SingleFilePath="$ROOTDIR/schemes/TRADINGDB-$Ticket$Seperator$ObjectList.sql"
logex file to generate:  $SingleFilePath

DmlOption="with dml_logging = full"
SegmentOption=" on 'default'"

while read DatabaseName ObjectType ObjectName
do
  #echo "$DatabaseName $ObjectType $ObjectName"
  #ddlgen -U$DatabaseUser -P$DatabasePassword -S$DatabaseServer -D$DatabaseName -T$ObjectType -N$ObjectName
  ddlgen -U$DatabaseUser -P$DatabasePassword -S$DatabaseServer -D$DatabaseName -T$ObjectType -N$ObjectName >> $SingleFilePath
  LineNumber=$[$LineNumber + 1]
  FormattedLineNumber=`printf '%03d' $LineNumber`
  FileName="$ROOTDIR/schemes/$Ticket$Seperator$FormattedLineNumber$Seperator$DatabaseName$Seperator$ObjectType$Seperator$ObjectName.sql"

  #logex extracting line $LineNumber for object $ObjectName into $FileName
  logex processing line $LineNumber object $ObjectName
  ddlgen -U$DatabaseUser -P$DatabasePassword -S$DatabaseServer -D$DatabaseName -T$ObjectType -N$ObjectName > $FileName
  sed -i "/^${DmlOption}/d" $FileName #1>/dev/null
  sed -i "/^${SegmentOption}/d" $FileName #1>/dev/null
  if [ $? -ne 0 ]
     then log error process line $LineNumber
          break
  fi
done < $ObjectListFile.txt.tmp

sed -i "/^${DmlOption}/d" $SingleFilePath #1>/dev/null
sed -i "/^${SegmentOption}/d" $SingleFilePath #1>/dev/null
rm $ObjectListFile.txt.tmp

logex all done

No comments:

Post a Comment