]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3476] Correct option def data type enum overlap
authorThomas Markwalder <tmark@isc.org>
Tue, 9 Jul 2024 17:38:58 +0000 (13:38 -0400)
committerThomas Markwalder <tmark@isc.org>
Thu, 18 Jul 2024 15:17:41 +0000 (11:17 -0400)
new files:
    src/share/database/scripts/mysql/upgrade_023_to_024.sh.in
    src/share/database/scripts/pgsql/upgrade_023_to_024.sh.in

configure.ac
    Added new upgrade files

    src/share/database/scripts/mysql/upgrade_023_to_024.sh
    src/share/database/scripts/pgsql/upgrade_023_to_024.sh

src/bin/admin/kea-admin.in
    mysql_upgrade()
    pgsql_upgrade() - now sort scripts numerically

src/bin/admin/tests/mysql_tests.sh.in
    mysql_upgrade_23_to_24_test() - new test function
    mysql_upgrade_test() - updated version check

src/bin/admin/tests/pgsql_tests.sh.in
    pgsql_db_version_test() - updated version check
    pgsql_upgrade_23_to_24_test()  - new test function

src/lib/dhcp/option_data_types.h
    Set explicit values for OPT_RECORD_TYPE and OPT_UKNOWN_TYPE

src/lib/dhcp/tests/option_definition_unittest.cc
    TEST(OptionDataTypeUtil, typeToString)
    TEST(OptionDataTypeUtil, stringToType) - new tests

src/lib/mysql/mysql_constants.h
    Updated schema version to 24.0

src/lib/pgsql/pgsql_connection.h
    Updated schema version to 24.0

src/share/database/scripts/mysql/dhcpdb_create.mysql
    Added option_def_data_type table creation

src/share/database/scripts/mysql/dhcpdb_drop.mysql
    Added option_def_data_type table drop

src/share/database/scripts/pgsql/dhcpdb_create.pgsql
    Added option_def_data_type table creation

src/share/database/scripts/pgsql/dhcpdb_drop.pgsql
    Added option_def_data_type table drop

18 files changed:
configure.ac
src/bin/admin/kea-admin.in
src/bin/admin/tests/mysql_tests.sh.in
src/bin/admin/tests/pgsql_tests.sh.in
src/lib/dhcp/option_data_types.h
src/lib/dhcp/tests/option_definition_unittest.cc
src/lib/mysql/mysql_constants.h
src/lib/pgsql/pgsql_connection.h
src/share/database/scripts/mysql/.gitignore
src/share/database/scripts/mysql/Makefile.am
src/share/database/scripts/mysql/dhcpdb_create.mysql
src/share/database/scripts/mysql/dhcpdb_drop.mysql
src/share/database/scripts/mysql/upgrade_023_to_024.sh.in [new file with mode: 0644]
src/share/database/scripts/pgsql/.gitignore
src/share/database/scripts/pgsql/Makefile.am
src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/dhcpdb_drop.pgsql
src/share/database/scripts/pgsql/upgrade_023_to_024.sh.in [new file with mode: 0644]

index 09ea780501e970ce17804da5a090ecec8525564e..405e5ef871af5dfa77ca2893fa130324897d352b 100644 (file)
@@ -1768,6 +1768,8 @@ AC_CONFIG_FILES([src/share/database/scripts/mysql/upgrade_021_to_022.sh],
                 [chmod +x src/share/database/scripts/mysql/upgrade_021_to_022.sh])
 AC_CONFIG_FILES([src/share/database/scripts/mysql/upgrade_022_to_023.sh],
                 [chmod +x src/share/database/scripts/mysql/upgrade_022_to_023.sh])
+AC_CONFIG_FILES([src/share/database/scripts/mysql/upgrade_023_to_024.sh],
+                [chmod +x src/share/database/scripts/mysql/upgrade_023_to_024.sh])
 AC_CONFIG_FILES([src/share/database/scripts/mysql/wipe_data.sh],
                 [chmod +x src/share/database/scripts/mysql/wipe_data.sh])
 AC_CONFIG_FILES([src/share/database/scripts/pgsql/Makefile])
@@ -1827,6 +1829,8 @@ AC_CONFIG_FILES([src/share/database/scripts/pgsql/upgrade_021_to_022.sh],
                 [chmod +x src/share/database/scripts/pgsql/upgrade_021_to_022.sh])
 AC_CONFIG_FILES([src/share/database/scripts/pgsql/upgrade_022_to_023.sh],
                 [chmod +x src/share/database/scripts/pgsql/upgrade_022_to_023.sh])
+AC_CONFIG_FILES([src/share/database/scripts/pgsql/upgrade_023_to_024.sh],
+                [chmod +x src/share/database/scripts/pgsql/upgrade_023_to_024.sh])
 AC_CONFIG_FILES([src/share/database/scripts/pgsql/wipe_data.sh],
                 [chmod +x src/share/database/scripts/pgsql/wipe_data.sh])
 AC_CONFIG_FILES([src/share/yang/Makefile])
index 283e446c47db53cf3c1328c7f64bd8d3cc1c6d20..098c11dcaa1ef9662745f0fea675894b8364023e 100644 (file)
@@ -376,7 +376,7 @@ mysql_upgrade() {
         echo "Processing $script file..."
         "${script}" --host="${db_host}" --user="${db_user}" \
         --password="${db_password}" "${db_name}" ${extra_arguments}
-    done
+    done | sort -V
 
     version=$(checked_mysql_version)
     printf 'Schema version reported after upgrade: %s\n' "${version}"
@@ -423,7 +423,7 @@ pgsql_upgrade() {
         echo "Processing $script file..."
         "${script}" -U "${db_user}" -h "${db_host}" \
         -d "${db_name}" ${extra_arguments}
-    done
+    done | sort -V
 
     version=$(checked_pgsql_version)
     printf 'Schema version reported after upgrade: %s\n' "${version}"
index b0b555b3a45e33e45386bad447e800f0a984fa64..4c79c0afeab83f03f5e14204ec0757af361e92f1 100644 (file)
@@ -821,6 +821,14 @@ mysql_upgrade_22_to_23_test() {
     assert_str_eq 'released' "${OUTPUT}" "${qry}: expected output %s, returned %s"
 }
 
+mysql_upgrade_23_to_24_test() {
+    query="SELECT count(id) from option_def_data_type"
+    run_command \
+        mysql_execute "${query}"
+    assert_eq 0 "${EXIT_CODE}" "${query}: expected %d, returned %d"
+    assert_str_eq '18' "${OUTPUT}" "${query}: expected output %s, returned %s"
+}
+
 mysql_upgrade_test() {
 
     test_start "mysql.upgrade"
@@ -842,7 +850,7 @@ mysql_upgrade_test() {
 
     # Verify that the upgraded schema reports the latest version.
     version=$("${kea_admin}" db-version mysql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
-    assert_str_eq "23.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
+    assert_str_eq "24.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
 
     # Let's check that the new tables are indeed there.
 
@@ -1505,10 +1513,12 @@ SET @disable_audit = 0"
     # Check upgrade from 18.0 to 19.0.
     mysql_upgrade_18_to_19_test
 
-
     # Check upgrade from 22.0 to 23.0.
     mysql_upgrade_22_to_23_test
 
+    # Check upgrade from 23.0 to 24.0.
+    mysql_upgrade_23_to_24_test
+
     # Let's wipe the whole database
     mysql_wipe
 
index 7f8a3406b21756d6c44f0fe5cef978342bf65cf1..b05c0aba10a2fe7651c626f53b00ea81ed1a669e 100644 (file)
@@ -142,7 +142,7 @@ pgsql_db_version_test() {
     run_command \
         "${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}"
     version="${OUTPUT}"
-    assert_str_eq "23.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
+    assert_str_eq "24.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
 
     # Let's wipe the whole database
     pgsql_wipe
@@ -917,6 +917,14 @@ pgsql_upgrade_22_to_23_test() {
     assert_str_eq 'released' "${OUTPUT}" "${query}: expected output %s, returned %s"
 }
 
+pgsql_upgrade_23_to_24_test() {
+    query="SELECT count(id) from option_def_data_type"
+    run_command \
+        pgsql_execute "${query}"
+    assert_eq 0 "${EXIT_CODE}" "${query}: expected %d, returned %d"
+    assert_str_eq '18' "${OUTPUT}" "${query}: expected output %s, returned %s"
+}
+
 pgsql_upgrade_test() {
     test_start "pgsql.upgrade"
 
@@ -935,7 +943,7 @@ pgsql_upgrade_test() {
 
     # Verify upgraded schema reports the latest version.
     version=$("${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
-    assert_str_eq "23.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
+    assert_str_eq "24.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
 
     # Check 1.0 to 2.0 upgrade
     pgsql_upgrade_1_0_to_2_0_test
@@ -997,6 +1005,9 @@ pgsql_upgrade_test() {
     # Check 22 to 23 upgrade
     pgsql_upgrade_22_to_23_test
 
+    # Check 23 to 24 upgrade
+    pgsql_upgrade_23_to_24_test
+
     # Let's wipe the whole database
     pgsql_wipe
 
index 58bee3d1e6fb78054148b054c672e7a8fb082eec..52e8a2e18d58d3c6681d1d9c7b577193995d588c 100644 (file)
@@ -61,8 +61,8 @@ enum OptionDataType {
     OPT_FQDN_TYPE,
     // Type to be used only internally. Allows convenient notation of the option config.
     OPT_INTERNAL_TYPE,
-    OPT_RECORD_TYPE,
-    OPT_UNKNOWN_TYPE
+    OPT_RECORD_TYPE = 254,  // Do not alter this value.
+    OPT_UNKNOWN_TYPE = 255  // Do not alter this value.
 };
 
 /// @brief Parameters being used to make up an option definition.
index 48c4b6111586b51357a9063e3cadcb959a7a2090..5ca6175de76928b27b94e0b01bfc4c8a9b11ae47 100644 (file)
@@ -2155,4 +2155,50 @@ TEST_F(OptionDefinitionTest, internalOptionTypeBinary) {
               option_cast->toText());
 }
 
+// Verify converstion of OptionDefintion type strings to type enums.
+TEST(OptionDataTypeUtil, typeToString) {
+    EXPECT_EQ(OPT_EMPTY_TYPE, OptionDataTypeUtil::getDataType("empty"));
+    EXPECT_EQ(OPT_BINARY_TYPE, OptionDataTypeUtil::getDataType("binary"));
+    EXPECT_EQ(OPT_BOOLEAN_TYPE, OptionDataTypeUtil::getDataType("boolean"));
+    EXPECT_EQ(OPT_INT8_TYPE, OptionDataTypeUtil::getDataType("int8"));
+    EXPECT_EQ(OPT_INT16_TYPE, OptionDataTypeUtil::getDataType("int16"));
+    EXPECT_EQ(OPT_INT32_TYPE, OptionDataTypeUtil::getDataType("int32"));
+    EXPECT_EQ(OPT_UINT8_TYPE, OptionDataTypeUtil::getDataType("uint8"));
+    EXPECT_EQ(OPT_UINT16_TYPE, OptionDataTypeUtil::getDataType("uint16"));
+    EXPECT_EQ(OPT_UINT32_TYPE, OptionDataTypeUtil::getDataType("uint32"));
+    EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, OptionDataTypeUtil::getDataType("ipv4-address"));
+    EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, OptionDataTypeUtil::getDataType("ipv6-address"));
+    EXPECT_EQ(OPT_IPV6_PREFIX_TYPE, OptionDataTypeUtil::getDataType("ipv6-prefix"));
+    EXPECT_EQ(OPT_PSID_TYPE, OptionDataTypeUtil::getDataType("psid"));
+    EXPECT_EQ(OPT_STRING_TYPE, OptionDataTypeUtil::getDataType("string"));
+    EXPECT_EQ(OPT_TUPLE_TYPE, OptionDataTypeUtil::getDataType("tuple"));
+    EXPECT_EQ(OPT_FQDN_TYPE, OptionDataTypeUtil::getDataType("fqdn"));
+    // EXPECT_EQ(OPT_INTERNAL_TYPE, OptionDataTypeUtil::getDataType("internal"));
+    EXPECT_EQ(OPT_RECORD_TYPE, OptionDataTypeUtil::getDataType("record"));
+    EXPECT_EQ(OPT_UNKNOWN_TYPE, OptionDataTypeUtil::getDataType("bogus"));
+}
+
+// Verify converstion of OptionDefintion type enums to type strings.
+TEST(OptionDataTypeUtil, stringToType) {
+    EXPECT_EQ("empty", OptionDataTypeUtil::getDataTypeName(OPT_EMPTY_TYPE));
+    EXPECT_EQ("binary", OptionDataTypeUtil::getDataTypeName( OPT_BINARY_TYPE));
+    EXPECT_EQ("boolean", OptionDataTypeUtil::getDataTypeName(OPT_BOOLEAN_TYPE));
+    EXPECT_EQ("int8", OptionDataTypeUtil::getDataTypeName(OPT_INT8_TYPE));
+    EXPECT_EQ("int16", OptionDataTypeUtil::getDataTypeName(OPT_INT16_TYPE));
+    EXPECT_EQ("int32", OptionDataTypeUtil::getDataTypeName(OPT_INT32_TYPE));
+    EXPECT_EQ("uint8", OptionDataTypeUtil::getDataTypeName(OPT_UINT8_TYPE));
+    EXPECT_EQ("uint16", OptionDataTypeUtil::getDataTypeName(OPT_UINT16_TYPE));
+    EXPECT_EQ("uint32", OptionDataTypeUtil::getDataTypeName(OPT_UINT32_TYPE));
+    EXPECT_EQ("ipv4-address", OptionDataTypeUtil::getDataTypeName(OPT_IPV4_ADDRESS_TYPE));
+    EXPECT_EQ("ipv6-address", OptionDataTypeUtil::getDataTypeName(OPT_IPV6_ADDRESS_TYPE));
+    EXPECT_EQ("ipv6-prefix", OptionDataTypeUtil::getDataTypeName(OPT_IPV6_PREFIX_TYPE));
+    EXPECT_EQ("psid", OptionDataTypeUtil::getDataTypeName(OPT_PSID_TYPE));
+    EXPECT_EQ("string", OptionDataTypeUtil::getDataTypeName(OPT_STRING_TYPE));
+    EXPECT_EQ("tuple", OptionDataTypeUtil::getDataTypeName(OPT_TUPLE_TYPE));
+    EXPECT_EQ("fqdn", OptionDataTypeUtil::getDataTypeName(OPT_FQDN_TYPE));
+    // EXPECT_EQ("internal", OptionDataTypeUtil::getDataTypeName(OPT_INTERNAL_TYPE));
+    EXPECT_EQ("record", OptionDataTypeUtil::getDataTypeName(OPT_RECORD_TYPE));
+    EXPECT_EQ("unknown", OptionDataTypeUtil::getDataTypeName(OPT_UNKNOWN_TYPE));
+}
+
 } // anonymous namespace
index 443faa4490a53759b74042ee4d8e28c0843ded31..273dd261fc1edbe2a1f28d9d9763d3a30a16a0cb 100644 (file)
@@ -52,7 +52,7 @@ const int MLM_MYSQL_FETCH_FAILURE = 0;
 
 /// @name Current database schema version values.
 //@{
-const uint32_t MYSQL_SCHEMA_VERSION_MAJOR = 23;
+const uint32_t MYSQL_SCHEMA_VERSION_MAJOR = 24;
 const uint32_t MYSQL_SCHEMA_VERSION_MINOR = 0;
 
 //@}
index e9f9fb2be3a5697e170c78b53fcd472e60fa48f5..f0d99948781bf3b8442845704ae360c0be67b44c 100644 (file)
@@ -18,7 +18,7 @@ namespace isc {
 namespace db {
 
 /// @brief Define the PostgreSQL backend version.
-const uint32_t PGSQL_SCHEMA_VERSION_MAJOR = 23;
+const uint32_t PGSQL_SCHEMA_VERSION_MAJOR = 24;
 const uint32_t PGSQL_SCHEMA_VERSION_MINOR = 0;
 
 // Maximum number of parameters that can be used a statement
index 653d970974b8d69bf6924afef9211f2acf04db92..d99c5a2f2e0420b061eafd57417133728e9c19be 100644 (file)
@@ -31,4 +31,5 @@
 /upgrade_020_to_021.sh
 /upgrade_021_to_022.sh
 /upgrade_022_to_023.sh
+/upgrade_023_to_024.sh
 /wipe_data.sh
index 5ab615559de7078225dbfc91fa37d2961257c39a..d74e597641b7f733141098a64b5bfdaa67a08742 100644 (file)
@@ -42,6 +42,7 @@ mysql_SCRIPTS += upgrade_019_to_020.sh
 mysql_SCRIPTS += upgrade_020_to_021.sh
 mysql_SCRIPTS += upgrade_021_to_022.sh
 mysql_SCRIPTS += upgrade_022_to_023.sh
+mysql_SCRIPTS += upgrade_023_to_024.sh
 mysql_SCRIPTS += wipe_data.sh
 
 DISTCLEANFILES = ${mysql_SCRIPTS}
index c3c51ecb02d8712d8b8a4ae68e82116a1f833a29..b882c12fdc148574d590f65b67e426e08819dde8 100644 (file)
@@ -5927,6 +5927,77 @@ UPDATE schema_version
 
 -- This line concludes the schema upgrade to version 23.0.
 
+-- This line starts the schema upgrade to version 24.0.
+
+-- Create a function to conditionally migrate option_def data type
+-- values.  If they are updating from 2.6.1 this has been done already
+-- and we don't want to do it twice.
+DROP PROCEDURE IF EXISTS updateOptionDataDef;
+DELIMITER $$
+CREATE PROCEDURE updateOptionDataDef()
+BEGIN
+    DECLARE skipper TINYINT;
+
+    SELECT COUNT(table_name) FROM information_schema.tables
+        WHERE table_schema LIKE database() AND table_name = 'option_def_data_type' into skipper;
+
+    IF skipper = 0 THEN
+        -- First we migrate existing OPT_RECORD_TYPE values
+        -- If they're coming from pre-2.6.0 record type is 17, from
+        -- 2.6.0 or 2.7.0 it is 18. No viable to way to know where we
+        -- started.
+        UPDATE dhcp4_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+        UPDATE dhcp6_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+
+        -- Create the table that enumerates option definition data types.
+        CREATE TABLE option_def_data_type (
+            id TINYINT UNSIGNED NOT NULL PRIMARY KEY,
+            name VARCHAR(32) NOT NULL
+        ) ENGINE = InnoDB;
+
+        -- Now insert supported types.
+        -- We skip (9, 'any-address') as it is not externally supported.
+        INSERT INTO option_def_data_type VALUES
+            (0, 'empty'),
+            (1, 'binary'),
+            (2, 'boolean'),
+            (3, 'int8"'),
+            (4, 'int16'),
+            (5, 'int32'),
+            (6, 'uint8'),
+            (7, 'uint16'),
+            (8, 'uint32'),
+            (10, 'ipv4-address'),
+            (11, 'ipv6-address'),
+            (12, 'ipv6-prefix'),
+            (13, 'psid'),
+            (14, 'string'),
+            (15, 'tuple'),
+            (16, 'fqdn'),
+            (17, 'internal'),
+            (254, 'record');
+
+        --  Add foreign key constraints to enforce only valid types.
+        ALTER TABLE dhcp4_option_def
+          ADD CONSTRAINT fk_option_def_data_type4 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+        ALTER TABLE dhcp6_option_def
+          ADD CONSTRAINT fk_option_def_data_type6 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+    END IF;
+END $$
+DELIMITER ;
+
+call updateOptionDataDef();
+
+-- Get rid of the now obsolete function.
+DROP PROCEDURE IF EXISTS updateOptionDataDef;
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '24', minor = '0';
+
+-- This line concludes the schema upgrade to version 24.0.
+
 # Notes:
 #
 # Indexes
index 7bbe6af08d9dc51d8306f8139d3d9b3cc2b84a7b..82881efb0f33251eff0135a928ee3acb58bbf346 100644 (file)
@@ -161,3 +161,4 @@ DROP FUNCTION IF EXISTS checkLease6Limits;
 DROP FUNCTION IF EXISTS isJsonSupported;
 DROP TABLE IF EXISTS lease6_relay_id;
 DROP TABLE IF EXISTS lease6_remote_id;
+DROP TABLE IF EXISTS option_def_data_type;
diff --git a/src/share/database/scripts/mysql/upgrade_023_to_024.sh.in b/src/share/database/scripts/mysql/upgrade_023_to_024.sh.in
new file mode 100644 (file)
index 0000000..f750a7d
--- /dev/null
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+# Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+# shellcheck disable=SC2034
+# SC2034: ... appears unused. Verify use (or export if used externally).
+prefix="@prefix@"
+
+# Include utilities based on location of this script. Check for sources first,
+# so that the unexpected situations with weird paths fall on the default
+# case of installed.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+if test "${script_path}" = "@abs_top_builddir@/src/share/database/scripts/mysql"; then
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
+else
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
+fi
+
+# Check version.
+version=$(mysql_version "${@}")
+if test "${version}" != "23.0"; then
+    printf 'This script upgrades 23.0 to 24.0. '
+    printf 'Reported version is %s. Skipping upgrade.\n' "${version}"
+    exit 0
+fi
+
+# Get the schema name from database argument. We need this to
+# query information_schema for the right database.
+for arg in "${@}"
+do
+    if ! printf '%s' "${arg}" | grep -Eq -- '^--'
+    then
+        schema="$arg"
+        break
+    fi
+done
+
+# Make sure we have the schema.
+if [ -z "$schema" ]
+then
+    printf "Could not find database schema name in cmd line args: %s\n" "${*}"
+    exit 255
+fi
+
+mysql "$@" <<EOF
+
+-- This line starts the schema upgrade to version 24.0.
+
+-- Create a function to conditionally migrate option_def data type
+-- values.  If they are updating from 2.6.1 this has been done already
+-- and we don't want to do it twice.
+DROP PROCEDURE IF EXISTS updateOptionDataDef;
+DELIMITER $$
+CREATE PROCEDURE updateOptionDataDef()
+BEGIN
+    DECLARE skipper TINYINT;
+
+    SELECT COUNT(table_name) FROM information_schema.tables
+        WHERE table_schema LIKE database() AND table_name = 'option_def_data_type' into skipper;
+
+    IF skipper = 0 THEN
+        -- First we migrate existing OPT_RECORD_TYPE values
+        -- If they're coming from pre-2.6.0 record type is 17, from
+        -- 2.6.0 or 2.7.0 it is 18. No viable to way to know where we
+        -- started.
+        UPDATE dhcp4_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+        UPDATE dhcp6_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+
+        -- Create the table that enumerates option definition data types.
+        CREATE TABLE option_def_data_type (
+            id TINYINT UNSIGNED NOT NULL PRIMARY KEY,
+            name VARCHAR(32) NOT NULL
+        ) ENGINE = InnoDB;
+
+        -- Now insert supported types.
+        -- We skip (9, 'any-address') as it is not externally supported.
+        INSERT INTO option_def_data_type VALUES
+            (0, 'empty'),
+            (1, 'binary'),
+            (2, 'boolean'),
+            (3, 'int8"'),
+            (4, 'int16'),
+            (5, 'int32'),
+            (6, 'uint8'),
+            (7, 'uint16'),
+            (8, 'uint32'),
+            (10, 'ipv4-address'),
+            (11, 'ipv6-address'),
+            (12, 'ipv6-prefix'),
+            (13, 'psid'),
+            (14, 'string'),
+            (15, 'tuple'),
+            (16, 'fqdn'),
+            (17, 'internal'),
+            (254, 'record');
+
+        --  Add foreign key constraints to enforce only valid types.
+        ALTER TABLE dhcp4_option_def
+          ADD CONSTRAINT fk_option_def_data_type4 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+        ALTER TABLE dhcp6_option_def
+          ADD CONSTRAINT fk_option_def_data_type6 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+    END IF;
+END $$
+DELIMITER ;
+
+call updateOptionDataDef();
+
+-- Get rid of the now obsolete function.
+DROP PROCEDURE IF EXISTS updateOptionDataDef;
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '24', minor = '0';
+
+-- This line concludes the schema upgrade to version 24.0.
+
+EOF
index b039f2d93787c804d96ef5773573c3b6f3206248..9c47fd671b1dee5bc150732efc959dde771f8416 100644 (file)
@@ -26,4 +26,5 @@
 /upgrade_020_to_021.sh
 /upgrade_021_to_022.sh
 /upgrade_022_to_023.sh
+/upgrade_023_to_024.sh
 /wipe_data.sh
index 8150d9cb17ebeadb5a5b6d86da84921b6db57aff..a05e9b7a50c7607fbb6e269274c52f04464227c4 100644 (file)
@@ -37,6 +37,7 @@ pgsql_SCRIPTS += upgrade_019_to_020.sh
 pgsql_SCRIPTS += upgrade_020_to_021.sh
 pgsql_SCRIPTS += upgrade_021_to_022.sh
 pgsql_SCRIPTS += upgrade_022_to_023.sh
+pgsql_SCRIPTS += upgrade_023_to_024.sh
 pgsql_SCRIPTS += wipe_data.sh
 
 DISTCLEANFILES = ${pgsql_SCRIPTS}
index 8459ffff4c90742a8fa4ac45e0136017d313a7db..b70414e942a19af8813aa8a4e4188778c657a5da 100644 (file)
@@ -6387,8 +6387,80 @@ INSERT INTO lease_state VALUES (3, 'released');
 UPDATE schema_version
     SET version = '23', minor = '0';
 
--- This line concludes the schema upgrade to version 23.0.
+-- This line starts the schema upgrade to version 24.0.
+
+-- Create a function to conditionally migrate option_def data type
+-- values.  If they are updating from 2.6.1 this has been done already
+-- and we don't want to do it twice.
+DROP FUNCTION IF EXISTS updateOptionDataDef();
+CREATE OR REPLACE FUNCTION updateOptionDataDef() RETURNS text AS $$
+    DECLARE skipper BOOLEAN;
+BEGIN
+    SELECT exists(SELECT FROM pg_tables WHERE tablename = 'option_def_data_type') into skipper;
+    IF skipper THEN
+        -- Table already exists which means the migration was already done.
+        RETURN 'EXISTS';
+    END IF;
+
+    -- First we migrate existing OPT_RECORD_TYPE values
+    -- If they're coming from pre-2.6.0 record type is 17, from
+    -- 2.6.0 or 2.7.0 it is 18. No viable to way to know where we
+    -- started.
+
+    -- Can we attempt to discern between internal and rec by testing
+    -- record_types?
+    UPDATE dhcp4_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+    UPDATE dhcp6_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+
+    -- Create the table that enumerates option definition data types.
+    CREATE TABLE option_def_data_type (
+        id smallint NOT NULL,
+        name VARCHAR(32) NOT NULL,
+        PRIMARY KEY (id)
+    );
+
+    -- Now insert supported types.
+    -- We skip (9, 'any-address') as it is not externally supported.
+    INSERT INTO option_def_data_type VALUES
+        (0, 'empty'),
+        (1, 'binary'),
+        (2, 'boolean'),
+        (3, 'int8"'),
+        (4, 'int16'),
+        (5, 'int32'),
+        (6, 'uint8'),
+        (7, 'uint16'),
+        (8, 'uint32'),
+        (10, 'ipv4-address'),
+        (11, 'ipv6-address'),
+        (12, 'ipv6-prefix'),
+        (13, 'psid'),
+        (14, 'string'),
+        (15, 'tuple'),
+        (16, 'fqdn'),
+        (17, 'internal'),
+        (254, 'record');
+
+    --  Add foreign key constraints to enforce only valid types.
+    ALTER TABLE dhcp4_option_def
+        ADD CONSTRAINT fk_option_def_data_type4 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+    ALTER TABLE dhcp6_option_def
+        ADD CONSTRAINT fk_option_def_data_type6 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+    RETURN 'UPDATED';
+END;
+$$ LANGUAGE plpgsql;
+
+select updateOptionDataDef();
+
+-- Get rid of the now obsolete function.
+DROP FUNCTION IF EXISTS updateOptionDataDef();
+
+UPDATE schema_version
+    SET version = '24', minor = '0';
 
+-- This line concludes the schema upgrade to version 24.0.
 
 -- Commit the script transaction.
 COMMIT;
index 044d9aa2346abeb70801a5f01c582e762014a388..9334e6235e07786bc8185062b6aee51717624a25 100644 (file)
@@ -31,6 +31,7 @@ DROP FUNCTION IF EXISTS proc_stat_lease6_insert ();
 DROP FUNCTION IF EXISTS proc_stat_lease6_update ();
 DROP FUNCTION IF EXISTS proc_stat_lease6_delete ();
 DROP TABLE IF EXISTS logs CASCADE;
+DROP TABLE IF EXISTS option_def_data_type CASCADE;
 
 -- config backend procedures for DHCPv6
 DROP FUNCTION IF EXISTS createAuditRevisionDHCP6(audit_ts TIMESTAMP WITH TIME ZONE, server_tag VARCHAR(64),
diff --git a/src/share/database/scripts/pgsql/upgrade_023_to_024.sh.in b/src/share/database/scripts/pgsql/upgrade_023_to_024.sh.in
new file mode 100644 (file)
index 0000000..ecf920e
--- /dev/null
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+# Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+# shellcheck disable=SC2034
+# SC2034: ... appears unused. Verify use (or export if used externally).
+prefix="@prefix@"
+
+# Include utilities based on location of this script. Check for sources first,
+# so that the unexpected situations with weird paths fall on the default
+# case of installed.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+if test "${script_path}" = "@abs_top_builddir@/src/share/database/scripts/pgsql"; then
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
+else
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
+fi
+
+VERSION=$(pgsql_version "$@")
+
+if [ "$VERSION" != "23.0" ]; then
+    printf 'This script upgrades 23.0 to 24.0. '
+    printf 'Reported version is %s. Skipping upgrade.\n' "${VERSION}"
+    exit 0
+fi
+
+psql "$@" >/dev/null <<EOF
+START TRANSACTION;
+
+-- This line starts the schema upgrade to version 24.0.
+
+-- Create a function to conditionally migrate option_def data type
+-- values.  If they are updating from 2.6.1 this has been done already
+-- and we don't want to do it twice.
+DROP FUNCTION IF EXISTS updateOptionDataDef();
+CREATE OR REPLACE FUNCTION updateOptionDataDef() RETURNS text AS \$\$
+    DECLARE skipper BOOLEAN;
+BEGIN
+    SELECT exists(SELECT FROM pg_tables WHERE tablename = 'option_def_data_type') into skipper;
+    IF skipper THEN
+        -- Table already exists which means the migration was already done.
+        RETURN 'EXISTS';
+    END IF;
+
+    -- First we migrate existing OPT_RECORD_TYPE values
+    -- If they're coming from pre-2.6.0 record type is 17, from
+    -- 2.6.0 or 2.7.0 it is 18. No viable to way to know where we
+    -- started.
+
+    -- Can we attempt to discern between internal and rec by testing
+    -- record_types?
+    UPDATE dhcp4_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+    UPDATE dhcp6_option_def SET type = 254 WHERE type >= 17 AND record_types IS NOT NULL;
+
+    -- Create the table that enumerates option definition data types.
+    CREATE TABLE option_def_data_type (
+        id smallint NOT NULL,
+        name VARCHAR(32) NOT NULL,
+        PRIMARY KEY (id)
+    );
+
+    -- Now insert supported types.
+    -- We skip (9, 'any-address') as it is not externally supported.
+    INSERT INTO option_def_data_type VALUES
+        (0, 'empty'),
+        (1, 'binary'),
+        (2, 'boolean'),
+        (3, 'int8"'),
+        (4, 'int16'),
+        (5, 'int32'),
+        (6, 'uint8'),
+        (7, 'uint16'),
+        (8, 'uint32'),
+        (10, 'ipv4-address'),
+        (11, 'ipv6-address'),
+        (12, 'ipv6-prefix'),
+        (13, 'psid'),
+        (14, 'string'),
+        (15, 'tuple'),
+        (16, 'fqdn'),
+        (17, 'internal'),
+        (254, 'record');
+
+    --  Add foreign key constraints to enforce only valid types.
+    ALTER TABLE dhcp4_option_def
+        ADD CONSTRAINT fk_option_def_data_type4 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+    ALTER TABLE dhcp6_option_def
+        ADD CONSTRAINT fk_option_def_data_type6 FOREIGN KEY (type) REFERENCES option_def_data_type(id);
+
+    RETURN 'UPDATED';
+END;
+\$\$ LANGUAGE plpgsql;
+
+select updateOptionDataDef();
+
+-- Get rid of the now obsolete function.
+DROP FUNCTION IF EXISTS updateOptionDataDef();
+
+UPDATE schema_version
+    SET version = '24', minor = '0';
+
+-- This line concludes the schema upgrade to version 24.0.
+
+-- Commit the script transaction.
+COMMIT;
+
+EOF