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

Commit

Permalink
[Load views] Fixed database entry duplication (#5260)
Browse files Browse the repository at this point in the history
* Fixed database entry duplication

* Fix coding style

* Fix "unique" field in table "custom_view_user_relation"

* Update Update-DB-2.8.8_to_2.9.0.sql

* Update and rename Update-DB-2.8.8_to_2.9.0.sql to Update-DB-2.8.10_to_2.9.0.sql
  • Loading branch information
Jean-Baptiste authored and kduret committed Jul 12, 2017
1 parent e46916f commit ff3db10
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
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;

0 comments on commit ff3db10

Please sign in to comment.