• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Why aren't add-on tables removed with the add-on?

CTXMedia

Formerly CyclingTribe
#1
I've just noticed a number of tables in my XF database that belong to add-ons I've long since removed.

Why aren't the tables removed with the add-on?

Cheers,
Shaun :D
 

Jake Bunce

XenForo moderator
Staff member
#5
Correct. Addons are responsible for adding and removing their own custom tables and fields in the install / uninstall routines. For example, here is the install / uninstall code for Nodes As Tabs:

Code:
<?php

class NodesAsTabs_Install
{
	public static function install()
	{
		$db = XenForo_Application::get('db');

		$db->query("
			CREATE TABLE IF NOT EXISTS `nat_options` (
				`node_id` int(10) unsigned NOT NULL,
				`nat_display_tab` tinyint(3) unsigned NOT NULL,
				`nat_display_tabperms` tinyint(3) unsigned NOT NULL,
				`nat_tabtitle` varchar(50) NOT NULL,
				`nat_display_order` int(10) unsigned NOT NULL,
				`nat_position` enum('home','middle','end') NOT NULL,
				`nat_childlinks` tinyint(3) unsigned NOT NULL,
				`nat_childlinksperms` tinyint(3) unsigned NOT NULL,
				`nat_markread` tinyint(3) unsigned NOT NULL,
				`nat_linkstemplate` varchar(50) NOT NULL,
				`nat_popup` tinyint(3) unsigned NOT NULL,
				`nat_tabid` varchar(50) NOT NULL,
				`nat_childnodes` text NOT NULL,
				`nat_firstchildnodes` mediumblob NOT NULL,
				UNIQUE KEY `node_id` (`node_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='NodesAsTabs addon'
		");

		// EARLY VERSIONS DIDN'T HAVE THESE COLUMNS
		self::addColumnIfNotExists('nat_markread', 'tinyint(3) unsigned NOT NULL', 'nat_childlinksperms');
		self::addColumnIfNotExists('nat_popup', 'tinyint(3) unsigned NOT NULL', 'nat_linkstemplate');
		self::addColumnIfNotExists('nat_tabtitle', 'varchar(50) NOT NULL', 'nat_display_tabperms');
		self::addColumnIfNotExists('nat_tabid', 'varchar(50) NOT NULL', 'nat_popup');

		$optionsModel = XenForo_Model::create('NodesAsTabs_Model_Options');

		$optionsModel->deleteOrphans();
		$optionsModel->rebuildCache();
	}

	public static function addColumnIfNotExists($fieldName, $fieldDef, $after)
	{
		$db = XenForo_Application::get('db');

		$exists = $db->fetchRow("
			SHOW COLUMNS
			FROM nat_options
			WHERE Field = ?
		", $fieldName);

		if (!$exists)
		{
			$db->query("
				ALTER TABLE nat_options ADD {$fieldName} {$fieldDef} AFTER {$after}
			");
		}
	}

	public static function uninstall()
	{
		$db = XenForo_Application::get('db');

		$db->query("
			DROP TABLE IF EXISTS `nat_options`
		");

		XenForo_Application::setSimpleCacheData('natCache', false);
	}
}
On the other hand, XenForo's addon system has many integrated data types that are automatically removed by the system including templates, phrases, nav links, listeners, etc. The system is very tight. Addons only need to worry about managing their custom data types like new tables.