src/bin/admin/tests/mysql_tests.sh
src/bin/admin/scripts/mysql/Makefile
src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh
+ src/bin/admin/scripts/mysql/upgrade_2.0_to_3.0.sh
src/bin/admin/scripts/pgsql/Makefile
src/hooks/Makefile
src/hooks/dhcp/Makefile
SUBDIRS = .
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 upgrade_2.0_to_3.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 upgrade_2.0_to_3.0.sh
# This line starts database upgrade to version 3.0.
# Upgrade extending MySQL schema with the ability to store hosts.
-SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
-SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
-SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-
-CREATE SCHEMA IF NOT EXISTS `kea_host_reservation` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
-USE `kea_host_reservation` ;
-
+CREATE TABLE IF NOT EXISTS hosts (
+ host_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
+ dhcp_identifier VARBINARY(128) NOT NULL,
+ dhcp_identifier_type TINYINT NOT NULL,
+ dhcp4_subnet_id INT UNSIGNED NULL,
+ dhcp6_subnet_id INT UNSIGNED NULL,
+ ipv4_address INT UNSIGNED NULL,
+ hostname VARCHAR(255) NULL,
+ dhcp4_client_classes VARCHAR(255) NULL,
+ dhcp6_client_classes VARCHAR(255) NULL,
+ PRIMARY KEY (host_id),
+ INDEX key_dhcp4_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC),
+ INDEX key_dhcp6_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp6_subnet_id ASC)
+) ENGINE=INNODB;
-- -----------------------------------------------------
--- Table `kea_host_reservation`.`hosts`
+-- Table `ipv6_reservations`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `kea_host_reservation`.`hosts` (
- `host_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
- `dhcp_identifier` VARBINARY(128) NOT NULL ,
- `dhcp_identifier_type` TINYINT NOT NULL ,
- `dhcp4_subnet_id` INT UNSIGNED NULL ,
- `dhcp6_subnet_id` INT UNSIGNED NULL ,
- `ipv4_address` INT UNSIGNED NULL ,
- `hostname` VARCHAR(255) NULL ,
- `dhcp4_cllient_classes` VARCHAR(255) NULL ,
- `dhcp6_client_classes` VARCHAR(255) NULL ,
- PRIMARY KEY (`host_id`) ,
- INDEX `key_dhcp4_identifier_subnet_id` (`dhcp_identifier` ASC, `dhcp_identifier_type` ASC) ,
- INDEX `key_dhcp6_identifier_subnet_id` (`dhcp_identifier` ASC, `dhcp_identifier_type` ASC, `dhcp6_subnet_id` ASC) )
-ENGINE = InnoDB;
-
+CREATE TABLE IF NOT EXISTS ipv6_reservations (
+ reservation_id INT NOT NULL AUTO_INCREMENT,
+ address VARCHAR(39) NOT NULL,
+ prefix_len TINYINT(3) UNSIGNED NOT NULL DEFAULT 128,
+ type TINYINT(4) UNSIGNED NOT NULL DEFAULT 0,
+ dhcp6_iaid INT UNSIGNED NULL,
+ host_id INT UNSIGNED NOT NULL,
+ PRIMARY KEY (reservation_id),
+ INDEX fk_ipv6_reservations_host_idx (host_id ASC),
+ CONSTRAINT fk_ipv6_reservations_Host FOREIGN KEY (host_id)
+ REFERENCES hosts (host_id)
+ ON DELETE NO ACTION ON UPDATE NO ACTION
+) ENGINE=INNODB;
-- -----------------------------------------------------
--- Table `kea_host_reservation`.`ipv6_reservations`
+-- Table `dhcp4_options`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `kea_host_reservation`.`ipv6_reservations` (
- `reservation_id` INT NOT NULL AUTO_INCREMENT ,
- `address` VARCHAR(39) NOT NULL ,
- `prefix_len` TINYINT(3) UNSIGNED NOT NULL DEFAULT 128 ,
- `type` TINYINT(4) UNSIGNED NOT NULL DEFAULT 0 ,
- `dhcp6_iaid` INT UNSIGNED NULL ,
- `host_id` INT UNSIGNED NOT NULL ,
- PRIMARY KEY (`reservation_id`) ,
- INDEX `fk_ipv6_reservations_host_idx` (`host_id` ASC) ,
- CONSTRAINT `fk_ipv6_reservations_Host`
- FOREIGN KEY (`host_id` )
- REFERENCES `kea_host_reservation`.`hosts` (`host_id` )
- ON DELETE NO ACTION
- ON UPDATE NO ACTION)
-ENGINE = InnoDB;
-
-
--- -----------------------------------------------------
--- Table `kea_host_reservation`.`dhcp4_options`
--- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `kea_host_reservation`.`dhcp4_options` (
- `option_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
- `code` TINYINT UNSIGNED NOT NULL ,
- `value` BLOB NULL ,
- `formatted_value` TEXT NULL ,
- `space` VARCHAR(128) NULL ,
- `persistent` TINYINT(1) NOT NULL DEFAULT 0 ,
- `dhcp_client_class` VARCHAR(128) NULL ,
- `dhcp4_subnet_id` INT NULL ,
- `host_id` INT UNSIGNED NULL ,
- PRIMARY KEY (`option_id`) ,
- UNIQUE INDEX `option_id_UNIQUE` (`option_id` ASC) ,
- INDEX `fk_options_host1_idx` (`host_id` ASC) ,
- CONSTRAINT `fk_options_host1`
- FOREIGN KEY (`host_id` )
- REFERENCES `kea_host_reservation`.`hosts` (`host_id` )
- ON DELETE NO ACTION
- ON UPDATE NO ACTION)
-ENGINE = InnoDB;
-
-
+CREATE TABLE IF NOT EXISTS dhcp4_options (
+ option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
+ code TINYINT UNSIGNED NOT NULL,
+ value BLOB NULL,
+ formatted_value TEXT NULL,
+ space VARCHAR(128) NULL,
+ persistent TINYINT(1) NOT NULL DEFAULT 0,
+ dhcp_client_class VARCHAR(128) NULL,
+ dhcp4_subnet_id INT NULL,
+ host_id INT UNSIGNED NULL,
+ PRIMARY KEY (option_id),
+ UNIQUE INDEX option_id_UNIQUE (option_id ASC),
+ INDEX fk_options_host1_idx (host_id ASC),
+ CONSTRAINT fk_options_host1 FOREIGN KEY (host_id)
+ REFERENCES hosts (host_id)
+ ON DELETE NO ACTION ON UPDATE NO ACTION
+) ENGINE=INNODB;
-- -----------------------------------------------------
--- Table `kea_host_reservation`.`dhcp6_options`
+-- Table `dhcp6_options`
-- -----------------------------------------------------
-CREATE TABLE IF NOT EXISTS `kea_host_reservation`.`dhcp6_options` (
- `option_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
- `code` INT UNSIGNED NOT NULL ,
- `value` BLOB NULL ,
- `formatted_value` TEXT NULL ,
- `space` VARCHAR(128) NULL ,
- `persistent` TINYINT(1) NOT NULL DEFAULT 0 ,
- `dhcp_client_class` VARCHAR(128) NULL ,
- `dhcp6_subnet_id` INT NULL ,
- `host_id` INT UNSIGNED NULL ,
- PRIMARY KEY (`option_id`) ,
- UNIQUE INDEX `option_id_UNIQUE` (`option_id` ASC) ,
- INDEX `fk_options_host1_idx` (`host_id` ASC) ,
- CONSTRAINT `fk_options_host10`
- FOREIGN KEY (`host_id` )
- REFERENCES `kea_host_reservation`.`hosts` (`host_id` )
- ON DELETE NO ACTION
- ON UPDATE NO ACTION)
-ENGINE = InnoDB;
-
-USE `kea_host_reservation` ;
-USE `kea_host_reservation`;
+CREATE TABLE IF NOT EXISTS dhcp6_options (
+ option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
+ code INT UNSIGNED NOT NULL,
+ value BLOB NULL,
+ formatted_value TEXT NULL,
+ space VARCHAR(128) NULL,
+ persistent TINYINT(1) NOT NULL DEFAULT 0,
+ dhcp_client_class VARCHAR(128) NULL,
+ dhcp6_subnet_id INT NULL,
+ host_id INT UNSIGNED NULL,
+ PRIMARY KEY (option_id),
+ UNIQUE INDEX option_id_UNIQUE (option_id ASC),
+ INDEX fk_options_host1_idx (host_id ASC),
+ CONSTRAINT fk_options_host10 FOREIGN KEY (host_id)
+ REFERENCES hosts (host_id)
+ ON DELETE NO ACTION ON UPDATE NO ACTION
+) ENGINE=INNODB;
DELIMITER $$
-USE `kea_host_reservation`$$
-
-
-CREATE TRIGGER `host_BDEL` BEFORE DELETE ON hosts FOR EACH ROW
+CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
BEGIN
-DELETE FROM `ipv6_reservations` WHERE `ipv6_reservations`.host_id = OLD.host_id;
+DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id;
END
$$
-
-
DELIMITER ;
-
-SET SQL_MODE=@OLD_SQL_MODE;
-SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
-SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-
-
+UPDATE schema_version
+SET version = '3', minor = '0';
# This line concludes database upgrade to version 3.0.
# Notes:
--- /dev/null
+#!/bin/sh
+
+# Include utilities. Use installed version if available and
+# use build version if it isn't.
+if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
+ . @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
+else
+ . @abs_top_builddir@/src/bin/admin/admin-utils.sh
+fi
+
+mysql_version "$@"
+VERSION=$_RESULT
+
+if [ "$VERSION" != "2.0" ]; then
+ printf "This script upgrades 2.0 to 3.0. Reported version is $VERSION. Skipping upgrade.\n"
+ exit 0
+fi
+
+mysql "$@" <<EOF
+CREATE TABLE IF NOT EXISTS hosts (
+host_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
+dhcp_identifier VARBINARY(128) NOT NULL ,
+dhcp_identifier_type TINYINT NOT NULL ,
+dhcp4_subnet_id INT UNSIGNED NULL ,
+dhcp6_subnet_id INT UNSIGNED NULL ,
+ipv4_address INT UNSIGNED NULL ,
+hostname VARCHAR(255) NULL ,
+dhcp4_client_classes VARCHAR(255) NULL ,
+dhcp6_client_classes VARCHAR(255) NULL ,
+PRIMARY KEY (host_id) ,
+INDEX key_dhcp4_identifier_subnet_id (dhcp_identifier ASC, dhcp_identifier_type ASC) ,
+INDEX key_dhcp6_identifier_subnet_id (dhcp_identifier ASC, dhcp_identifier_type ASC, dhcp6_subnet_id ASC) );
+
+CREATE TABLE IF NOT EXISTS ipv6_reservations (
+reservation_id INT NOT NULL AUTO_INCREMENT ,
+address VARCHAR(39) NOT NULL ,
+prefix_len TINYINT(3) UNSIGNED NOT NULL DEFAULT 128 ,
+type TINYINT(4) UNSIGNED NOT NULL DEFAULT 0 ,
+dhcp6_iaid INT UNSIGNED NULL ,
+host_id INT UNSIGNED NOT NULL ,
+PRIMARY KEY (reservation_id) ,
+INDEX fk_ipv6_reservations_host_idx (host_id ASC) ,
+CONSTRAINT fk_ipv6_reservations_Host
+FOREIGN KEY (host_id )
+REFERENCES hosts (host_id )
+ON DELETE NO ACTION
+ON UPDATE NO ACTION);
+
+CREATE TABLE IF NOT EXISTS dhcp4_options (
+option_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
+code TINYINT UNSIGNED NOT NULL ,
+value BLOB NULL ,
+formatted_value TEXT NULL ,
+space VARCHAR(128) NULL ,
+persistent TINYINT(1) NOT NULL DEFAULT 0 ,
+dhcp_client_class VARCHAR(128) NULL ,
+dhcp4_subnet_id INT NULL ,
+host_id INT UNSIGNED NULL ,
+PRIMARY KEY (option_id) ,
+UNIQUE INDEX option_id_UNIQUE (option_id ASC) ,
+INDEX fk_options_host1_idx (host_id ASC) ,
+CONSTRAINT fk_options_host1
+FOREIGN KEY (host_id )
+REFERENCES hosts (host_id )
+ON DELETE NO ACTION
+ON UPDATE NO ACTION);
+
+CREATE TABLE IF NOT EXISTS dhcp6_options (
+option_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
+code INT UNSIGNED NOT NULL ,
+value BLOB NULL ,
+formatted_value TEXT NULL ,
+space VARCHAR(128) NULL ,
+persistent TINYINT(1) NOT NULL DEFAULT 0 ,
+dhcp_client_class VARCHAR(128) NULL ,
+dhcp6_subnet_id INT NULL ,
+host_id INT UNSIGNED NULL ,
+PRIMARY KEY (option_id) ,
+UNIQUE INDEX option_id_UNIQUE (option_id ASC) ,
+INDEX fk_options_host1_idx (host_id ASC) ,
+CONSTRAINT fk_options_host10
+FOREIGN KEY (host_id )
+REFERENCES hosts (host_id )
+ON DELETE NO ACTION
+ON UPDATE NO ACTION);
+
+DELIMITER $$
+CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW
+-- Edit trigger body code below this line. Do not edit lines above this one
+BEGIN
+DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id;
+END
+$$
+DELIMITER ;
+
+UPDATE schema_version SET version="3", minor="0";
+EOF
+
+RESULT=$?
+
+exit $?
test_finish 0
}
-mysql_lease_upgrade_test() {
+mysql_host_reservation_init_test() {
+ test_start "mysql.host_reservation-init"
- # @todo: need to test whether non-empty database is updated correctly and the data survives the upgrade
+ # Let's wipe the whole database
+ mysql_wipe
+
+ # Ok, now let's initalize the database
+ ${keaadmin} lease-init mysql -u $db_user -p $db_pass -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
+ ERRCODE=$?
+
+ assert_eq 0 $ERRCODE "kea-admin lease-init mysql returned non-zero status code %d, expected %d"
+
+ # Ok, now let's check if the tables are indeed there.
+ # First table: schema_version. Should have 2 columns: version and minor.
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT version, minor FROM schema_version;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "schema_version table is missing or broken. (returned status code %d, expected %d)"
+
+ # Second table: hosts
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes FROM hosts;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "hosts table is missing or broken. (returned status code %d, expected %d)"
+
+ # Third table: ipv6_reservations
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT reservation_id, address, prefix_len, type, dhcp6_iaid, host_id FROM ipv6_reservations;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "ipv6_reservations table is missing or broken. (returned status code %d, expected %d)"
+
+ # Fourth table: dhcp4_options
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp4_subnet_id, host_id FROM dhcp4_options;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "dhcp4_options table is missing or broken. (returned status code %d, expected %d)"
+
+ # Fifth table: dhcp6_options
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id FROM dhcp6_options;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (returned status code %d, expected %d)"
+
+ # Let's wipe the whole database
+ mysql_wipe
- test_start "mysql.lease-upgrade"
+ test_finish 0
+}
+
+mysql_upgrade_test() {
+
+ test_start "mysql.host_reservation-upgrade"
# Let's wipe the whole database
mysql_wipe
assert_str_eq "1.0" ${version} "Expected kea-admin to return %s, returned value was %s"
- # Ok, we have a 1.0 database. Let's upgrade it to 2.0
+ # Ok, we have a 1.0 database. Let's upgrade it to 3.0
${keaadmin} lease-upgrade mysql -u $db_user -p $db_pass -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
ERRCODE=$?
# Let's check that the new tables are indeed there.
- # Third table: lease6
+ #table: lease6 (upgrade 1.0 -> 2.0)
mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
SELECT hwaddr, hwtype, hwaddr_source FROM lease6;
EOF
ERRCODE=$?
assert_eq 0 $ERRCODE "lease6 table not upgraded to 2.0 (returned status code %d, expected %d)"
- # Fifth table: lease_hwaddr_source
+ #table: lease_hwaddr_source (upgrade 1.0 -> 2.0)
mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
SELECT hwaddr_source, name FROM lease_hwaddr_source;
EOF
ERRCODE=$?
assert_eq 0 $ERRCODE "lease_hwaddr_source table is missing or broken. (returned status code %d, expected %d)"
- # Verify that it reports version 2.0.
+ #table: hosts (upgrade 2.0 -> 3.0)
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT host_id, dhcp_identifier, dhcp_identifier_type, dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, dhcp4_client_classes, dhcp6_client_classes FROM hosts;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "hosts table is missing or broken. (returned status code %d, expected %d)"
+
+ #table: ipv6_reservations (upgrade 2.0 -> 3.0)
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT reservation_id, address, prefix_len, type, dhcp6_iaid, host_id FROM ipv6_reservations;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "ipv6_reservations table is missing or broken. (returned status code %d, expected %d)"
+
+ #table: dhcp4_options (upgrade 2.0 -> 3.0)
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp4_subnet_id, host_id FROM dhcp4_options;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "dhcp4_options table is missing or broken. (returned status code %d, expected %d)"
+
+ #table: dhcp6_options (upgrade 2.0 -> 3.0)
+ mysql -u$db_user -p$db_pass $db_name >/dev/null 2>&1 <<EOF
+ SELECT option_id, code, value, formatted_value, space, persistent, dhcp_client_class, dhcp6_subnet_id, host_id FROM dhcp6_options;
+EOF
+ ERRCODE=$?
+ assert_eq 0 $ERRCODE "dhcp6_options table is missing or broken. (returned status code %d, expected %d)"
+
+
+ # Verify that it reports version 3.0.
version=$(${keaadmin} lease-version mysql -u $db_user -p $db_pass -n $db_name)
- assert_str_eq "2.0" ${version} "Expected kea-admin to return %s, returned value was %s"
+ assert_str_eq "3.0" ${version} "Expected kea-admin to return %s, returned value was %s"
# Let's wipe the whole database
mysql_wipe
}
mysql_lease_init_test
+mysql_host_reservation_init_test
mysql_lease_version_test
-mysql_lease_upgrade_test
+mysql_upgrade_test