Commit 4f083581 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Gerrit Code Review

Merge changes from topic 'Triggers'

* changes:
  Bug 1785985: Remove quota triggers
  Bug 1785985: Remove notification triggers
  Bug 1785985: Remove Elasticsearch triggers
parents 55d0e8c9 8e7a3135
......@@ -126,39 +126,13 @@ class PluginArtefactFile extends PluginArtefact {
public static function set_quota_triggers() {
set_config_plugin('artefact', 'file', 'quotanotifylimit', 80);
set_config_plugin('artefact', 'file', 'quotanotifyadmin', false);
// Create triggers to reset the quota notification flag
if (is_postgres()) {
$sql = "DROP FUNCTION IF EXISTS {unmark_quota_exeed_upd_set}() CASCADE;";
execute_sql($sql);
db_create_trigger(
'unmark_quota_exceed_upd_usr_set',
'AFTER', 'UPDATE', 'usr', "
UPDATE {usr_account_preference}
SET value = 0 FROM {artefact_config}
WHERE {usr_account_preference}.field = 'quota_exceeded_notified'
AND {usr_account_preference}.usr = NEW.id
AND {artefact_config}.plugin = 'file'
AND {artefact_config}.field = 'quotanotifylimit'
AND CAST(NEW.quotaused AS float)/CAST(NEW.quota AS float) < CAST({artefact_config}.value AS float)/100;"
);
}
else {
$sql = "DROP TRIGGER IF EXISTS {unmark_quota_exceed_upd_set}";
execute_sql($sql);
db_create_trigger(
'unmark_quota_exceed_upd_usr_set',
'AFTER', 'UPDATE', 'usr', "
UPDATE {usr_account_preference}, {artefact_config}
SET {usr_account_preference}.value = 0
WHERE {usr_account_preference}.field = 'quota_exceeded_notified'
AND {usr_account_preference}.usr = NEW.id
AND {artefact_config}.plugin = 'file'
AND {artefact_config}.field = 'quotanotifylimit'
AND NEW.quotaused/NEW.quota < {artefact_config}.value/100;"
);
}
}
......
......@@ -1376,9 +1376,7 @@ function xmldb_core_upgrade($oldversion=0) {
WHERE id IN (
SELECT u.id FROM {usr} u
JOIN {auth_instance} ui ON ui.id = u.authinstance
WHERE ui.authname != 'internal' AND ui.active = 1
)
AND id != 0"); // Ignore the root user
WHERE ui.authname != 'internal' AND ui.active = 1 AND u.id != 0)"); // Ignore the root user
}
}
......@@ -1485,5 +1483,22 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2019093000) {
$searches = plugins_installed('search');
if (isset($searches['elasticsearch'])) {
log_debug('Remove triggers for elasticsearch');
safe_require('search', 'elasticsearch');
ElasticSearchIndexing::drop_trigger_functions();
}
log_debug('Remove triggers for notifications');
db_drop_trigger('update_unread_insert', 'notification_internal_activity');
db_drop_trigger('update_unread_update', 'notification_internal_activity');
db_drop_trigger('update_unread_delete', 'notification_internal_activity');
db_drop_trigger('update_unread_insert2', 'module_multirecipient_userrelation');
db_drop_trigger('update_unread_update2', 'module_multirecipient_userrelation');
db_drop_trigger('update_unread_delete2', 'module_multirecipient_userrelation');
db_drop_trigger('unmark_quota_exceed_upd_usr_set', 'usr');
}
return $status;
}
This diff is collapsed.
......@@ -1825,6 +1825,31 @@ function generate_class_name() {
return 'Plugin' . implode('', array_map('ucfirst', $args));
}
function generate_class_name_from_table() {
$tableargs = func_get_args();
$args = generate_plugin_type_from_table($tableargs[0]);
return 'Plugin' . implode('', array_map('ucfirst', $args));
}
function generate_plugin_type_from_table() {
$tableargs = func_get_args();
$table = $tableargs[0];
if ($prefix = get_config('dbprefix')) {
$table = preg_replace('/' . $prefix . '/', '', $table);
}
$tableargs = explode('_', $table);
if (count($tableargs) > 2) {
$args = array_slice($tableargs, 0, 2);
// Fix for multirecipientnotifications where table name doesn't follow convention
if (isset($args[1]) && $args[1] == 'multirecipient') {
$args[1] = 'multirecipientnotification';
}
}
return array((isset($args[0]) ? $args[0] : null), (isset($args[1]) ? $args[1] : null));
}
function generate_artefact_class_name($type) {
return 'ArtefactType' . ucfirst($type);
}
......
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2019090901;
$config->version = 2019093000;
$config->series = '19.10';
$config->release = '19.10dev';
$config->minupgradefrom = 2017031605;
......
......@@ -119,34 +119,27 @@ class PluginModuleMultirecipientnotification extends PluginModule {
return $tabs;
}
/**
* The pseudo trigger function that should work like how triggers worked before
* But instead of things happening automatically at db level
* we call the command at the dml.php level to have some control over it
* @param string $id The id of the user to update
* @param string $savetype Whether we are doing an insert / update / or delete
* - Note: in this instance of the pseudo_trigger() we don't care about the $savetype
* as we can work out the current state via an SQL query
*/
public static function pseudo_trigger($id, $savetype = 'insert') {
$usr = get_field('module_multirecipient_userrelation', 'usr', 'id', $id);
execute_sql("UPDATE {usr} SET unread = (
SELECT SUM(counts) FROM (
SELECT COUNT(*) AS counts FROM {module_multirecipient_userrelation} WHERE \"role\" = 'recipient' AND \"read\" = ? AND usr = ?
UNION
SELECT COUNT(*) AS counts FROM {notification_internal_activity} WHERE \"read\" = ? AND usr = ?
) AS countsum
) WHERE id = ?", array('0', $usr, 0, $usr, $usr), false);
}
public static function postinst($prevversion) {
if ($prevversion < 20131010) {
// Add triggers to update user unread message count when updating
// module_multirecipient_userrelation
db_create_trigger(
'update_unread_insert2',
'AFTER', 'INSERT', 'module_multirecipient_userrelation', '
IF NEW.role = \'recipient\' AND NEW.read = \'0\' THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_update2',
'AFTER', 'UPDATE', 'module_multirecipient_userrelation', '
IF OLD.read = \'0\' AND NEW.read = \'1\' AND NEW.role = \'recipient\' THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = NEW.usr;
ELSEIF OLD.read = \'1\' AND NEW.read = \'0\' AND NEW.role = \'recipient\' THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_delete2',
'AFTER', 'DELETE', 'module_multirecipient_userrelation', '
IF OLD.read = \'0\' AND OLD.role = \'recipient\' THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = OLD.usr;
END IF;'
);
}
}
/**
......
......@@ -66,34 +66,27 @@ class PluginNotificationInternal extends PluginNotification {
return $messageid;
}
/**
* The pseudo trigger function that should work like how triggers worked before
* But instead of things happening automatically at db level
* we call the command at the dml.php level to have some control over it
* @param string $id The id of the user to update
* @param string $savetype Whether we are doing an insert / update / or delete
* - Note: in this instance of the pseudo_trigger() we don't care about the $savetype
* as we can work out the current state via an SQL query
*/
public static function pseudo_trigger($id, $savetype = 'insert') {
$usr = get_field('notification_internal_activity', 'usr', 'id', $id);
execute_sql("UPDATE {usr} SET unread = (
SELECT SUM(counts) FROM (
SELECT COUNT(*) AS counts FROM {module_multirecipient_userrelation} WHERE \"role\" = 'recipient' AND \"read\" = ? AND usr = ?
UNION
SELECT COUNT(*) AS counts FROM {notification_internal_activity} WHERE \"read\" = ? AND usr = ?
) AS countsum
) WHERE id = ?", array(0, $usr, 0, $usr, $usr), false);
}
public static function postinst($prevversion) {
if ($prevversion == 0) {
// Add triggers to update user unread message count when updating
// notification_internal_activity
db_create_trigger(
'update_unread_insert',
'AFTER', 'INSERT', 'notification_internal_activity', '
IF NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_update',
'AFTER', 'UPDATE', 'notification_internal_activity', '
IF OLD.read = 0 AND NEW.read = 1 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = NEW.usr;
ELSEIF OLD.read = 1 AND NEW.read = 0 THEN
UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
END IF;'
);
db_create_trigger(
'update_unread_delete',
'AFTER', 'DELETE', 'notification_internal_activity', '
IF OLD.read = 0 THEN
UPDATE {usr} SET unread = unread - 1 WHERE id = OLD.usr;
END IF;'
);
}
}
public static function get_event_subscriptions() {
......
This diff is collapsed.
......@@ -248,6 +248,24 @@ class BehatAdmin extends BehatBase {
),
),
),
'search' => array (
'elasticsearch' => array(
'indexname' => array(),
'types' => array(
'usr',
'interaction_instance',
'interaction_forum_post',
'group',
'view',
'artefact',
'block_instance',
'collection',
),
'cronlimit' => array(),
'shards' => array(),
'replicashards' => array(),
),
),
);
// if artefact internal profilemandatory is set we need to make sure that firstname/lastname/email are included.
if (!empty($settings['artefact']['internal']['profilemandatory'])) {
......@@ -263,6 +281,12 @@ class BehatAdmin extends BehatBase {
$values = array_merge($mandatory, $values);
$settings['artefact']['internal']['profilepublic'] = implode(',', $values);
}
// if search elasticsearch types set we need to make sure to use only valid ones
if (!empty($settings['search']['elasticsearch']['types'])) {
$values = explode(',', $settings['search']['elasticsearch']['types']);
$values = array_intersect($values, $allowsettings['search']['elasticsearch']['types']);
$settings['search']['elasticsearch']['types'] = implode(',', $values);
}
// Update plugin settings
foreach ($allowsettings as $plugintype => $plugins) {
......
......@@ -13,20 +13,20 @@ Background:
| search | elasticsearch | shards | 5 |
| search | elasticsearch | replicashards | 0 |
And the following site settings are set:
And the following site settings are set:
| field | value |
| searchplugin | elasticsearch |
And the following "users" exist:
And the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| UserA | Kupuh1pa! | UserA@example.org | Angela | User | mahara | internal | member |
| UserB | Kupuh1pa! | UserB@example.org | Bob | User | mahara | internal | member |
And the following "pages" exist:
And the following "pages" exist:
| title | description | ownertype | ownername |
| Page UserA_01 | Page 01 | user | UserA |
And the following "permissions" exist:
And the following "permissions" exist:
| title | accesstype | accessname |
| Page UserA_01 | user | admin |
......@@ -42,7 +42,8 @@ Scenario: Testing functions for user search page (Bug 1431569)
Then I should see "Angela"
And I should see "Page UserA_01"
# set system off elasticsearch
Then the following site settings are set:
| field | value |
| searchplugin | internal |
And I choose "Configure site" from administration menu
And I expand the section "Search settings"
And I select "internal" from "Search plugin"
And I press "Update site options"
And I log out
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment