Skip to content

Commit 7a06191

Browse files
committed
Adds structureUid migration
1 parent 460d8d1 commit 7a06191

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace BarrelStrength\Sprout\redirects\migrations;
4+
5+
use BarrelStrength\Sprout\core\modules\SettingsHelper;
6+
use BarrelStrength\Sprout\redirects\migrations\helpers\RedirectStructureHelper;
7+
use Craft;
8+
use craft\db\Migration;
9+
use craft\errors\ElementNotFoundException;
10+
11+
/**
12+
* @role permanent
13+
* @schema sprout-module-redirects
14+
* @since merge with install migration in craftcms/cms:6.0
15+
*/
16+
class m260218_000001_ensure_integrity_of_structureUid extends Migration
17+
{
18+
public const SPROUT_KEY = 'sprout';
19+
public const MODULE_ID = 'sprout-module-redirects';
20+
21+
/**
22+
* There is a bug in the install/upgrade path where the structureUid in the Project Config can get out of sync with
23+
* the structureUid in the database. This migration ensures the structureUid from the project config matches the
24+
* structureUid in the database.
25+
*/
26+
public function safeUp(): void
27+
{
28+
$moduleSettingsKey = self::SPROUT_KEY . '.' . self::MODULE_ID;
29+
30+
$projectConfigSettings = Craft::$app->getProjectConfig()->get($moduleSettingsKey);
31+
$structureUidFromConfig = $projectConfigSettings['structureUid'] ?? null;
32+
33+
if (!$structureUidFromConfig) {
34+
throw new ElementNotFoundException('Unable to find Structure Element in Project Config for Redirects.');
35+
}
36+
37+
$structureUidInDb = RedirectStructureHelper::getStructureUidFromDb($structureUidFromConfig);
38+
39+
if (!$structureUidInDb) {
40+
throw new ElementNotFoundException('Unable to find or create Structure Element for Redirects.');
41+
}
42+
43+
if ($structureUidFromConfig !== $structureUidInDb) {
44+
RedirectStructureHelper::updateStructureInDbToMatchUidInProjectConfig(
45+
$structureUidFromConfig,
46+
$structureUidInDb
47+
);
48+
}
49+
}
50+
51+
public function safeDown(): bool
52+
{
53+
echo self::class . " cannot be reverted.\n";
54+
55+
return false;
56+
}
57+
}

0 commit comments

Comments
 (0)