From: Marcin Siodelski Date: Fri, 25 Jan 2019 08:11:04 +0000 (+0100) Subject: [#396,!205] Cleanup in the database creation script. X-Git-Tag: 429-Updated-StampedValue-to-support-reals_base~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c709b42fd037066da34119b4d2503c2ec0fa4623;p=thirdparty%2Fkea.git [#396,!205] Cleanup in the database creation script. --- diff --git a/src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc b/src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc index 75ab68c22a..79f2c0ddf6 100644 --- a/src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc +++ b/src/hooks/dhcp/mysql_cb/mysql_cb_dhcp4.cc @@ -52,7 +52,7 @@ public: /// reading the database are less than those of statements modifying the /// database. enum StatementIndex { - INIT_AUDIT_REVISION, + CREATE_AUDIT_REVISION, GET_GLOBAL_PARAMETER4, GET_ALL_GLOBAL_PARAMETERS4, GET_MODIFIED_GLOBAL_PARAMETERS4, @@ -216,13 +216,11 @@ public: MySqlTransaction transaction(conn_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "global parameter set", false); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "global parameter set", false); // Try to update the existing row. if (conn_.updateDeleteQuery(MySqlConfigBackendDHCPv4Impl::UPDATE_GLOBAL_PARAMETER4, @@ -753,12 +751,10 @@ public: MySqlTransaction transaction(conn_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, + MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, "subnet set", true); try { @@ -870,13 +866,11 @@ public: MySqlTransaction transaction(conn_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - log_message, cascade_delete); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + log_message, cascade_delete); auto count = deleteFromTable(index, server_selector, operation, keys...); @@ -1164,13 +1158,11 @@ public: MySqlTransaction transaction(conn_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "shared network set", true); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "shared network set", true); try { @@ -1279,13 +1271,11 @@ public: option->option_->getType(), option->space_name_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "global option set", false); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "global option set", false); if (existing_option) { in_bindings.push_back(MySqlBinding::createString(tag)); @@ -1350,13 +1340,11 @@ public: option->option_->getType(), option->space_name_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "subnet specific option set", cascade_update); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "subnet specific option set", cascade_update); if (existing_option) { in_bindings.push_back(MySqlBinding::createString(tag)); @@ -1439,13 +1427,11 @@ public: option->option_->getType(), option->space_name_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "pool specific option set", cascade_update); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "pool specific option set", cascade_update); if (existing_option) { in_bindings.push_back(MySqlBinding::createString(tag)); @@ -1510,14 +1496,12 @@ public: option->option_->getType(), option->space_name_); - // Create scoped audit revision. It initiates session variables in the - // database to be used for creating new audit revision. As long as this - // instance exists no new audit revisions are created as a result of - // any subsequent calls. - ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "shared network specific option set", - cascade_update); + // Create scoped audit revision. As long as this instance exists + // no new audit revisions are created in any subsequent calls. + ScopedAuditRevision + audit_revision(this, MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "shared network specific option set", + cascade_update); if (existing_option) { in_bindings.push_back(MySqlBinding::createString(tag)); @@ -1840,7 +1824,7 @@ public: // instance exists no new audit revisions are created as a result of // any subsequent calls. ScopedAuditRevision audit_revision(this, - MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, + MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, "option definition set", true); @@ -2055,8 +2039,8 @@ TaggedStatementArray; /// @brief Prepared MySQL statements used by the backend to insert and /// retrieve data from the database. TaggedStatementArray tagged_statements = { { - { MySqlConfigBackendDHCPv4Impl::INIT_AUDIT_REVISION, - "CALL initAuditRevision(?, ?)" + { MySqlConfigBackendDHCPv4Impl::CREATE_AUDIT_REVISION, + "CALL createAuditRevisionDHCP4(?, ?, ?)" }, // Select global parameter by name. diff --git a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc index 9b5f6ce78d..66fe2187d3 100644 --- a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc +++ b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc @@ -91,6 +91,7 @@ MySqlConfigBackendImpl::initAuditRevision(const int index, } MySqlBindingCollection in_bindings = { + MySqlBinding::createString("all"), MySqlBinding::createString(log_message), MySqlBinding::createInteger(static_cast(cascade_transaction)) }; diff --git a/src/share/database/scripts/mysql/dhcpdb_create.mysql b/src/share/database/scripts/mysql/dhcpdb_create.mysql index a4d263e680..fa1f5d40b5 100644 --- a/src/share/database/scripts/mysql/dhcpdb_create.mysql +++ b/src/share/database/scripts/mysql/dhcpdb_create.mysql @@ -1341,6 +1341,7 @@ CREATE TABLE IF NOT EXISTS dhcp4_audit_revision ( id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, modification_ts TIMESTAMP NOT NULL, log_message TEXT, + server_id BIGINT(10) UNSIGNED, PRIMARY KEY (id), KEY key_dhcp4_audit_revision_by_modification_ts (modification_ts) ) ENGINE=InnoDB; @@ -1365,41 +1366,44 @@ ALTER TABLE dhcp4_audit REFERENCES dhcp4_audit_revision (id) ON DELETE NO ACTION ON UPDATE CASCADE; --- ----------------------------------------------------- --- Stored procedure which initializes the session --- variables for creation of the new audit revision. --- ----------------------------------------------------- -DROP PROCEDURE IF EXISTS initAuditRevision; -DELIMITER $$ -CREATE PROCEDURE initAuditRevision(IN log_message TEXT, - IN cascade_transaction TINYINT(1)) -BEGIN - SET @audit_log_message = log_message; - SET @cascade_transaction = cascade_transaction; - SET @audit_revision_id = NULL; -END $$ -DELIMITER ; - -- ----------------------------------------------------- -- Stored procedure which creates a new entry in the --- dhcp4_audit_revision table. This procedure should --- be called from the triggers of the tables where --- the config modifications are applied. If the --- corresponding revision doesn't exist, it will be --- created by this procedure. +-- dhcp4_audit_revision table and sets appropriate session +-- variables to be used while creating the audit entries +-- by triggers. This procedure should be called at the +-- beginning of a transaction which modifies configuration +-- data in the database, e.g. when new subnet is added. +-- +-- Parameters: +-- - server_tag is used to retrieve the server_id which +-- associates the changes applied with the particular +-- server or all servers. +-- - audit_log_message is a log message associates with +-- the audit revision. +-- - cascade_transaction is assigned to a session +-- variable which is used in some triggers to determine +-- if the audit entry should be created for them or +-- not. Specifically, this is used when DHCP options +-- are inserted, updated or deleted. If such modification +-- is a part of the larger change (e.g. change in the +-- subnet the options belong to) the dedicated audit +-- entry for options must not be created. On the other +-- 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. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditRevisionDHCP4; DELIMITER $$ -CREATE PROCEDURE createAuditRevisionDHCP4() +CREATE PROCEDURE createAuditRevisionDHCP4(IN server_tag VARCHAR(256), + IN audit_log_message TEXT, + IN cascade_transaction TINYINT(1)) BEGIN - DECLARE local_audit_log_message TEXT; - IF @audit_revision_id IS NULL THEN - SET local_audit_log_message = @audit_log_message; - SET @audit_log_message = NULL; - INSERT INTO dhcp4_audit_revision (modification_ts, log_message) - VALUES (NOW(), local_audit_log_message); - SET @audit_revision_id = LAST_INSERT_ID(); - END IF; + 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 (NOW(), srv_id, audit_log_message); + SET @audit_revision_id = LAST_INSERT_ID(); + SET @cascade_transaction = cascade_transaction; END $$ DELIMITER ; @@ -1409,16 +1413,26 @@ DELIMITER ; -- triggers of the tables where the config modifications -- are applied. The @audit_revision_id variable contains -- the revision id to be placed in the audit entries. +-- +-- The following parameters are passed to this procedure: +-- - object_type_val: name of the table to be associated +-- with the applied changes. +-- - object_id_val: identifier of the modified object in +-- that table. +-- - modification_type_val: string value indicating the +-- type of the change, i.e. "create", "update" or +-- "delete". -- ---------------------------------------------------- DROP PROCEDURE IF EXISTS createAuditEntryDHCP4; DELIMITER $$ CREATE PROCEDURE createAuditEntryDHCP4(IN object_type_val VARCHAR(256), IN object_id_val BIGINT(20) UNSIGNED, - IN modification_type_val TINYINT(1)) + IN modification_type_val VARCHAR(32)) BEGIN - CALL createAuditRevisionDHCP4(); INSERT INTO dhcp4_audit (object_type, object_id, modification_type, revision_id) - VALUES (object_type_val, object_id_val, modification_type_val, @audit_revision_id); + VALUES (object_type_val, object_id_val, \ + (SELECT id FROM modification WHERE modification_type = modification_type_val), \ + @audit_revision_id); END $$ DELIMITER ; @@ -1433,7 +1447,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_global_parameter_AINS AFTER INSERT ON dhcp4_global_parameter FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_global_parameter', NEW.id, 0); + CALL createAuditEntryDHCP4('dhcp4_global_parameter', NEW.id, "create"); END $$ DELIMITER ; @@ -1442,7 +1456,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_global_parameter_AUPD AFTER UPDATE ON dhcp4_global_parameter FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_global_parameter', NEW.id, 1); + CALL createAuditEntryDHCP4('dhcp4_global_parameter', NEW.id, "update"); END $$ DELIMITER ; @@ -1451,7 +1465,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_global_parameter_ADEL AFTER DELETE ON dhcp4_global_parameter FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_global_parameter', OLD.id, 2); + CALL createAuditEntryDHCP4('dhcp4_global_parameter', OLD.id, "delete"); END $$ DELIMITER ; @@ -1460,7 +1474,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_subnet_AINS AFTER INSERT ON dhcp4_subnet FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_subnet', NEW.subnet_id, 0); + CALL createAuditEntryDHCP4('dhcp4_subnet', NEW.subnet_id, "create"); END $$ DELIMITER ; @@ -1469,7 +1483,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_subnet_AUPD AFTER UPDATE ON dhcp4_subnet FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_subnet', NEW.subnet_id, 1); + CALL createAuditEntryDHCP4('dhcp4_subnet', NEW.subnet_id, "update"); END $$ DELIMITER ; @@ -1478,7 +1492,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_subnet_ADEL AFTER DELETE ON dhcp4_subnet FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_subnet', OLD.subnet_id, 2); + CALL createAuditEntryDHCP4('dhcp4_subnet', OLD.subnet_id, "delete"); END $$ DELIMITER ; @@ -1487,7 +1501,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_shared_network_AINS AFTER INSERT ON dhcp4_shared_network FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_shared_network', NEW.id, 0); + CALL createAuditEntryDHCP4('dhcp4_shared_network', NEW.id, "create"); END $$ DELIMITER ; @@ -1496,7 +1510,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_shared_network_AUPD AFTER UPDATE ON dhcp4_shared_network FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_shared_network', NEW.id, 1); + CALL createAuditEntryDHCP4('dhcp4_shared_network', NEW.id, "update"); END $$ DELIMITER ; @@ -1505,7 +1519,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_shared_network_ADEL AFTER DELETE ON dhcp4_shared_network FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_shared_network', OLD.id, 2); + CALL createAuditEntryDHCP4('dhcp4_shared_network', OLD.id, "delete"); END $$ DELIMITER ; @@ -1514,7 +1528,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_option_def_AINS AFTER INSERT ON dhcp4_option_def FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_option_def', NEW.id, 0); + CALL createAuditEntryDHCP4('dhcp4_option_def', NEW.id, "create"); END $$ DELIMITER ; @@ -1523,7 +1537,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_option_def_AUPD AFTER UPDATE ON dhcp4_option_def FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_option_def', NEW.id, 1); + CALL createAuditEntryDHCP4('dhcp4_option_def', NEW.id, "update"); END $$ DELIMITER ; @@ -1532,7 +1546,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_option_def_ADEL AFTER DELETE ON dhcp4_option_def FOR EACH ROW BEGIN - CALL createAuditEntryDHCP4('dhcp4_option_def', OLD.id, 2); + CALL createAuditEntryDHCP4('dhcp4_option_def', OLD.id, "delete"); END $$ DELIMITER ; @@ -1552,7 +1566,7 @@ DELIMITER ; -- created as part of the current transaction. -- -- The following parameters are passed to the procedure: --- - modification_type: CREATE, UPDATE or DELETE +-- - modification_type: "create", "update" or "delete" -- - scope_id: identifier of the option scope, e.g. -- global, subnet specific etc. -- - option_id: identifier of the option. @@ -1562,10 +1576,12 @@ DELIMITER ; -- - belongs to the host. -- - network_name: shared network name if the option -- belongs to the shared network. +-- - pool_id: identifier of the pool if the option +-- belongs to the pool. -- ----------------------------------------------------- DROP PROCEDURE IF EXISTS createOptionAuditDHCP4; DELIMITER $$ -CREATE PROCEDURE createOptionAuditDHCP4(IN modification_type TINYINT(1), +CREATE PROCEDURE createOptionAuditDHCP4(IN modification_type VARCHAR(32), IN scope_id TINYINT(3) UNSIGNED, IN option_id BIGINT(20) UNSIGNED, IN subnet_id INT(10) UNSIGNED, @@ -1599,24 +1615,24 @@ BEGIN # If subnet specific option is added or modified, create # audit entry for the entire subnet, which indicates that # it should be treated as the subnet update. - CALL createAuditEntryDHCP4('dhcp4_subnet', subnet_id, 1); + CALL createAuditEntryDHCP4('dhcp4_subnet', subnet_id, "update"); ELSEIF scope_id = 3 THEN # If host specific option is added or modified, create # audit entry for the host, which indicates that it # should be treated as the host update. - CALL createAuditEntryDHCP4('hosts', host_id, 1); + CALL createAuditEntryDHCP4('hosts', host_id, "update"); ELSEIF scope_id = 4 THEN # If shared network specific option is added or modified, # create audit entry for the shared network which # indicates that it should be treated as the shared # network update. SELECT id INTO snid FROM dhcp4_shared_network WHERE name = network_name LIMIT 1; - CALL createAuditEntryDHCP4('dhcp4_shared_network', snid, 1); + CALL createAuditEntryDHCP4('dhcp4_shared_network', snid, "update"); ELSEIF scope_id = 5 THEN # If pool specific option is added or modified, create # audit entry for the subnet which this pool belongs to. SELECT dhcp4_pool.subnet_id INTO sid FROM dhcp4_pool WHERE id = pool_id; - CALL createAuditEntryDHCP4('dhcp4_subnet', sid, 1); + CALL createAuditEntryDHCP4('dhcp4_subnet', sid, "update"); END IF; END IF; END $$ @@ -1627,7 +1643,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_options_AINS AFTER INSERT ON dhcp4_options FOR EACH ROW BEGIN - CALL createOptionAuditDHCP4(0, NEW.scope_id, NEW.option_id, NEW.dhcp4_subnet_id, + CALL createOptionAuditDHCP4("create", NEW.scope_id, NEW.option_id, NEW.dhcp4_subnet_id, NEW.host_id, NEW.shared_network_name, NEW.pool_id); END $$ DELIMITER ; @@ -1637,7 +1653,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_options_AUPD AFTER UPDATE ON dhcp4_options FOR EACH ROW BEGIN - CALL createOptionAuditDHCP4(1, NEW.scope_id, NEW.option_id, NEW.dhcp4_subnet_id, + CALL createOptionAuditDHCP4("update", NEW.scope_id, NEW.option_id, NEW.dhcp4_subnet_id, NEW.host_id, NEW.shared_network_name, NEW.pool_id); END $$ DELIMITER ; @@ -1647,7 +1663,7 @@ DELIMITER $$ CREATE TRIGGER dhcp4_options_ADEL AFTER DELETE ON dhcp4_options FOR EACH ROW BEGIN - CALL createOptionAuditDHCP4(2, OLD.scope_id, OLD.option_id, OLD.dhcp4_subnet_id, + CALL createOptionAuditDHCP4("delete", OLD.scope_id, OLD.option_id, OLD.dhcp4_subnet_id, OLD.host_id, OLD.shared_network_name, OLD.pool_id); END $$ DELIMITER ; diff --git a/src/share/database/scripts/mysql/dhcpdb_drop.mysql b/src/share/database/scripts/mysql/dhcpdb_drop.mysql index 2a41667d9c..7fcb8734ee 100644 --- a/src/share/database/scripts/mysql/dhcpdb_drop.mysql +++ b/src/share/database/scripts/mysql/dhcpdb_drop.mysql @@ -59,7 +59,6 @@ DROP TABLE IF EXISTS dhcp6_shared_network_server; DROP TABLE IF EXISTS dhcp6_subnet; DROP TABLE IF EXISTS dhcp6_subnet_server; DROP TABLE IF EXISTS modification; -DROP PROCEDURE IF EXISTS initAuditRevision; DROP PROCEDURE IF EXISTS createAuditRevisionDHCP4; DROP PROCEDURE IF EXISTS createAuditEntryDHCP4; DROP TRIGGER IF EXISTS dhcp4_global_parameter_AINS;