Parcourir la source

[3802] Addressed review comments

Primarily, SQL text for lease-dump now comes from a file and the use of
PIPESTATUS has been eliminated:

src/bin/admin/admin-utils.sh

    mysql_execute() - altered the function to not catch the mysql output in
    in an environment variable.  This removes the risk of large result sets
    crashing the shell and allows callers more flexibility.

    mysql_version() - now returns exit value of its call to mysql_execute.

    mysql_version_printf() - deleted this function.

src/bin/admin/kea-admin.in

    Removed the use of $_RESULT env variable formerly set by mysql_execute

    Replaced use of mysql_version_printf with appropriately structured
    calls to mysql_version

    Check_file_overwrite () - utility function for prompting the user that
    a given file will be overwritten

    get_dump_qry()  - new function which fetches the lease dump  SQL text
    specific to a given backend, version, and protocol from a file

    mysql_dump()
        - Updated to use get_dump_qry() to fetch the needed SQL
        - Restructured to use a temp file rather than pipe to eliminate
        use of PIPESTATUS

    pgsql_dump()
        Updated to get_dump_qry() to fetch the needed SQL
        Removed use of PIPESTATUS (didn't need it anyway)

src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh.in
    Updated calls to mysql_version

Added the following files which contain lease-dump SQL text:
    src/bin/admin/scripts/mysql/lease_dump_1.0.sh
    src/bin/admin/scripts/mysql/lease_dump_2.0.sh
    src/bin/admin/scripts/pgsql/lease_dump_1.0.sh
Thomas Markwalder il y a 10 ans
Parent
commit
19221f4a4f

+ 9 - 7
src/bin/admin/admin-utils.sh

@@ -22,24 +22,26 @@
 #     more convenient to use if the script didn't parse db_user db_password
 #     more convenient to use if the script didn't parse db_user db_password
 #     and db_name.
 #     and db_name.
 #
 #
+# It saves mysql command exit status both to the env variable _ADMIN_STATUS
+# as well as returning it as $? to the caller.
+
 mysql_execute() {
 mysql_execute() {
     if [ $# -gt 1 ]; then
     if [ $# -gt 1 ]; then
         QUERY="$1"
         QUERY="$1"
         shift
         shift
-        _RESULT=$(mysql -N -B  $* -e "${QUERY}")
+        mysql -N -B  $* -e "${QUERY}"
         retcode=$?
         retcode=$?
     else
     else
-        _RESULT=$(mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name)
-        retcode=$?
+        mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name
+        retcode="$?"
     fi
     fi
+
     return $retcode
     return $retcode
 }
 }
 
 
+
 mysql_version() {
 mysql_version() {
     mysql_execute "SELECT CONCAT(version,\".\",minor) FROM schema_version" "$@"
     mysql_execute "SELECT CONCAT(version,\".\",minor) FROM schema_version" "$@"
+    return $?
 }
 }
 
 
-mysql_version_print() {
-    mysql_version "$@"
-    printf "%s" $_RESULT
-}

+ 116 - 65
src/bin/admin/kea-admin.in

@@ -1,6 +1,6 @@
 #!/bin/sh
 #!/bin/sh
 
 
-# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
 #
 #
 # Permission to use, copy, modify, and/or distribute this software for any
 # Permission to use, copy, modify, and/or distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
 # purpose with or without fee is hereby granted, provided that the above
@@ -36,6 +36,7 @@ db_name="keatest"
 # lease dump parameters
 # lease dump parameters
 dump_type=0
 dump_type=0
 dump_file=""
 dump_file=""
+dump_qry=""
 
 
 # Include utilities. Use installed version if available and
 # Include utilities. Use installed version if available and
 # use build version if it isn't.
 # use build version if it isn't.
@@ -143,8 +144,15 @@ mysql_init() {
     # We should not hide them as they may give hints to user what is wrong with
     # We should not hide them as they may give hints to user what is wrong with
     # his setup.
     # his setup.
     #
     #
-    mysql_execute "SHOW TABLES;"
-    COUNT=`echo $_RESULT | wc -w`
+    RESULT=`mysql_execute "SHOW TABLES;"`
+    ERRCODE=$?
+    if [ $ERRCODE -ne  0 ]
+    then
+        log_error "mysql_init table query failed, mysql status = $ERRCODE"
+        exit 1
+    fi
+
+    COUNT=`echo $RESULT | wc -w`
     if [ $COUNT -gt 0 ]; then
     if [ $COUNT -gt 0 ]; then
         # Let't start with a new line. mysql could have printed something out.
         # Let't start with a new line. mysql could have printed something out.
         printf "\n"
         printf "\n"
@@ -160,7 +168,7 @@ mysql_init() {
 
 
     if [ "$ERRCODE" -eq 0 ]; then
     if [ "$ERRCODE" -eq 0 ]; then
         printf "Lease DB version reported after initialization: "
         printf "Lease DB version reported after initialization: "
-        mysql_version_print
+        mysql_version
         printf "\n"
         printf "\n"
     fi
     fi
 
 
@@ -200,7 +208,7 @@ memfile_upgrade() {
 mysql_upgrade() {
 mysql_upgrade() {
 
 
     printf "Lease DB version reported before upgrade: "
     printf "Lease DB version reported before upgrade: "
-    mysql_version_print
+    mysql_version
     printf "\n"
     printf "\n"
 
 
     # Check if the scripts directory exists at all.
     # Check if the scripts directory exists at all.
@@ -223,7 +231,7 @@ mysql_upgrade() {
     done
     done
 
 
     printf "Lease DB version reported after upgrade: "
     printf "Lease DB version reported after upgrade: "
-    mysql_version_print
+    mysql_version
     printf "\n"
     printf "\n"
 }
 }
 
 
@@ -231,34 +239,55 @@ pgsql_upgrade() {
     log_error "NOT IMPLEMENTED"
     log_error "NOT IMPLEMENTED"
 }
 }
 
 
-### Functions used for dump
-memfile_dump() {
-    log_error "lease-dump is not supported for memfile"
-    exit 1
+# Utility function which tests if the given file exists and
+# if so notifies the user and provides them the opportunity
+# to abort the current command.
+check_file_overwrite () {
+    local file=$1
+    if [ -e ${file} ]
+    then
+        echo "Output file, $file, exists and will be overwritten."
+        echo "Do you wish to continue? (y/n)"
+        read ans
+        if [ ${ans} != "y" ]
+        then
+            echo "$command aborted by user."
+            exit 1
+        fi
+    fi
 }
 }
 
 
 ### Functions used for dump
 ### Functions used for dump
-mysql_dump() {
+
+# Sets the global variable, dump_qry, to the schema-version specific
+# SQL text needed to dump the lease data for the current backend
+# and protocol
+get_dump_query() {
+    local version=$1
+
+    dump_qry=""
+    dump_sql_file="$scripts_dir/${backend}/lease_dump_$version.sh"
+    if [ ! -e $dump_sql_file ]
+    then
+        log_error "lease-dump: cannot access dump_sql_file: $dump_sql_file"
+        exit 1;
+    fi
+
+    # source in the dump file which defines the sql text we'll need
+    . $dump_sql_file
+    if [ $? -ne 0 ]
+    then
+        log_error "lease-dump: error sourcing dump_sql_file: $dump_sql_file"
+        exit 1
+    fi
 
 
     # Construct the SQL text to dump the leases based on protocol type
     # Construct the SQL text to dump the leases based on protocol type
     case ${dump_type} in
     case ${dump_type} in
         4)
         4)
-            qry="\
-SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
-'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
-SELECT INET_NTOA(address), IFNULL(HEX(hwaddr), ''), IFNULL(HEX(client_id), ''),\
-valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname from lease4"
+            dump_qry="$lease4_dump_sql";
             ;;
             ;;
         6)
         6)
-            qry="\
-SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
-'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
-'fqdn_rev', 'hostname', 'hwaddr', 'hwtype', 'hwaddr_source';\
-SELECT a.address, IFNULL(HEX(a.duid), ''), a.valid_lifetime,\
-a.expire, a.subnet_id, a.pref_lifetime, IFNULL(b.name, ''), a.iaid, a.prefix_len,\
-a.fqdn_fwd, a.fqdn_rev, hostname, IFNULL(HEX(hwaddr), ''), IFNULL(hwtype, ''),\
-IFNULL(hwaddr_source, '') \
-FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
+            dump_qry="$lease6_dump_sql";
             ;;
             ;;
         *)
         *)
             log_error "you must specify -4 or -6 for lease-dump"
             log_error "you must specify -4 or -6 for lease-dump"
@@ -267,6 +296,33 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
             ;;
             ;;
     esac
     esac
 
 
+    if [ "$dump_qry" = "" ]
+    then
+        log_error "lease-dump: dump query appears to be undefined"
+        exit 1
+    fi
+}
+
+memfile_dump() {
+    log_error "lease-dump is not supported for memfile"
+    exit 1
+}
+
+mysql_dump() {
+
+    # get the correct dump query
+    version=`mysql_version`
+    retcode=$?
+    if [ $retcode -ne 0 ]
+    then
+        log_error "lease-dump: mysql_version failed, exit code $retcode"
+        exit 1;
+    fi
+
+    # Fetch the correct SQL text. Note this function will exit
+    # if it fails.
+    get_dump_query $version
+
     # Make sure they specified a file
     # Make sure they specified a file
     if [ "$dump_file" = "" ]; then
     if [ "$dump_file" = "" ]; then
         log_error "you must specify an output file for lease-dump"
         log_error "you must specify an output file for lease-dump"
@@ -275,19 +331,34 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
 
 
     fi
     fi
 
 
-    # Call mysql directly so we can redirect its output to tr to translate tabs to commas
-    # then to the output file. "mysql_execute" function stores the query result in
-    # environment variable and for large DBs this might be an issue.
-    # We also do not use MySQL's output to file as that requires linux superuser privileges to
-    # execute the select.
-    mysql -N -B --user=$db_user --password=$db_password -e "${qry}" $db_name | tr '\t' ',' >$dump_file
+    # If output file exists, notify user, allow them a chance to bail
+    check_file_overwrite $dump_file
+
+    # Check the temp file too
+    tmp_file="$dump_file.tmp"
+    check_file_overwrite $tmp_file
+
+    # Run the sql to output tab-delimited lease data to a temp file.
+    # By using a temp file we can check for MySQL errors before using
+    # 'tr' to translate tabs to commas.  We do not use MySQL's output
+    # to file as that requires linux superuser privileges to execute
+    # the select.
+    mysql_execute "${dump_qry}" > $tmp_file
+    retcode=$?
+    if [ $retcode -ne 0 ]; then
+        log_error "lease-dump: mysql_execute failed, exit code $retcode";
+        exit 1
+    fi
 
 
-    # Check for errors
-    if [ ${PIPESTATUS[0]} -ne 0 ]; then
-        log_error "lease-dump failed";
+    # Now translate tabs to commas.
+    cat $tmp_file | tr '\t' ',' >$dump_file
+    if [ $? -ne 0 ]; then
+        log_error "lease-dump: reformatting failed";
         exit 1
         exit 1
     fi
     fi
 
 
+    # delete the tmp file on success
+    rm $tmp_file
     echo lease$dump_type successfully dumped to $dump_file
     echo lease$dump_type successfully dumped to $dump_file
     exit 0
     exit 0
 }
 }
@@ -295,33 +366,9 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
 ### Functions used for dump
 ### Functions used for dump
 pgsql_dump() {
 pgsql_dump() {
 
 
-    # Construct the SQL text to dump the leases based on protocol type
-    case ${dump_type} in
-        4)
-            qry="\
-SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
-'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
-SELECT ('0.0.0.0'::inet + address) AS address,\
-encode(hwaddr,'hex'), encode(client_id,'hex'), valid_lifetime,\
-expire, subnet_id, fqdn_fwd::int AS fqdn_fwd, fqdn_rev::int AS fqdn_rev,\
-hostname FROM lease4"
-            ;;
-        6)
-            qry="\
-SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
-'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
-'fqdn_rev', 'hostname';\
-SELECT a.address, encode(a.duid,'hex'), a.valid_lifetime,\
-a.expire, a.subnet_id, a.pref_lifetime, b.name, a.iaid, a.prefix_len,\
-a.fqdn_fwd, a.fqdn_rev, hostname \
-FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
-            ;;
-        *)
-            log_error "you must specify -4 or -6 for lease-dump"
-            usage
-            exit 1
-            ;;
-    esac
+    # @todo use pgsql_version once implemented. See #3883
+    version='1.0'
+    get_dump_query $version
 
 
     # Make sure they specified a file
     # Make sure they specified a file
     if [ "$dump_file" = "" ]; then
     if [ "$dump_file" = "" ]; then
@@ -331,16 +378,20 @@ FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"
 
 
     fi
     fi
 
 
+    # If output file exists, notify user, allow them a chance to bail
+    check_file_overwrite $dump_file
+
     # psql does not accept password as a parameter but will look in the environment
     # psql does not accept password as a parameter but will look in the environment
     export PGPASSWORD=$db_password
     export PGPASSWORD=$db_password
 
 
     # Call psql and redirect output to the dump file. We don't use psql "to csv"
     # Call psql and redirect output to the dump file. We don't use psql "to csv"
     # as it can only be run as db superuser.
     # as it can only be run as db superuser.
-    echo "$header;$qry" | psql -t -q --user=$db_user --dbname=$db_name -w --no-align --field-separator=',' >$dump_file
+    echo "$dump_qry" | psql --set ON_ERROR_STOP=1 -t -q --user=$db_user --dbname=$db_name -w --no-align --field-separator=',' >$dump_file
+    retcode=$?
 
 
     # Check for errors.
     # Check for errors.
-    if [ ${PIPESTATUS[1]} -ne 0 ]; then
-        log_error "lease-dump failed";
+    if [ $retcode -ne 0 ]; then
+        log_error "lease-dump: psql call failed, exit code: $retcode";
         exit 1
         exit 1
     fi
     fi
 
 
@@ -479,7 +530,7 @@ case ${command} in
                 memfile_version
                 memfile_version
                 ;;
                 ;;
             mysql)
             mysql)
-                mysql_version_print
+                mysql_version
                 printf "\n"
                 printf "\n"
                 ;;
                 ;;
             pgsql)
             pgsql)

+ 2 - 2
src/bin/admin/scripts/mysql/Makefile.am

@@ -1,6 +1,6 @@
 SUBDIRS = .
 SUBDIRS = .
 
 
 sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/mysql
 sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/mysql
-sqlscripts_DATA = dhcpdb_create.mysql upgrade_1.0_to_2.0.sh
+sqlscripts_DATA = dhcpdb_create.mysql upgrade_1.0_to_2.0.sh lease_dump_1.0.sh lease_dump_2.0.sh
 
 
-EXTRA_DIST = dhcpdb_create.mysql upgrade_1.0_to_2.0.sh
+EXTRA_DIST = dhcpdb_create.mysql upgrade_1.0_to_2.0.sh lease_dump_1.0.sh lease_dump_2.0.sh

+ 37 - 0
src/bin/admin/scripts/mysql/lease_dump_1.0.sh

@@ -0,0 +1,37 @@
+#!/bin/sh
+# Copyright (C) 2015  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+# Specifies the MySQL sql for schema-version 1.0 required to produce 
+# lease output that includes a header row containing column names,
+# followed by the lease data. The text is used in a single call 
+# to the mysql command line tool.
+
+# SQL for DHCPv4 leases
+lease4_dump_sql="\
+SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
+'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
+SELECT INET_NTOA(address), IFNULL(HEX(hwaddr), ''), IFNULL(HEX(client_id), ''),\
+valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname from lease4"
+
+# SQL for DHCPv6 leases
+lease6_dump_sql="\
+SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
+'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
+'fqdn_rev', 'hostname';\
+SELECT a.address, IFNULL(HEX(a.duid), ''), a.valid_lifetime,\
+a.expire, a.subnet_id, a.pref_lifetime, IFNULL(b.name, ''), a.iaid, a.prefix_len,\
+a.fqdn_fwd, a.fqdn_rev, hostname \
+FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"

+ 38 - 0
src/bin/admin/scripts/mysql/lease_dump_2.0.sh

@@ -0,0 +1,38 @@
+#!/bin/sh
+# Copyright (C) 2015  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+# Specifies the MySQL sql for schema-version 2.0 required to produce 
+# lease output that includes a header row containing column names,
+# followed by the lease data. The text is used in a single call 
+# to the mysql command line tool.
+
+# SQL for DHCPv4 leases
+lease4_dump_sql="\
+SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
+'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
+SELECT INET_NTOA(address), IFNULL(HEX(hwaddr), ''), IFNULL(HEX(client_id), ''),\
+valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname from lease4"
+
+# SQL for DHCPv6 leases
+lease6_dump_sql="\
+SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
+'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
+'fqdn_rev', 'hostname', 'hwaddr', 'hwtype', 'hwaddr_source';\
+SELECT a.address, IFNULL(HEX(a.duid), ''), a.valid_lifetime,\
+a.expire, a.subnet_id, a.pref_lifetime, IFNULL(b.name, ''), a.iaid, a.prefix_len,\
+a.fqdn_fwd, a.fqdn_rev, hostname, IFNULL(HEX(hwaddr), ''), IFNULL(hwtype, ''),\
+IFNULL(hwaddr_source, '') \
+FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"

+ 1 - 2
src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh.in

@@ -8,8 +8,7 @@ else
     . @abs_top_builddir@/src/bin/admin/admin-utils.sh
     . @abs_top_builddir@/src/bin/admin/admin-utils.sh
 fi
 fi
 
 
-mysql_version "$@"
-VERSION=$_RESULT
+VERSION=`mysql_version "$@"`
 
 
 if [ "$VERSION" != "1.0" ]; then
 if [ "$VERSION" != "1.0" ]; then
     printf "This script upgrades 1.0 to 2.0. Reported version is $VERSION. Skipping upgrade.\n"
     printf "This script upgrades 1.0 to 2.0. Reported version is $VERSION. Skipping upgrade.\n"

+ 2 - 2
src/bin/admin/scripts/pgsql/Makefile.am

@@ -1,6 +1,6 @@
 SUBDIRS = .
 SUBDIRS = .
 
 
 sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/pgsql
 sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/pgsql
-sqlscripts_DATA = dhcpdb_create.pgsql
+sqlscripts_DATA = dhcpdb_create.pgsql lease_dump_1.0.sh
 
 
-EXTRA_DIST = dhcpdb_create.pgsql
+EXTRA_DIST = dhcpdb_create.pgsql lease_dump_1.0.sh

+ 39 - 0
src/bin/admin/scripts/pgsql/lease_dump_1.0.sh

@@ -0,0 +1,39 @@
+#!/bin/sh
+# Copyright (C) 2015  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+# Specifies the PostgreSQL sql for schema-version 1.0 required to produce 
+# lease output that includes a header row containing column names,
+# followed by the lease data. The text is used in a single call 
+# to the mysql command line tool.
+
+# SQL for DHCPv4 leases
+lease4_dump_sql="\
+SELECT 'address', 'hwaddr', 'client_id', 'valid_lifetime', 'expire',\
+'subnet_id', 'fqdn_fwd', 'fqdn_rev', 'hostname';\
+SELECT ('0.0.0.0'::inet + address) AS address,\
+encode(hwaddr,'hex'), encode(client_id,'hex'), valid_lifetime,\
+expire, subnet_id, fqdn_fwd::int AS fqdn_fwd, fqdn_rev::int AS fqdn_rev,\
+hostname FROM lease4"
+
+# SQL for DHCPv6 leases
+lease6_dump_sql="\
+SELECT 'address', 'duid', 'valid_lifetime', 'expire',\
+'subnet_id', 'pref_lifetime', 'lease_type', 'iaid', 'prefix_len', 'fqdn_fwd',\
+'fqdn_rev', 'hostname';\
+SELECT a.address, encode(a.duid,'hex'), a.valid_lifetime,\
+a.expire, a.subnet_id, a.pref_lifetime, b.name, a.iaid, a.prefix_len,\
+a.fqdn_fwd, a.fqdn_rev, hostname \
+FROM lease6 a left outer join lease6_types b on (a.lease_type = b.lease_type)"

+ 3 - 1
src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv

@@ -1,2 +1,4 @@
 address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname
 address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname
-0.0.0.10,3230,3330,40,0000-00-00 00:00:00,50,1,1,example.com
+0.0.0.10,3230,3330,40,0000-00-00 00:00:00,50,1,1,one.example.com
+0.0.0.11,,313233,40,0000-00-00 00:00:00,50,1,1,
+0.0.0.12,3232,,40,0000-00-00 00:00:00,50,1,1,three.example.com

+ 3 - 1
src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv

@@ -1,2 +1,4 @@
 address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,hwtype,hwaddr_source
 address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,hwtype,hwaddr_source
-10,3230,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,example.com,3830,90,100
+10,3230,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,one.example.com,3830,90,100
+11,,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,,3830,90,100
+12,3231,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,three.example.com,3830,90,100

+ 33 - 2
src/bin/admin/tests/mysql_tests.sh.in

@@ -185,8 +185,20 @@ mysql_lease4_dump_test() {
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     output_file="$test_dir/data/lease4_dump_test.output.csv"
     output_file="$test_dir/data/lease4_dump_test.output.csv"
+    tmp_file="$test_dir/data/lease4_dump_test.output.csv.tmp"
     ref_file="$test_dir/data/mysql.lease4_dump_test.reference.csv"
     ref_file="$test_dir/data/mysql.lease4_dump_test.reference.csv"
 
 
+    # wipe out any residuals from prior failed runs
+    if [ -e $output_file ]
+    then
+        rm $output_file
+    fi
+
+    if [ -e $tmp_file ]
+    then
+        rm $tmp_file
+    fi
+
     # Let's wipe the whole database
     # Let's wipe the whole database
     mysql_wipe
     mysql_wipe
 
 
@@ -196,7 +208,11 @@ mysql_lease4_dump_test() {
     assert_eq 0 $ERRCODE "could not create database,  status code %d"
     assert_eq 0 $ERRCODE "could not create database,  status code %d"
 
 
     # Insert the reference record
     # Insert the reference record
-    insert_sql="insert into lease4 values(10,20,30,40,0,50,1,1,\"example.com\");"
+    insert_sql="\
+insert into lease4 values(10,20,30,40,0,50,1,1,\"one.example.com\");\
+insert into lease4 values(11,NULL,123,40,0,50,1,1,\"\");\
+insert into lease4 values(12,22,NULL,40,0,50,1,1,\"three.example.com\");"
+
     mysql_execute "$insert_sql"
     mysql_execute "$insert_sql"
     ERRCODE=$?
     ERRCODE=$?
     assert_eq 0 $ERRCODE "insert into lease4 failed, status code %d"
     assert_eq 0 $ERRCODE "insert into lease4 failed, status code %d"
@@ -226,8 +242,20 @@ mysql_lease6_dump_test() {
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     output_file="$test_dir/data/lease6_dump_test.output.csv"
     output_file="$test_dir/data/lease6_dump_test.output.csv"
+    tmp_file="$test_dir/data/lease6_dump_test.output.csv.tmp"
     ref_file="$test_dir/data/mysql.lease6_dump_test.reference.csv"
     ref_file="$test_dir/data/mysql.lease6_dump_test.reference.csv"
 
 
+    # wipe out any residuals from prior failed runs
+    if [ -e $output_file ]
+    then
+        rm $output_file
+    fi
+
+    if [ -e $tmp_file ]
+    then
+        rm $tmp_file
+    fi
+
     # Let's wipe the whole database
     # Let's wipe the whole database
     mysql_wipe
     mysql_wipe
 
 
@@ -237,7 +265,10 @@ mysql_lease6_dump_test() {
     assert_eq 0 $ERRCODE "could not create database,  status code %d"
     assert_eq 0 $ERRCODE "could not create database,  status code %d"
 
 
     # Insert the reference record
     # Insert the reference record
-    insert_sql="insert into lease6 values(10,20,30,0,40,50,1,60,70,1,1,\"example.com\",80,90,100);"
+    insert_sql="\
+insert into lease6 values(10,20,30,0,40,50,1,60,70,1,1,\"one.example.com\",80,90,100);\
+insert into lease6 values(11,NULL,30,0,40,50,1,60,70,1,1,\"\",80,90,100);\
+insert into lease6 values(12,21,30,0,40,50,1,60,70,1,1,\"three.example.com\",80,90,100);"
 
 
     mysql_execute "$insert_sql"
     mysql_execute "$insert_sql"
     ERRCODE=$?
     ERRCODE=$?