Parcourir la source

[master] Fixes MySQL InnoDB engine issue, MySQL version bump to 4.1

    Merges branch 'trac4237'
Thomas Markwalder il y a 9 ans
Parent
commit
f0fb9f35a3

+ 3 - 0
AUTHORS

@@ -87,6 +87,9 @@ We have received the following contributions:
  - Jinmei Tatuya
    2015-10: Pkt4o6 class improvements
 
+ - Sebastien Couture, Ubity Inc
+   2015-12: Fixes to MySQL schema creation
+
 Kea uses log4cplus (http://sourceforge.net/projects/log4cplus/) for logging,
 Boost (http://www.boost.org/) library for almost everything, and can use Botan
 (http://botan.randombit.net/) or OpenSSL (https://www.openssl.org/) for

+ 1 - 0
configure.ac

@@ -1417,6 +1417,7 @@ AC_CONFIG_FILES([compatcheck/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/mysql/upgrade_3.0_to_4.0.sh
+                 src/bin/admin/scripts/mysql/upgrade_4.0_to_4.1.sh
                  src/bin/admin/scripts/pgsql/Makefile
                  src/bin/admin/scripts/pgsql/upgrade_1.0_to_2.0.sh
                  src/hooks/Makefile

+ 1 - 0
src/bin/admin/scripts/mysql/Makefile.am

@@ -5,5 +5,6 @@ sqlscripts_DATA = dhcpdb_create.mysql
 sqlscripts_DATA += upgrade_1.0_to_2.0.sh
 sqlscripts_DATA += upgrade_2.0_to_3.0.sh
 sqlscripts_DATA += upgrade_3.0_to_4.0.sh
+sqlscripts_DATA += upgrade_4.0_to_4.1.sh
 
 EXTRA_DIST = ${sqlscripts_DATA}

+ 20 - 6
src/bin/admin/scripts/mysql/dhcpdb_create.mysql

@@ -83,7 +83,8 @@ CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid);
 CREATE TABLE lease6_types (
     lease_type TINYINT PRIMARY KEY NOT NULL,    # Lease type code.
     name VARCHAR(5)                             # Name of the lease type
-    );
+    ) ENGINE = INNODB;
+
 START TRANSACTION;
 INSERT INTO lease6_types VALUES (0, "IA_NA");   # Non-temporary v6 addresses
 INSERT INTO lease6_types VALUES (1, "IA_TA");   # Temporary v6 addresses
@@ -102,7 +103,7 @@ COMMIT;
 CREATE TABLE schema_version (
     version INT PRIMARY KEY NOT NULL,       # Major version number
     minor INT                               # Minor version number
-    );
+    ) ENGINE = INNODB;
 START TRANSACTION;
 INSERT INTO schema_version VALUES (1, 0);
 COMMIT;
@@ -127,7 +128,7 @@ ALTER TABLE lease6
 CREATE TABLE lease_hwaddr_source (
     hwaddr_source INT PRIMARY KEY NOT NULL,
     name VARCHAR(40)
-);
+) ENGINE = INNODB;
 
 # Hardware address obtained from raw sockets
 INSERT INTO lease_hwaddr_source VALUES (1, "HWADDR_SOURCE_RAW");
@@ -332,9 +333,10 @@ SELECT
     l.fqdn_rev,
     l.hostname,
     s.name
-from
+FROM
     lease4 l
-    LEFT OUTER JOIN lease_state s on (l.state = s.state);
+    LEFT OUTER JOIN lease_state s on (l.state = s.state)
+ORDER BY l.address;
 END $$
 DELIMITER ;
 
@@ -372,15 +374,27 @@ SELECT
 FROM lease6 l
     left outer join lease6_types t on (l.lease_type = t.lease_type)
     left outer join lease_state s on (l.state = s.state)
-    left outer join lease_hwaddr_source h on (l.hwaddr_source = h.hwaddr_source);
+    left outer join lease_hwaddr_source h on (l.hwaddr_source = h.hwaddr_source)
+ORDER BY l.address;
 END $$
 DELIMITER ;
 
 # Update the schema version number
 UPDATE schema_version
 SET version = '4', minor = '0';
+
 # This line concludes database upgrade to version 4.0.
 
+# In the event hardware address cannot be determined, we need to satisfy
+# foreign key constraint between lease6 and lease_hardware_source
+INSERT INTO lease_hwaddr_source VALUES (0, "HWADDR_SOURCE_UNKNOWN");
+
+# Update the schema version number
+UPDATE schema_version
+SET version = '4', minor = '1';
+
+# This line concludes database upgrade to version 4.1.
+
 # Notes:
 #
 # Indexes

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

@@ -24,7 +24,7 @@ ALTER TABLE lease6
 CREATE TABLE lease_hwaddr_source (
     hwaddr_source INT PRIMARY KEY NOT NULL,
     name VARCHAR(40)
-);
+) ENGINE = INNODB;
 
 -- See src/lib/dhcp/dhcp/pkt.h for detailed explanation
 INSERT INTO lease_hwaddr_source VALUES (1, "HWADDR_SOURCE_RAW");

+ 8 - 4
src/bin/admin/scripts/mysql/upgrade_2.0_to_3.0.sh.in

@@ -28,7 +28,8 @@ 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) );
+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 ,
@@ -43,7 +44,8 @@ CONSTRAINT fk_ipv6_reservations_Host
 FOREIGN KEY (host_id )
 REFERENCES hosts (host_id )
 ON DELETE NO ACTION
-ON UPDATE NO ACTION);
+ON UPDATE NO ACTION
+) ENGINE = INNODB;
 
 CREATE TABLE IF NOT EXISTS dhcp4_options (
 option_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
@@ -62,7 +64,8 @@ CONSTRAINT fk_options_host1
 FOREIGN KEY (host_id )
 REFERENCES hosts (host_id )
 ON DELETE NO ACTION
-ON UPDATE NO ACTION);
+ON UPDATE NO ACTION
+) ENGINE = INNODB;
 
 CREATE TABLE IF NOT EXISTS dhcp6_options (
 option_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
@@ -81,7 +84,8 @@ CONSTRAINT fk_options_host10
 FOREIGN KEY (host_id )
 REFERENCES hosts (host_id )
 ON DELETE NO ACTION
-ON UPDATE NO ACTION);
+ON UPDATE NO ACTION
+) ENGINE = INNODB;
 
 DELIMITER $$
 CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW

+ 90 - 0
src/bin/admin/scripts/mysql/upgrade_4.0_to_4.1.sh.in

@@ -0,0 +1,90 @@
+#!/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
+
+VERSION=`mysql_version "$@"`
+
+if [ "$VERSION" != "4.0" ]; then
+    printf "This script upgrades 4.0 to 4.1. Reported version is $VERSION. Skipping upgrade.\n"
+    exit 0
+fi
+
+mysql "$@" <<EOF
+
+# In the event hardware address cannot be determined, we need to satisfy
+# foreign key constraint between lease6 and lease_hardware_source
+INSERT INTO lease_hwaddr_source VALUES (0, "HWADDR_SOURCE_UNKNOWN");
+
+#
+# Add order by lease address to lease4DumpData 
+#
+DROP PROCEDURE IF EXISTS lease4DumpData;
+DELIMITER $$
+CREATE PROCEDURE lease4DumpData()
+BEGIN
+SELECT
+    INET_NTOA(l.address),
+    IFNULL(HEX(l.hwaddr), ''),
+    IFNULL(HEX(l.client_id), ''),
+    l.valid_lifetime,
+    l.expire,
+    l.subnet_id,
+    l.fqdn_fwd,
+    l.fqdn_rev,
+    l.hostname,
+    s.name
+FROM
+    lease4 l
+    LEFT OUTER JOIN lease_state s on (l.state = s.state)
+ORDER BY l.address;
+END $$
+DELIMITER ;
+
+#
+# Add order by lease address to lease6DumpData 
+#
+DROP PROCEDURE IF EXISTS lease6DumpData;
+DELIMITER $$
+CREATE PROCEDURE lease6DumpData()
+BEGIN
+SELECT
+    l.address,
+    IFNULL(HEX(l.duid), ''),
+    l.valid_lifetime,
+    l.expire,
+    l.subnet_id,
+    l.pref_lifetime,
+    IFNULL(t.name, ''),
+    l.iaid,
+    l.prefix_len,
+    l.fqdn_fwd,
+    l.fqdn_rev,
+    l.hostname,
+    IFNULL(HEX(l.hwaddr), ''),
+    IFNULL(l.hwtype, ''),
+    IFNULL(h.name, ''),
+    IFNULL(s.name, '')
+FROM lease6 l
+    left outer join lease6_types t on (l.lease_type = t.lease_type)
+    left outer join lease_state s on (l.state = s.state)
+    left outer join lease_hwaddr_source h on (l.hwaddr_source = h.hwaddr_source)
+ORDER BY l.address;
+END $$
+DELIMITER ;
+
+# Update the schema version number
+UPDATE schema_version
+SET version = '4', minor = '1';
+# This line concludes database upgrade to version 4.1.
+
+EOF
+
+RESULT=$?
+
+exit $?

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

@@ -1,4 +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,state
+10,3230,30,2015-04-04 01:15:30,40,50,IA_TA,60,70,1,1,one.example.com,3830,90,HWADDR_SOURCE_REMOTE_ID,default
 11,,30,2015-05-05 02:30:45,40,50,IA_TA,60,70,1,1,,3830,90,HWADDR_SOURCE_RAW,declined
 12,3231,30,2015-06-06 11:01:07,40,50,IA_TA,60,70,1,1,three.example.com,3830,90,HWADDR_SOURCE_DUID,expired-reclaimed
-10,3230,30,2015-04-04 01:15:30,40,50,IA_TA,60,70,1,1,one.example.com,3830,90,HWADDR_SOURCE_REMOTE_ID,default

+ 2 - 2
src/bin/admin/tests/dhcpdb_create_1.0.mysql

@@ -75,7 +75,7 @@ CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid);
 CREATE TABLE lease6_types (
     lease_type TINYINT PRIMARY KEY NOT NULL,    # Lease type code.
     name VARCHAR(5)                             # Name of the lease type
-    );
+    ) ENGINE = INNODB;
 START TRANSACTION;
 INSERT INTO lease6_types VALUES (0, "IA_NA");   # Non-temporary v6 addresses
 INSERT INTO lease6_types VALUES (1, "IA_TA");   # Temporary v6 addresses
@@ -94,7 +94,7 @@ COMMIT;
 CREATE TABLE schema_version (
     version INT PRIMARY KEY NOT NULL,       # Major version number
     minor INT                               # Minor version number
-    );
+    ) ENGINE = INNODB;
 START TRANSACTION;
 INSERT INTO schema_version VALUES (1, 0);
 COMMIT;

+ 28 - 10
src/bin/admin/tests/mysql_tests.sh.in

@@ -30,11 +30,6 @@ keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
 mysql_wipe() {
     printf "Wiping whole database %s\n" $db_name
 
-    # ipv6_reservations table must be deleted first, as it has contraints that
-    # are dependent on hosts. Therefore hosts table cannot be deleted before
-    # ipv6_reservations.
-    mysql_execute "DROP TABLE IF EXISTS ipv6_reservations;"
-
     # First we build the list of drop table commands
     # We don't bother with "cascade" because as of MySQL
     # 5.1 it is only there to ease porting, it doesn't
@@ -204,7 +199,7 @@ mysql_upgrade_test() {
 
     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 4.0
+    # Ok, we have a 1.0 database. Let's upgrade it to 4.1
     ${keaadmin} lease-upgrade mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
     ERRCODE=$?
 
@@ -254,7 +249,6 @@ 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.
     #table: lease_state table added (upgrade 3.0 -> 4.0)
     mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 <<EOF
     SELECT state,name from lease_state;
@@ -283,10 +277,34 @@ EOF
     ERRCODE=$?
     assert_eq 0 $ERRCODE "lease dump stored procedures are missing or broken. (returned status code %d, expected %d)"
 
-    # Verify that it reports version 4.0.
-    version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name)
+    #lease_hardware_source should have row for source = 0 (upgrade 4.0 -> 4.1)
+    qry="select count(hwaddr_source) from lease_hwaddr_source where hwaddr_source = 0 and name='HWADDR_SOURCE_UNKNOWN';"
+    count=`mysql_execute "${qry}"`
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "select from lease_hwaddr_source failed. (returned status code %d, expected %d)"
+    assert_eq 1 "$count" "lease_hwaddr_source does not contain entry for HWADDR_SOURCE_UKNOWN. (record count %d, expected %d)"
+
+    # table: stored procedures for lease data dumps were modified (upgrade 4.0 -> 4.1)
+    # verify  lease4DumpData has order by lease address
+    qry="show create procedure lease4DumpData"
+    text=`mysql_execute "${qry}"`
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "procedure text fetch for lease4DumpData failed. (returned status code %d, expected %d)"
+    count=`echo $text | grep -ic "order by l\.address"`
+    assert_eq 1 $count "lease4DumpData doesn't have order by clause. (returned count %d, expected %d)"
+   
+    # verify  lease6DumpData has order by lease address
+    qry="show create procedure lease6DumpData"
+    text=`mysql_execute "${qry}"`
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "procedure text fetch for lease6DumpData failed. (returned status code %d, expected %d)"
+    count=`echo $text | grep -ic "order by l\.address"`
+    assert_eq 1 $count "lease6DumpData doesn't have order by clause. (returned count %d, expected %d)"
+
+    # Verify upgraded schemd reports version 4.1.
+    version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts)
+    assert_str_eq "4.1" ${version} "Expected kea-admin to return %s, returned value was %s"
 
-    assert_str_eq "4.0" ${version} "Expected kea-admin to return %s, returned value was %s"
 
     # Let's wipe the whole database
     mysql_wipe

+ 3 - 1
src/lib/dhcpsrv/tests/mysql_schema.cc

@@ -114,7 +114,9 @@ void createMySQLSchema() {
     // Execute creation statements.
     for (int i = 0; create_statement[i] != NULL; ++i) {
         ASSERT_EQ(0, mysql_query(mysql, create_statement[i]))
-            << "Failed on statement " << i << ": " << create_statement[i];
+            << "Failed on statement " << i << ": " << create_statement[i]
+            << " error: " << mysql_error(mysql) << " (error code "
+            << mysql_errno(mysql) << ")";
     }
 }
 

+ 4 - 6
src/lib/dhcpsrv/tests/schema_mysql_copy.h

@@ -24,15 +24,13 @@ namespace {
 // Deletion of existing tables.
 
 const char* destroy_statement[] = {
+    // Turning off referential integrity checks ensures tables get dropped
+    "SET SESSION FOREIGN_KEY_CHECKS = 0",
     "DROP TABLE lease4",
     "DROP TABLE lease6",
     "DROP TABLE lease6_types",
     "DROP TABLE lease_hwaddr_source",
     "DROP TABLE schema_version",
-
-    // We need to drop ipv6_reservations before hosts, as it has constrains
-    // that depend on hosts. Therefore hosts table cannot be deleted while
-    // ipv6_reservations exists.
     "DROP TABLE ipv6_reservations",
     "DROP TABLE hosts",
     "DROP TABLE dhcp4_options",
@@ -84,7 +82,7 @@ const char* create_statement[] = {
     "CREATE TABLE lease6_types ("
         "lease_type TINYINT PRIMARY KEY NOT NULL,"
         "name VARCHAR(5)"
-        ")",
+        ") ENGINE = INNODB",
 
     "INSERT INTO lease6_types VALUES (0, \"IA_NA\")",
     "INSERT INTO lease6_types VALUES (1, \"IA_TA\")",
@@ -93,7 +91,7 @@ const char* create_statement[] = {
     "CREATE TABLE schema_version ("
         "version INT PRIMARY KEY NOT NULL,"
         "minor INT"
-        ")",
+        ") ENGINE = INNODB",
 
     "INSERT INTO schema_version VALUES (1, 0)",
     "COMMIT",