Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

[Load views] Fixed database entry duplication #5260

Merged
merged 8 commits into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions www/class/centreonCustomView.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ public function syncCustomView($customViewId, $userId = null)
public function loadCustomView($params)
{
$isLocked = 1;
$query = 'SELECT custom_view_id, locked ' .
$update = false;
$query = 'SELECT custom_view_id, locked, user_id ' .
'FROM custom_view_user_relation ' .
'WHERE custom_view_id = :viewLoad ' .
'AND ' .
Expand All @@ -616,14 +617,25 @@ public function loadCustomView($params)
if ($row['locked'] == "0") {
$isLocked = $row['locked'];
}
if (!is_null($row['user_id']) && $row['user_id'] > 0){
$update = true;
}
}

$query = 'INSERT INTO custom_view_user_relation (custom_view_id,user_id,is_owner,locked,is_share) ' .
'VALUES (:viewLoad, :userId, 0, :isLocked, 1)';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT);
$stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT);
$stmt->bindParam(':isLocked', $isLocked, PDO::PARAM_INT);
if ($update) {
$query = 'UPDATE custom_view_user_relation SET is_consumed=1 WHERE ' .
' custom_view_id = :viewLoad AND user_id = :userId';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT);
$stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT);
} else {
$query = 'INSERT INTO custom_view_user_relation (custom_view_id,user_id,is_owner,locked,is_share) ' .
'VALUES (:viewLoad, :userId, 0, :isLocked, 1)';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':viewLoad', $params['viewLoad'], PDO::PARAM_INT);
$stmt->bindParam(':userId', $this->userId, PDO::PARAM_INT);
$stmt->bindParam(':isLocked', $isLocked, PDO::PARAM_INT);
}
$dbResult = $stmt->execute();
if (!$dbResult) {
throw new \Exception("An error occured");
Expand Down
3 changes: 2 additions & 1 deletion www/install/createTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,8 @@ CREATE TABLE `custom_view_user_relation` (
`is_owner` tinyint(6) DEFAULT '0',
`is_share` tinyint(6) DEFAULT '0',
`is_consumed` int(1) NOT NULL DEFAULT 1,
UNIQUE KEY `view_user_unique_index` (`custom_view_id`,`user_id`,`usergroup_id`),
UNIQUE KEY `view_user_unique_index` (`custom_view_id`,`user_id`),
UNIQUE KEY `view_usergroup_unique_index` (`custom_view_id`,`usergroup_id`),
KEY `fk_custom_views_user_id` (`user_id`),
KEY `fk_custom_views_usergroup_id` (`usergroup_id`),
CONSTRAINT `fk_custom_views_user_id` FOREIGN KEY (`user_id`) REFERENCES `contact` (`contact_id`) ON DELETE CASCADE,
Expand Down
25 changes: 24 additions & 1 deletion www/install/sql/centreon/Update-DB-2.8.10_to_2.9.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,27 @@ WHERE `fieldname` = 'negociation';

UPDATE `cfg_centreonbroker_info`
SET `config_key` = 'negotiation'
WHERE `config_key` = 'negociation';
WHERE `config_key` = 'negociation';

-- Delete duplicate entries in custom_view_user_relation
ALTER TABLE `custom_view_user_relation`
DROP FOREIGN KEY `fk_custom_views_usergroup_id`,
DROP FOREIGN KEY `fk_custom_views_user_id`,
DROP FOREIGN KEY `fk_custom_view_user_id`,
DROP INDEX `view_user_unique_index`;
ALTER IGNORE TABLE `custom_view_user_relation`
ADD UNIQUE INDEX `view_user_unique_index` (`custom_view_id`, `user_id`),
ADD UNIQUE INDEX `view_usergroup_unique_index` (`custom_view_id`, `usergroup_id`);
ALTER TABLE `custom_view_user_relation`
ADD CONSTRAINT `fk_custom_views_usergroup_id`
FOREIGN KEY (`usergroup_id`)
REFERENCES `centreon`.`contactgroup` (`cg_id`)
ON DELETE CASCADE,
ADD CONSTRAINT `fk_custom_views_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `centreon`.`contact` (`contact_id`)
ON DELETE CASCADE,
ADD CONSTRAINT `fk_custom_view_user_id`
FOREIGN KEY (`custom_view_id`)
REFERENCES `centreon`.`custom_views` (`custom_view_id`)
ON DELETE CASCADE;