DB IDO: Use upsert and session token for comment/downtime updates

refs #12258
fixes #12288
This commit is contained in:
Michael Friedrich 2016-08-01 17:54:03 +02:00
parent 53930e321c
commit cd5c9366cf
7 changed files with 54 additions and 17 deletions

View File

@ -305,17 +305,6 @@ void DbEvents::AddComments(const Checkable::Ptr& checkable)
std::vector<DbQuery> queries; std::vector<DbQuery> queries;
/* Ensure to delete all comments and then insert any or none.
* We must purge obsolete comments in the database at all cost. */
DbQuery query1;
query1.Table = "comments";
query1.Type = DbQueryDelete;
query1.Category = DbCatComment;
query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("object_id", checkable);
queries.push_back(query1);
BOOST_FOREACH(const Comment::Ptr& comment, comments) { BOOST_FOREACH(const Comment::Ptr& comment, comments) {
AddCommentInternal(queries, comment, false); AddCommentInternal(queries, comment, false);
} }
@ -326,7 +315,6 @@ void DbEvents::AddComments(const Checkable::Ptr& checkable)
void DbEvents::AddComment(const Comment::Ptr& comment) void DbEvents::AddComment(const Comment::Ptr& comment)
{ {
std::vector<DbQuery> queries; std::vector<DbQuery> queries;
RemoveCommentInternal(queries, comment);
AddCommentInternal(queries, comment, false); AddCommentInternal(queries, comment, false);
DbObject::OnMultipleQueries(queries); DbObject::OnMultipleQueries(queries);
} }
@ -382,10 +370,19 @@ void DbEvents::AddCommentInternal(std::vector<DbQuery>& queries, const Comment::
DbQuery query1; DbQuery query1;
if (!historical) { if (!historical) {
query1.Table = "comments"; query1.Table = "comments";
query1.Type = DbQueryInsert | DbQueryUpdate;
fields1->Set("session_token", 0); /* DbConnection class fills in real ID */
query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("internal_comment_id", comment->GetLegacyId());
query1.WhereCriteria->Set("object_id", checkable);
query1.WhereCriteria->Set("comment_time", DbValue::FromTimestamp(entry_time));
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
} else { } else {
query1.Table = "commenthistory"; query1.Table = "commenthistory";
query1.Type = DbQueryInsert;
} }
query1.Type = DbQueryInsert;
query1.Category = DbCatComment; query1.Category = DbCatComment;
query1.Fields = fields1; query1.Fields = fields1;
@ -520,12 +517,22 @@ void DbEvents::AddDowntimeInternal(std::vector<DbQuery>& queries, const Downtime
DbQuery query1; DbQuery query1;
if (!historical) if (!historical) {
query1.Table = "scheduleddowntime"; query1.Table = "scheduleddowntime";
else query1.Type = DbQueryInsert | DbQueryUpdate;
query1.Table = "downtimehistory";
fields1->Set("session_token", 0); /* DbConnection class fills in real ID */
query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("object_id", checkable);
query1.WhereCriteria->Set("internal_downtime_id", downtime->GetLegacyId());
query1.WhereCriteria->Set("entry_time", DbValue::FromTimestamp(downtime->GetEntryTime()));
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
} else {
query1.Table = "downtimehistory";
query1.Type = DbQueryInsert;
}
query1.Type = DbQueryInsert;
query1.Category = DbCatDowntime; query1.Category = DbCatDowntime;
query1.Fields = fields1; query1.Fields = fields1;

View File

@ -442,6 +442,8 @@ void IdoMysqlConnection::ClearTablesBySession(void)
ClearTableBySession("hostgroup_members"); ClearTableBySession("hostgroup_members");
ClearTableBySession("servicegroup_members"); ClearTableBySession("servicegroup_members");
ClearTableBySession("contactgroup_members"); ClearTableBySession("contactgroup_members");
ClearTableBySession("comments");
ClearTableBySession("scheduleddowntime");
} }
void IdoMysqlConnection::ClearTableBySession(const String& table) void IdoMysqlConnection::ClearTableBySession(const String& table)

View File

@ -110,6 +110,7 @@ CREATE TABLE IF NOT EXISTS icinga_comments (
expires smallint default 0, expires smallint default 0,
expiration_time timestamp default '0000-00-00 00:00:00', expiration_time timestamp default '0000-00-00 00:00:00',
name TEXT character set latin1 default NULL, name TEXT character set latin1 default NULL,
session_token int default NULL,
PRIMARY KEY (comment_id), PRIMARY KEY (comment_id),
UNIQUE KEY instance_id (instance_id,object_id,comment_time,internal_comment_id) UNIQUE KEY instance_id (instance_id,object_id,comment_time,internal_comment_id)
) ENGINE=InnoDB COMMENT='Usercomments on Icinga objects'; ) ENGINE=InnoDB COMMENT='Usercomments on Icinga objects';
@ -1669,6 +1670,8 @@ CREATE INDEX idx_cg_session_del ON icinga_contactgroup_members (instance_id, ses
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token); CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token); CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);
CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
-- #12107 -- #12107
CREATE INDEX idx_statehistory_cleanup on icinga_statehistory(instance_id, state_time); CREATE INDEX idx_statehistory_cleanup on icinga_statehistory(instance_id, state_time);

View File

@ -78,6 +78,15 @@ DROP INDEX cvs_session_del_idx ON icinga_customvariablestatus;
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token); CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token); CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);
-- -----------------------------------------
-- #12258
-- -----------------------------------------
ALTER TABLE icinga_comments ADD COLUMN session_token INTEGER default NULL;
ALTER TABLE icinga_scheduleddowntime ADD COLUMN session_token INTEGER default NULL;
CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);
-- ----------------------------------------- -- -----------------------------------------
-- set dbversion -- set dbversion
-- ----------------------------------------- -- -----------------------------------------

View File

@ -413,6 +413,8 @@ void IdoPgsqlConnection::ClearTablesBySession(void)
ClearTableBySession("hostgroup_members"); ClearTableBySession("hostgroup_members");
ClearTableBySession("servicegroup_members"); ClearTableBySession("servicegroup_members");
ClearTableBySession("contactgroup_members"); ClearTableBySession("contactgroup_members");
ClearTableBySession("comments");
ClearTableBySession("scheduleddowntime");
} }
void IdoPgsqlConnection::ClearTableBySession(const String& table) void IdoPgsqlConnection::ClearTableBySession(const String& table)

View File

@ -136,6 +136,7 @@ CREATE TABLE icinga_comments (
expires INTEGER default 0, expires INTEGER default 0,
expiration_time timestamp with time zone default '1970-01-01 00:00:00+00', expiration_time timestamp with time zone default '1970-01-01 00:00:00+00',
name TEXT default NULL, name TEXT default NULL,
session_token INTEGER default NULL,
CONSTRAINT PK_comment_id PRIMARY KEY (comment_id) , CONSTRAINT PK_comment_id PRIMARY KEY (comment_id) ,
CONSTRAINT UQ_comments UNIQUE (instance_id,object_id,comment_time,internal_comment_id) CONSTRAINT UQ_comments UNIQUE (instance_id,object_id,comment_time,internal_comment_id)
) ; ) ;
@ -1005,6 +1006,7 @@ CREATE TABLE icinga_scheduleddowntime (
is_in_effect INTEGER default 0, is_in_effect INTEGER default 0,
trigger_time timestamp with time zone default '1970-01-01 00:00:00+00', trigger_time timestamp with time zone default '1970-01-01 00:00:00+00',
name TEXT default NULL, name TEXT default NULL,
session_token INTEGER default NULL,
CONSTRAINT PK_scheduleddowntime_id PRIMARY KEY (scheduleddowntime_id) , CONSTRAINT PK_scheduleddowntime_id PRIMARY KEY (scheduleddowntime_id) ,
CONSTRAINT UQ_scheduleddowntime UNIQUE (instance_id,object_id,entry_time,internal_downtime_id) CONSTRAINT UQ_scheduleddowntime UNIQUE (instance_id,object_id,entry_time,internal_downtime_id)
) ; ) ;
@ -1695,6 +1697,9 @@ CREATE INDEX idx_hg_session_del ON icinga_hostgroup_members (instance_id, sessio
CREATE INDEX idx_sg_session_del ON icinga_servicegroup_members (instance_id, session_token); CREATE INDEX idx_sg_session_del ON icinga_servicegroup_members (instance_id, session_token);
CREATE INDEX idx_cg_session_del ON icinga_contactgroup_members (instance_id, session_token); CREATE INDEX idx_cg_session_del ON icinga_contactgroup_members (instance_id, session_token);
CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token); CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token); CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);

View File

@ -56,6 +56,15 @@ DROP INDEX cvs_session_del_idx;
CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token); CREATE INDEX idx_cv_session_del ON icinga_customvariables (instance_id, session_token);
CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token); CREATE INDEX idx_cvs_session_del ON icinga_customvariablestatus (instance_id, session_token);
-- -----------------------------------------
-- #12258
-- -----------------------------------------
ALTER TABLE icinga_comments ADD COLUMN session_token INTEGER default NULL;
ALTER TABLE icinga_scheduleddowntime ADD COLUMN session_token INTEGER default NULL;
CREATE INDEX idx_comments_session_del ON icinga_comments (instance_id, session_token);
CREATE INDEX idx_downtimes_session_del ON icinga_scheduleddowntime (instance_id, session_token);
-- ----------------------------------------- -- -----------------------------------------
-- #12107 -- #12107
-- ----------------------------------------- -- -----------------------------------------