Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting Sections may not clean up all content in multi-site installs #3023

Closed
sbossarte opened this issue Jun 23, 2018 · 1 comment
Closed

Comments

@sbossarte
Copy link
Contributor

Description

When a section is deleted, but is not enabled on the primary site, its associated records aren't wholly cleaned up from the database. I've investigated the issue somewhat myself in my own testing, but there may be more cases out there I'm not seeing thus far, and any proposed changes of mine might have consequences outside of this particular issue.

I've written commit comments notes on a branch here:
https://github.com/sbossarte/cms/commits/fix-element-cleanup

Steps to reproduce

  1. Create a new (non-primary) site.
  2. Create a section that is not enabled for the primary site.
  3. Create entries for this new section, with translatable fields, and fill out contents for those entries.
  4. Delete the section.

An example test case

echo 'Creating test site' . "\n";
$site = new \craft\models\Site([
	'name' => 'Test Site',
	'handle' => 'testSite',
	'groupId' => \Craft::$app->getSites()->getPrimarySite()->groupId,
	'language' => \Craft::$app->getSites()->getPrimarySite()->language,
	'baseUrl' => '@web/testSite',
]);
if (!\Craft::$app->getSites()->saveSite($site)) {
	throw new \Exception('Failed to create site!');
}

$siteId = $site->id;
echo 'Site has ID: ' . $siteId . "\n";

echo 'Creating section' . "\n";
$section = new \craft\models\Section([
	'name' => 'Test Channel',
	'handle' => 'testChannel',
	'type' => \craft\models\Section::TYPE_CHANNEL,
	'siteSettings' => [
		new \craft\models\Section_SiteSettings([
			'siteId' => $siteId,
			'enabledByDefault' => true,
			'hasUrls' => true,
			'uriFormat' => 'test-channel/{slug}',
			'template' => '_testChannel',
		])
	]
]);
if (!\Craft::$app->getSections()->saveSection($section)) {
	throw new \Exception('Failed to create section (channel)!');
}

$sectionId = $section->id;
echo 'Section has ID: ' . $sectionId . "\n";

echo 'Creating field group' . "\n";
$fieldGroup = new \craft\models\FieldGroup([
	'name' => 'Test Fields',
]);
if (!\Craft::$app->getFields()->saveGroup($fieldGroup)) {
	throw new \Exception('Failed to create field group!');
}

echo 'Creating text field' . "\n";
$textField = \Craft::$app->getFields()->createField([
	'type' => \craft\fields\PlainText::class,
	'groupId' => $fieldGroup->id,
	'name' => 'Test Text Field',
	'handle' => 'testTextField',
	'instructions' => 'Test text field.',
	'translationMethod' => \craft\base\Field::TRANSLATION_METHOD_SITE,
	'translationKeyFormat' => null,
	'settings' => []
]);
if (!\Craft::$app->getFields()->saveField($textField)) {
	throw new \Exception('Failed to create text field!');
}

echo 'Creating entry type' . "\n";
$entryType = new \craft\models\EntryType([
	'name' => 'Test Channel Entry',
	'handle' => 'testChannelEntry',
	'sectionId' => $sectionId,
]);
$fieldLayout = new \craft\models\FieldLayout([
	'type' => \craft\elements\Entry::class,
]);
$fieldLayoutTab = new \craft\models\FieldLayoutTab([
	'name' => 'Content',
	'sortOrder' => 1,
]);
$fieldLayoutTab->setFields([$textField]);
$fieldLayout->setTabs([$fieldLayoutTab]);
$fieldLayout->setFields($fieldLayoutTab->getFields());
$entryType->setFieldLayout($fieldLayout);
if (!\Craft::$app->getSections()->saveEntryType($entryType)) {
	throw new \Exception('Failed to create entry type!');
}

echo 'Creating entry' . "\n";
$entry = new \craft\elements\Entry([
	'sectionId' => $sectionId,
	'siteId' => $siteId,
	'typeId' => $entryType->id,
	'authorId' => \Craft::$app->getUser()->getIdentity()->id,
	'enabled' => true,
	'title' => 'Test Entry asdf',
]);
$entry->setFieldValues([
	'testTextField' => 'This is test content.',
]);
if (!\Craft::$app->getElements()->saveElement($entry)) {
	throw new \Exception('Failed to create entry!');
}

$entryId = $entry->id;
echo 'Entry has ID: ' . $entryId . "\n";

echo '`entries` rows with ID ' . $entryId .': ';
echo \craft\records\Entry::find()->where(['id' => $entryId])->count() . "\n";

echo '`elements` rows with ID ' . $entryId .': ';
echo \craft\records\Element::find()->where(['id' => $entryId])->count() . "\n";

echo '`elements_sites` rows for element ID ' . $entryId .': ';
echo \craft\records\Element_SiteSettings::find()->where(['elementId' => $entryId])->count() . "\n";

echo '`content` rows for element ID ' . $entryId .': ';
echo (new \craft\db\Query())->from('{{%content}}')->where(['elementId' => $entryId])->count() . "\n";

echo '`searchindex` rows for element ID ' . $entryId .': ';
echo (new \craft\db\Query())->from('{{%searchindex}}')->where(['elementId' => $entryId])->count() . "\n";

echo 'Deleting section' . "\n";
\Craft::$app->getSections()->deleteSection($section);

echo '`entries` rows with ID ' . $entryId .': ';
echo \craft\records\Entry::find()->where(['id' => $entryId])->count() . "\n";

echo '`elements` rows with ID ' . $entryId .': ';
echo \craft\records\Element::find()->where(['id' => $entryId])->count() . "\n";

echo '`elements_sites` rows for element ID ' . $entryId .': ';
echo \craft\records\Element_SiteSettings::find()->where(['elementId' => $entryId])->count() . "\n";

echo '`content` rows for element ID ' . $entryId .': ';
echo (new \craft\db\Query())->from('{{%content}}')->where(['elementId' => $entryId])->count() . "\n";

echo '`searchindex` rows for element ID ' . $entryId .': ';
echo (new \craft\db\Query())->from('{{%searchindex}}')->where(['elementId' => $entryId])->count() . "\n";

Additional info

  • Craft version: Craft Solo 3.0.12
  • PHP version: 7.1.18
  • Database driver & version: MySQL 5.5.56
  • Plugins & versions: None
@brandonkelly
Copy link
Member

Fixed for today’s release - thanks!

This also ended up being the case when deleting entry types. And it could have even affected sections that are enabled for the primary site, if the “Propagate entries across all enabled sites?” setting was disabled.

brandonkelly pushed a commit that referenced this issue Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants