Skip to content

Commit

Permalink
Add FKCs to the modules tables (#1130)
Browse files Browse the repository at this point in the history
* Add FKCs to the modules tables (modules_boxes_content, modules_content, modules_folderrights, modules_php_extensions).
* Add all modules to modules table (this was required for the FKC for modules_boxes_content and is done in the install module).
* Add config.php for install module.
* Remove some info from config.php of error module as it would otherwise be added to the module menu.
  • Loading branch information
blackcoder87 authored Dec 3, 2024
1 parent 0beed3c commit 2de72ff
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 75 deletions.
149 changes: 119 additions & 30 deletions application/modules/admin/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Config extends \Ilch\Config\Install
{
public $config = [
'key' => 'admin',
'system_module' => true,
'boxes' => [
'langswitch' => [
'de_DE' => [
Expand Down Expand Up @@ -76,15 +77,15 @@ public function getInstallSql(): string
`autoload` TINYINT(1) NOT NULL,
UNIQUE KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_emails` (
`moduleKey` VARCHAR(255) NOT NULL,
`type` VARCHAR(255) NOT NULL,
`desc` VARCHAR(255) NOT NULL,
`text` TEXT NOT NULL,
`locale` VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_modules` (
`key` VARCHAR(191) NOT NULL,
`system` TINYINT(1) NOT NULL DEFAULT 0,
Expand All @@ -96,37 +97,45 @@ public function getInstallSql(): string
`icon_small` VARCHAR(255) NOT NULL,
UNIQUE KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_modules_content` (
`key` VARCHAR(255) NOT NULL,
`key` VARCHAR(191) NOT NULL,
`locale` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL
`name` VARCHAR(255) NOT NULL,
INDEX `FK_[prefix]_modules_content_[prefix]_modules` (`key`) USING BTREE,
CONSTRAINT `FK_[prefix]_modules_content_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_modules_php_extensions` (
`key` VARCHAR(255) NOT NULL,
`extension` VARCHAR(255) NOT NULL
`key` VARCHAR(191) NOT NULL,
`extension` VARCHAR(255) NOT NULL,
INDEX `FK_[prefix]_modules_php_extensions_[prefix]_modules` (`key`) USING BTREE,
CONSTRAINT `FK_[prefix]_modules_php_extensions_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_modules_folderrights` (
`key` VARCHAR(255) NOT NULL,
`folder` VARCHAR(255) NOT NULL
`key` VARCHAR(191) NOT NULL,
`folder` VARCHAR(255) NOT NULL,
INDEX `FK_[prefix]_modules_folderrights_[prefix]_modules` (`key`) USING BTREE,
CONSTRAINT `FK_[prefix]_modules_folderrights_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_modules_boxes_content` (
`key` VARCHAR(255) NOT NULL,
`module` VARCHAR(255) NOT NULL,
`module` VARCHAR(191) NOT NULL,
`locale` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NOT NULL
`name` VARCHAR(255) NOT NULL,
INDEX `FK_[prefix]_modules_boxes_content_[prefix]_modules` (`module`) USING BTREE,
CONSTRAINT `FK_[prefix]_modules_boxes_content_[prefix]_modules` FOREIGN KEY (`module`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_menu` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_menu_items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`menu_id` INT(11) NOT NULL,
Expand All @@ -143,26 +152,26 @@ public function getInstallSql(): string
`access` VARCHAR(255) NOT NULL DEFAULT "",
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_boxes` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date_created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_boxes_content` (
`box_id` INT(11) NOT NULL,
`content` MEDIUMTEXT NOT NULL,
`locale` VARCHAR(255) NOT NULL,
`title` VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_pages` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date_created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_pages_content` (
`page_id` INT(11) NOT NULL,
`content` MEDIUMTEXT NOT NULL,
Expand All @@ -172,28 +181,28 @@ public function getInstallSql(): string
`title` VARCHAR(255) NOT NULL,
`perma` VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_backup` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`date` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_logs` (
`user_id` VARCHAR(255) NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`info` VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_admin_layoutadvsettings` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`layoutKey` VARCHAR(255) NOT NULL,
`key` VARCHAR(255) NOT NULL,
`value` TEXT NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_admin_notifications` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -203,21 +212,21 @@ public function getInstallSql(): string
`type` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `[prefix]_admin_notifications_permission` (
`module` VARCHAR(255) NOT NULL,
`granted` TINYINT(1) NOT NULL,
`limit` TINYINT(1) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `[prefix]_admin_updateservers` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`url` VARCHAR(255) NOT NULL,
`operator` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
INSERT INTO `[prefix]_admin_updateservers` (`id`, `url`, `operator`, `country`) VALUES (1, "https://www.ilch.de/ilch2_updates/stable/", "ilch", "Germany");
INSERT INTO `[prefix]_admin_updateservers` (`id`, `url`, `operator`, `country`) VALUES (2, "https://ilch.blackcoder.de/stable/", "ilch", "Germany");';
}
Expand Down Expand Up @@ -1056,12 +1065,92 @@ public function getUpdate(string $installedVersion): string
$notificationsMapper->addNotification($notificationModel);
break;
case "2.2.4":
// Update vendor folder
replaceVendorDirectory();
break;
case "2.2.5":
// Update vendor folder
replaceVendorDirectory();
break;
case "2.2.6":
// Add FKCs for modules_content, modules_folderrights and modules_php_extensions tables.
// The column key of modules_boxes_content is not a module key (examples: lastwar, nexttraining, ...).
$fileConfig = new \Ilch\Config\File();
$fileConfig->loadConfigFromFile(CONFIG_PATH . '/config.php');
$dbname = $fileConfig->get('dbName');

// Adjust data type for key column. Change to VARCHAR(191) from VARCHAR(255).
$this->db()->queryMulti('ALTER TABLE `[prefix]_modules_boxes_content` MODIFY COLUMN `module` VARCHAR(191) NOT NULL;
ALTER TABLE `[prefix]_modules_content` MODIFY COLUMN `key` VARCHAR(191) NOT NULL;
ALTER TABLE `[prefix]_modules_folderrights` MODIFY COLUMN `key` VARCHAR(191) NOT NULL;
ALTER TABLE `[prefix]_modules_php_extensions` MODIFY COLUMN `key` VARCHAR(191) NOT NULL;');

$moduleKeys = $this->db()->select('key')
->from('modules')
->execute()
->fetchList();

$moduleKeysBoxesContent = $this->db()->select('module')
->from('modules_boxes_content')
->execute()
->fetchList();

$moduleKeysContent = $this->db()->select('key')
->from('modules_content')
->execute()
->fetchList();

$moduleKeysFolderrights = $this->db()->select('key')
->from('modules_folderrights')
->execute()
->fetchList();

$moduleKeysPhpExtensions = $this->db()->select('key')
->from('modules_php_extensions')
->execute()
->fetchList();

$orphanedRows = array_diff($moduleKeysBoxesContent ?? [], $moduleKeys ?? []);
if (count($orphanedRows) > 0) {
$this->db()->delete()->from('modules_boxes_content')
->where(['module' => $orphanedRows])
->execute();
}

$orphanedRows = array_diff($moduleKeysContent ?? [], $moduleKeys ?? []);
if (count($orphanedRows) > 0) {
$this->db()->delete()->from('modules_content')
->where(['key' => $orphanedRows])
->execute();
}

$orphanedRows = array_diff($moduleKeysFolderrights ?? [], $moduleKeys ?? []);
if (count($orphanedRows) > 0) {
$this->db()->delete()->from('modules_folderrights')
->where(['key' => $orphanedRows])
->execute();
}

$orphanedRows = array_diff($moduleKeysPhpExtensions ?? [], $moduleKeys ?? []);
if (count($orphanedRows) > 0) {
$this->db()->delete()->from('modules_php_extensions')
->where(['key' => $orphanedRows])
->execute();
}

if (!$this->db()->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $dbname . "' AND table_name='[prefix]_modules_boxes_content' AND constraint_name='FK_[prefix]_modules_boxes_content_[prefix]_modules');")) {
$this->db()->query('ALTER TABLE `[prefix]_modules_boxes_content` ADD CONSTRAINT `FK_[prefix]_modules_boxes_content_[prefix]_modules` FOREIGN KEY (`module`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

if (!$this->db()->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $dbname . "' AND table_name='[prefix]_modules_content' AND constraint_name='FK_[prefix]_modules_content_[prefix]_modules');")) {
$this->db()->query('ALTER TABLE `[prefix]_modules_content` ADD CONSTRAINT `FK_[prefix]_modules_content_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

if (!$this->db()->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $dbname . "' AND table_name='[prefix]_modules_folderrights' AND constraint_name='FK_[prefix]_modules_folderrights_[prefix]_modules');")) {
$this->db()->query('ALTER TABLE `[prefix]_modules_folderrights` ADD CONSTRAINT `FK_[prefix]_modules_folderrights_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

if (!$this->db()->queryCell("SELECT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='" . $dbname . "' AND table_name='[prefix]_modules_php_extensions' AND constraint_name='FK_[prefix]_modules_php_extensions_[prefix]_modules');")) {
$this->db()->query('ALTER TABLE `[prefix]_modules_php_extensions` ADD CONSTRAINT `FK_[prefix]_modules_php_extensions_[prefix]_modules` FOREIGN KEY (`key`) REFERENCES `[prefix]_modules` (`key`) ON UPDATE NO ACTION ON DELETE CASCADE;');
}

break;
}

Expand Down
9 changes: 1 addition & 8 deletions application/modules/admin/mappers/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,6 @@ public function delete(string $key)
$this->db()->delete('modules')
->where(['key' => $key])
->execute();

$this->db()->delete('modules_content')
->where(['key' => $key])
->execute();

$this->db()->delete('modules_boxes_content')
->where(['module' => $key])
->execute();
// Rows in modules_boxes_content, modules_content, modules_folderrights and modules_php_extensions are being deleted due to FKCs.
}
}
13 changes: 1 addition & 12 deletions application/modules/error/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ class Config extends \Ilch\Config\Install
{
public $config = [
'key' => 'error',
'icon_small' => 'fa-solid fa-triangle-exclamation',
'system_module' => true,
'languages' => [
'de_DE' => [
'name' => 'Error',
'description' => 'Hier kannst du die Fehlerseiten verwalten.',
],
'en_EN' => [
'name' => 'Error',
'description' => 'Here you can manage the error-pages.',
],
]
'system_module' => true
];

public function install()
Expand Down
26 changes: 26 additions & 0 deletions application/modules/install/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @copyright Ilch 2
* @package ilch
*/

namespace Modules\Install\Config;

class Config extends \Ilch\Config\Install
{
public $config = [
'key' => 'install',
'system_module' => true
];

public function install()
{

}

public function getUpdate($installedVersion)
{

}
}
Loading

0 comments on commit 2de72ff

Please sign in to comment.