From: Thomas Markwalder Date: Fri, 8 Mar 2019 17:06:38 +0000 (-0500) Subject: [#526,!269] - MySQL unit tests now wipe data, instead of schema X-Git-Tag: Kea-1.6.0-beta~391^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6af5e78ec87a98fd0980764736ffaf6a15656805;p=thirdparty%2Fkea.git [#526,!269] - MySQL unit tests now wipe data, instead of schema src/share/database/scripts/mysql/wipe_data.sh.in - New shell script which intelligently deletes data from the schema IF the schema version matches the expected version src/lib/mysql/testutils/mysql_schema.* destroyMySQLSchema() - modfied to default to calling wipeData(), and only destroying the schema if that fails or by force flag = true; createMySQLSchema() - modified to default to calling wipeData(), and only recreating the schema if that fails or by force flag = true; wipeData() - new method which runs the MySQL wipe_data.sh shell script, passing in the expected schema version. src/share/database/scripts/mysql/dhcpdb_create.mysql createAuditRevisionDHCP4() createAuditEntryDHCP4() createAuditRevisionDHCP6() createAuditEntryDHCP6() - added session variable, @disable_audit, to allow procedures to be "turned off" during data wiping. src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_mgr_unittest.cc src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc src/lib/dhcpsrv/tests/host_mgr_unittest.cc src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc Removed calls to destroy schema from test constructors --- diff --git a/configure.ac b/configure.ac index 5491715675..69fde01b2b 100644 --- a/configure.ac +++ b/configure.ac @@ -1696,6 +1696,7 @@ AC_CONFIG_FILES([Makefile src/share/database/scripts/mysql/upgrade_5.2_to_6.0.sh src/share/database/scripts/mysql/upgrade_6.0_to_7.0.sh src/share/database/scripts/mysql/upgrade_7.0_to_8.0.sh + src/share/database/scripts/mysql/wipe_data.sh src/share/database/scripts/pgsql/Makefile src/share/database/scripts/pgsql/upgrade_1.0_to_2.0.sh src/share/database/scripts/pgsql/upgrade_2.0_to_3.0.sh diff --git a/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_mgr_unittest.cc b/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_mgr_unittest.cc index 8aa67e809a..ab26ec7d69 100644 --- a/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_mgr_unittest.cc +++ b/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2019 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 @@ -30,7 +30,6 @@ public: ConfigBackendDHCPv4Mgr::create(); // Recreate database schema. - destroyMySQLSchema(); createMySQLSchema(); } diff --git a/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc b/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc index de1529d333..102e521cc8 100644 --- a/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc +++ b/src/hooks/dhcp/mysql_cb/tests/mysql_cb_dhcp4_unittest.cc @@ -48,7 +48,6 @@ public: MySqlConfigBackendDHCPv4Test() : test_subnets_(), test_networks_(), timestamps_(), audit_entries_() { // Recreate database schema. - destroyMySQLSchema(); createMySQLSchema(); try { diff --git a/src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc b/src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc index e57a6793ec..22ce7306aa 100644 --- a/src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_db_access_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2019 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 @@ -148,7 +148,6 @@ public: /// @brief Constructor. CfgMySQLDbAccessTest() { // Ensure schema is the correct one. - destroyMySQLSchema(); createMySQLSchema(); } diff --git a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc index 45091377c2..adba8f904e 100644 --- a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc @@ -258,7 +258,7 @@ void HostMgrTest::testGetAll(BaseHostDataSource& data_source1, BaseHostDataSource& data_source2) { // Initially, no reservations should be present. - ConstHostCollection hosts = + ConstHostCollection hosts = HostMgr::instance().getAll(Host::IDENT_HWADDR, &hwaddrs_[1]->hwaddr_[0], hwaddrs_[1]->hwaddr_.size()); @@ -863,7 +863,7 @@ public: /// appropriate schema and create a basic host manager to /// wipe out any prior instance virtual void SetUp() { - DatabaseConnection::db_lost_callback = 0; + DatabaseConnection::db_lost_callback = 0; destroySchema(); createSchema(); // Wipe out any pre-existing mgr @@ -875,7 +875,7 @@ public: /// Invoked by gtest upon test exit, we destroy the schema /// we created. virtual void TearDown() { - DatabaseConnection::db_lost_callback = 0; + DatabaseConnection::db_lost_callback = 0; destroySchema(); } @@ -972,7 +972,6 @@ MySQLHostMgrTest::SetUp() { HostMgrTest::SetUp(); // Ensure schema is the correct one. - db::test::destroyMySQLSchema(); db::test::createMySQLSchema(); // Connect to the database diff --git a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc index 57c3258d15..13a8b6320e 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -42,7 +42,6 @@ public: /// @brief Clears the database and opens connection to it. void initializeTest() { // Ensure schema is the correct one. - destroyMySQLSchema(); createMySQLSchema(); // Connect to the database @@ -158,7 +157,6 @@ public: TEST(MySqlHostDataSource, OpenDatabase) { // Schema needs to be created for the test to work. - destroyMySQLSchema(); createMySQLSchema(); // Check that host manager open the database opens correctly and tidy up. diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 851fbc777a..23abb1d749 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2019 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 @@ -44,7 +44,6 @@ public: /// @brief Clears the database and opens connection to it. void initializeTest() { // Ensure schema is the correct one. - destroyMySQLSchema(); createMySQLSchema(); // Connect to the database @@ -110,7 +109,6 @@ public: /// opened: the fixtures assume that and check basic operations. TEST(MySqlOpenTest, OpenDatabase) { // Schema needs to be created for the test to work. - destroyMySQLSchema(true); createMySQLSchema(true); // Check that lease manager open the database opens correctly and tidy up. diff --git a/src/lib/mysql/testutils/mysql_schema.cc b/src/lib/mysql/testutils/mysql_schema.cc index 93ed9418c2..7d7e9a8590 100644 --- a/src/lib/mysql/testutils/mysql_schema.cc +++ b/src/lib/mysql/testutils/mysql_schema.cc @@ -30,12 +30,40 @@ validMySQLConnectionString() { VALID_USER, VALID_PASSWORD)); } -void destroyMySQLSchema(bool show_err) { - runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_drop.mysql", show_err); +void destroyMySQLSchema(bool show_err, bool force) { + // If force is true or wipeData() fails, destory the schema. + if (force || wipeData(show_err)) { + runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_drop.mysql", show_err); + } +} + +void createMySQLSchema(bool show_err, bool force) { + // If force is true or wipeData() fails, recreate the schema. + if (force || wipeData(show_err)) { + destroyMySQLSchema(show_err, true); + runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_create.mysql", show_err); + } } -void createMySQLSchema(bool show_err) { - runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_create.mysql", show_err); +bool wipeData(bool show_err) { + std::ostringstream cmd; + cmd << "sh " << DATABASE_SCRIPTS_DIR << "/"; + + std::ostringstream version; + version << MYSQL_SCHEMA_VERSION_MAJOR << "." << MYSQL_SCHEMA_VERSION_MINOR; + + cmd << "mysql/wipe_data.sh" << " " << version.str() + << " -N -B --user=keatest --password=keatest keatest"; + if (!show_err) { + cmd << " 2>/dev/null "; + } + + int retval = ::system(cmd.str().c_str()); + if (retval) { + std::cerr << "wipeData failed:[" << cmd.str() << "]" << std::endl; + } + + return(retval); } void runMySQLScript(const std::string& path, const std::string& script_name, diff --git a/src/lib/mysql/testutils/mysql_schema.h b/src/lib/mysql/testutils/mysql_schema.h index 9457a3341c..fc5bce3146 100644 --- a/src/lib/mysql/testutils/mysql_schema.h +++ b/src/lib/mysql/testutils/mysql_schema.h @@ -22,31 +22,58 @@ extern const char* MYSQL_VALID_TYPE; /// @return valid MySQL connection string. std::string validMySQLConnectionString(); -/// @brief Clear everything from the database +/// @brief Clear the unit test database /// -/// Submits the current schema drop script: +/// Either calls @c wipeData to wipe the data from the +/// database /or destroys the database itself by submitting the +/// SQL script: /// -/// /mysql/dhcpdb_drop.mysql +/// /mysql/dhcpdb_drop.sh /// -/// to the unit test MySQL database. If the script fails, the invoking test -/// will fail. The output of stderr is suppressed unless the parameter, +/// If wipeData() is called and fails, it will destroy +/// the schema. If the schema destruction fails, the +/// invoking test should fail. +/// +/// The output stderr is suppressed unless the parameter, /// show_err is true. /// /// @param show_err flag which governs whether or not stderr is suppressed. -void destroyMySQLSchema(bool show_err = false); +/// @param force if true, the function will simply destroy the schema. +void destroyMySQLSchema(bool show_err = false, bool force = false); -/// @brief Create the MySQL Schema +/// @brief Create the unit test MySQL Schema /// -/// Submits the current schema creation script: +/// Ensures the unit test database is a empty and version-correct. +/// Unless, the force parameter is true, it will first attempt +/// to wipe the data from the database by calling @c wipeData. +/// If this call succeeds the function returns, otherwise it will +/// will call @c destroyMySQLSchema to forcibly remove the +/// existing schema and then submits the SQL script: /// /// /mysql/dhcpdb_create.mysql /// -/// to the unit test MySQL database. If the script fails, the invoking test -/// will fail. The output of stderr is suppressed unless the parameter, -/// show_err is true. +/// to the unit test MySQL database. +/// +/// @param show_err flag which governs whether or not stderr is suppressed. +/// @param force flag when true, the function will recreate the database +/// schema. +void createMySQLSchema(bool show_err = false, bool force = false); + + +/// @brief Attempts to wipe data from the MySQL unit test database +/// +/// Runs the shell script +/// +/// /mysql/wipe_data.sh +/// +/// This will fail if there is no schema, if the existing schema +/// version is incorrect (i.e. does not match MYSQL_SCHEMA_VERSION_MAJOR +/// and MYSQL_SCHEMA_VERSION_MINOR), or a SQL error occurs. Otherwise, +/// the script is should delete all transient data, leaving intact +/// reference tables. /// /// @param show_err flag which governs whether or not stderr is suppressed. -void createMySQLSchema(bool show_err = false); +bool wipeData(bool show_err = false); /// @brief Run a MySQL SQL script against the MySQL unit test database /// diff --git a/src/share/database/scripts/mysql/Makefile.am b/src/share/database/scripts/mysql/Makefile.am index fdf0f41e63..61263c38d2 100644 --- a/src/share/database/scripts/mysql/Makefile.am +++ b/src/share/database/scripts/mysql/Makefile.am @@ -13,6 +13,7 @@ sqlscripts_DATA += upgrade_5.1_to_5.2.sh sqlscripts_DATA += upgrade_5.2_to_6.0.sh sqlscripts_DATA += upgrade_6.0_to_7.0.sh sqlscripts_DATA += upgrade_7.0_to_8.0.sh +sqlscripts_DATA += wipe_data.sh DISTCLEANFILES = upgrade_1.0_to_2.0.sh DISTCLEANFILES += upgrade_2.0_to_3.0.sh @@ -24,5 +25,6 @@ DISTCLEANFILES += upgrade_5.1_to_5.2.sh DISTCLEANFILES += upgrade_5.2_to_6.0.sh DISTCLEANFILES += upgrade_6.0_to_7.0.sh DISTCLEANFILES += upgrade_7.0_to_8.0.sh +DISTCLEANFILES += wipe_data.sh EXTRA_DIST = ${sqlscripts_DATA} diff --git a/src/share/database/scripts/mysql/dhcpdb_create.mysql b/src/share/database/scripts/mysql/dhcpdb_create.mysql index 01526513f0..1e01cdcfa1 100644 --- a/src/share/database/scripts/mysql/dhcpdb_create.mysql +++ b/src/share/database/scripts/mysql/dhcpdb_create.mysql @@ -1543,6 +1543,10 @@ ALTER TABLE dhcp4_audit -- hand, if the global option is being added, the -- audit entry for the option must be created because -- it is the sole object modified in that case. +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditRevisionDHCP4; DELIMITER $$ @@ -1552,11 +1556,13 @@ CREATE PROCEDURE createAuditRevisionDHCP4(IN audit_ts TIMESTAMP, IN cascade_transaction TINYINT(1)) BEGIN DECLARE srv_id BIGINT(20); - SELECT id INTO srv_id FROM dhcp4_server WHERE tag = server_tag; - INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message) - VALUES (audit_ts, srv_id, audit_log_message); - SET @audit_revision_id = LAST_INSERT_ID(); - SET @cascade_transaction = cascade_transaction; + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + SELECT id INTO srv_id FROM dhcp4_server WHERE tag = server_tag; + INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message) + VALUES (audit_ts, srv_id, audit_log_message); + SET @audit_revision_id = LAST_INSERT_ID(); + SET @cascade_transaction = cascade_transaction; + END IF; END $$ DELIMITER ; @@ -1575,6 +1581,10 @@ DELIMITER ; -- - modification_type_val: string value indicating the -- type of the change, i.e. "create", "update" or -- "delete". +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ---------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditEntryDHCP4; DELIMITER $$ @@ -1582,10 +1592,12 @@ CREATE PROCEDURE createAuditEntryDHCP4(IN object_type_val VARCHAR(256), IN object_id_val BIGINT(20) UNSIGNED, IN modification_type_val VARCHAR(32)) BEGIN - INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id) - VALUES (object_type_val, object_id_val, \ + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id) + VALUES (object_type_val, object_id_val, \ (SELECT id FROM modification WHERE modification_type = modification_type_val), \ @audit_revision_id); + END IF; END $$ DELIMITER ; @@ -1955,6 +1967,10 @@ ALTER TABLE dhcp6_audit -- hand, if the global option is being added, the -- audit entry for the option must be created because -- it is the sole object modified in that case. +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditRevisionDHCP6; DELIMITER $$ @@ -1964,11 +1980,13 @@ CREATE PROCEDURE createAuditRevisionDHCP6(IN audit_ts TIMESTAMP, IN cascade_transaction TINYINT(1)) BEGIN DECLARE srv_id BIGINT(20); - SELECT id INTO srv_id FROM dhcp6_server WHERE tag = server_tag; - INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message) - VALUES (audit_ts, srv_id, audit_log_message); - SET @audit_revision_id = LAST_INSERT_ID(); - SET @cascade_transaction = cascade_transaction; + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + SELECT id INTO srv_id FROM dhcp6_server WHERE tag = server_tag; + INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message) + VALUES (audit_ts, srv_id, audit_log_message); + SET @audit_revision_id = LAST_INSERT_ID(); + SET @cascade_transaction = cascade_transaction; + END IF; END $$ DELIMITER ; @@ -1987,6 +2005,10 @@ DELIMITER ; -- - modification_type_val: string value indicating the -- type of the change, i.e. "create", "update" or -- "delete". +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ---------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditEntryDHCP6; DELIMITER $$ @@ -1994,10 +2016,12 @@ CREATE PROCEDURE createAuditEntryDHCP6(IN object_type_val VARCHAR(256), IN object_id_val BIGINT(20) UNSIGNED, IN modification_type_val VARCHAR(32)) BEGIN - INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id) - VALUES (object_type_val, object_id_val, \ + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id) + VALUES (object_type_val, object_id_val, \ (SELECT id FROM modification WHERE modification_type = modification_type_val), \ @audit_revision_id); + END IF; END $$ DELIMITER ; diff --git a/src/share/database/scripts/mysql/upgrade_7.0_to_8.0.sh.in b/src/share/database/scripts/mysql/upgrade_7.0_to_8.0.sh.in index b574a47ce0..c76e94d093 100644 --- a/src/share/database/scripts/mysql/upgrade_7.0_to_8.0.sh.in +++ b/src/share/database/scripts/mysql/upgrade_7.0_to_8.0.sh.in @@ -225,6 +225,10 @@ ALTER TABLE dhcp4_audit -- hand, if the global option is being added, the -- audit entry for the option must be created because -- it is the sole object modified in that case. +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditRevisionDHCP4; DELIMITER $$ @@ -234,11 +238,13 @@ CREATE PROCEDURE createAuditRevisionDHCP4(IN audit_ts TIMESTAMP, IN cascade_transaction TINYINT(1)) BEGIN DECLARE srv_id BIGINT(20); - SELECT id INTO srv_id FROM dhcp4_server WHERE tag = server_tag; - INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message) - VALUES (audit_ts, srv_id, audit_log_message); - SET @audit_revision_id = LAST_INSERT_ID(); - SET @cascade_transaction = cascade_transaction; + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + SELECT id INTO srv_id FROM dhcp4_server WHERE tag = server_tag; + INSERT INTO dhcp4_audit_revision (modification_ts, server_id, log_message) + VALUES (audit_ts, srv_id, audit_log_message); + SET @audit_revision_id = LAST_INSERT_ID(); + SET @cascade_transaction = cascade_transaction; + END IF; END $$ DELIMITER ; @@ -257,6 +263,10 @@ DELIMITER ; -- - modification_type_val: string value indicating the -- type of the change, i.e. "create", "update" or -- "delete". +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ---------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditEntryDHCP4; DELIMITER $$ @@ -264,10 +274,12 @@ CREATE PROCEDURE createAuditEntryDHCP4(IN object_type_val VARCHAR(256), IN object_id_val BIGINT(20) UNSIGNED, IN modification_type_val VARCHAR(32)) BEGIN - INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id) - VALUES (object_type_val, object_id_val, \ - (SELECT id FROM modification WHERE modification_type = modification_type_val), \ - @audit_revision_id); + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id) + VALUES (object_type_val, object_id_val, \ + (SELECT id FROM modification WHERE modification_type = modification_type_val), \ + @audit_revision_id); + END IF; END $$ DELIMITER ; @@ -637,6 +649,10 @@ ALTER TABLE dhcp6_audit -- hand, if the global option is being added, the -- audit entry for the option must be created because -- it is the sole object modified in that case. +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditRevisionDHCP6; DELIMITER $$ @@ -646,11 +662,13 @@ CREATE PROCEDURE createAuditRevisionDHCP6(IN audit_ts TIMESTAMP, IN cascade_transaction TINYINT(1)) BEGIN DECLARE srv_id BIGINT(20); - SELECT id INTO srv_id FROM dhcp6_server WHERE tag = server_tag; - INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message) - VALUES (audit_ts, srv_id, audit_log_message); - SET @audit_revision_id = LAST_INSERT_ID(); - SET @cascade_transaction = cascade_transaction; + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + SELECT id INTO srv_id FROM dhcp6_server WHERE tag = server_tag; + INSERT INTO dhcp6_audit_revision (modification_ts, server_id, log_message) + VALUES (audit_ts, srv_id, audit_log_message); + SET @audit_revision_id = LAST_INSERT_ID(); + SET @cascade_transaction = cascade_transaction; + END IF; END $$ DELIMITER ; @@ -669,6 +687,10 @@ DELIMITER ; -- - modification_type_val: string value indicating the -- type of the change, i.e. "create", "update" or -- "delete". +-- Session variable disable_audit is used to disable +-- the procedure when wiping the database during +-- unit tests. This avoids issues with revision_id +-- being null. -- ---------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditEntryDHCP6; DELIMITER $$ @@ -676,10 +698,12 @@ CREATE PROCEDURE createAuditEntryDHCP6(IN object_type_val VARCHAR(256), IN object_id_val BIGINT(20) UNSIGNED, IN modification_type_val VARCHAR(32)) BEGIN - INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id) - VALUES (object_type_val, object_id_val, \ + IF @disable_audit IS NULL OR @disable_audit = 0 THEN + INSERT INTO dhcp6_audit (object_type, object_id, modification_type, revision_id) + VALUES (object_type_val, object_id_val, \ (SELECT id FROM modification WHERE modification_type = modification_type_val), \ @audit_revision_id); + END IF; END $$ DELIMITER ; diff --git a/src/share/database/scripts/mysql/wipe_data.sh.in b/src/share/database/scripts/mysql/wipe_data.sh.in new file mode 100644 index 0000000000..3b5901089b --- /dev/null +++ b/src/share/database/scripts/mysql/wipe_data.sh.in @@ -0,0 +1,90 @@ +# Copyright (C) 2019 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/. + +#!/bin/sh + +# This script is primarily used for MySQL unit tests, which need to +# ensure an empty, but schema correct database for each test. It +# deletes ALL transient data from an existing Kea MySQL schema, +# including leases, reservations, etc... Use at your own peril. +# Reference tables will be left in-tact. + +# 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 + +# First argument is must be the expected schema version . +exp_version="$1" +shift; + +# Remaining arguments are used as mysql command line arguments + +# If the existing schema doesn't match, the fail +VERSION=`mysql_version "$@"` +if [ "$VERSION" != "$exp_version" ]; then + printf "Reported version is $VERSION is wrong, expected $exp_version.\n" + exit 1 +fi + +# Delete transient data from tables. Per MySQL documentation TRUNCATE +# destroys and there recreates tables. As schema updates are typically +# very slow, we're use deletes here. We turn off foreign key checks to +# worrying about table order. We also set the session variable +# disable_audit to turn off Back audit procedures, to avoid attempting +# to create entries for deleted records. +mysql "$@" <