Skip to content

Error‐Resolving orphaned Forums

johnhenley edited this page Jun 28, 2024 · 4 revisions

If you have a very old version of DNN Community Forums (perhaps from when it was called Active Forums), you might run into some database integrity issues. You likely will never run into these issues if you started with DNN Community Forums with version 7.7 or later, because we dramatically improved database referential integrity. But if you started with an older version, you possibly could have some orphaned data. In particular, I ran into it from Forums that were created back in 2008--but didn't even notice until testing the new 8.1 release candidate. Why it's just started to become an issue is because the Forums module used to retrieve most data using SQL stored procedures, and you can use COALESCE, etc. to handle missing related data. But as we move DNN Community Forums to use the DNN DAL2 APIs, null reference exceptions can occur when database integrity is an issue. So far, I've found two cases where it happened:

  1. Missing Forum Group for Forum
  2. Missing Permissions for Forum or Forum Group

We've added a little bit of protection into the DNN Community Forums so that -- if you ever do encounter this, rather than an ugly crash, you will get some alerts posted to the DNN Admin Log: image

You can use the script below to detect and repair issues with missing Forum Group for a Forum.

/* // // Community Forums // Copyright (c) 2013-2024 // by DNN Community // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the "Software"), to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and // to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions // of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // */

/*

// This script can be used to detect Forums with missing Forum Groups. // If you uncomment the bottom portion, it will create a new Forum Group for the orphaned Forums. // You will need to adjust this script if you use a schema other than [dbo], and/or if you use an object_qualifier.

*/

/* Run this command to see if you have orphaned forum groups */

SELECT f.ModuleId, f.ForumId, f.ForumName, f.ForumGroupId FROM [dbo].[activeforums_Forums] f LEFT OUTER JOIN [dbo].[activeforums_Groups] g ON g.ForumGroupId = f.ForumGroupId WHERE g.ForumGroupId IS NULL;

/* Only remove the comment below and the line at the end of this script when you are comfortable with what is being performed. If you are in doubt, please post an issue on https://github.com/DNNCommunity/Dnn.CommunityForums for more guidance */

/*

INSERT INTO [dbo].[activeforums_Permissions] ([CanView] ,[CanRead] ,[CanCreate] ,[CanReply] ,[CanEdit] ,[CanDelete] ,[CanLock] ,[CanPin] ,[CanAttach] ,[CanPoll] ,[CanBlock] ,[CanTrust] ,[CanSubscribe] ,[CanAnnounce] ,[CanModApprove] ,[CanModMove] ,[CanModSplit] ,[CanModDelete] ,[CanModUser] ,[CanModEdit] ,[CanModLock] ,[CanModPin] ,[CanTag] ,[CanCategorize] ,[CanPrioritize]) VALUES ('||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||', '||||');

DECLARE @PermissionId int = SCOPE_IDENTITY();

INSERT INTO [dbo].[activeforums_Groups] SELECT DISTINCT f.ModuleId, 'Orphaned Forums', 0, 0, 0, '', @PermissionId, 'Orphaned' FROM [dbo].[activeforums_Forums] f LEFT OUTER JOIN [dbo].[activeforums_Groups] g ON g.ForumGroupId = f.ForumGroupId AND g.ForumGroupId IS NULL;

UPDATE f SET ForumGroupId = g2.ForumGroupId FROM [dbo].[activeforums_Forums] f LEFT OUTER JOIN [dbo].[activeforums_Groups] g ON g.ForumGroupId = f.ForumGroupId INNER JOIN [dbo].[activeforums_Groups] g2 ON g2.ModuleId = f.ModuleId AND g2.PrefixURL = 'Orphaned' WHERE g.ForumGroupId IS NULL

UPDATE [dbo].[activeforums_Groups] SET GroupSettingsKey = 'G:'+ CAST(ForumGroupId as varchar(50)), PrefixURL = '' WHERE PrefixURL = 'Orphaned'

//this should now be empty SELECT f.ModuleId, f.ForumId, f.ForumName, f.ForumGroupId FROM [dbo].[activeforums_Forums] f LEFT OUTER JOIN [dbo].[activeforums_Groups] g ON g.ForumGroupId = f.ForumGroupId WHERE g.ForumGroupId IS NULL;

*/

After you run the script, you will see the orphaned forums: image

Clone this wiki locally