update migrations

This commit is contained in:
Flatlogic Bot 2026-04-07 18:42:59 +00:00
parent 547de74ee7
commit 19ec351a02
3 changed files with 62 additions and 0 deletions

View File

@ -4,6 +4,22 @@ require_once __DIR__ . '/includes/app.php';
require_once __DIR__ . '/includes/auth.php';
require_login();
// Auto-migrate roles if needed
try {
db()->query('SELECT 1 FROM roles LIMIT 1');
} catch (Exception $e) {
if (!isset($_GET['migrated'])) {
ob_start();
require_once __DIR__ . '/db/migrations/migrate_roles.php';
ob_end_clean();
$url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') !== false ? '&' : '?') . 'migrated=1';
header('Location: ' . $url);
exit;
} else {
die('Auto-migration failed. Please run /db/migrations/migrate_roles.php manually or contact support.');
}
}
$page = $_GET['page'] ?? 'dashboard';
$action = $_GET['action'] ?? '';

View File

@ -3,6 +3,16 @@
require_once __DIR__ . '/includes/app.php';
require_once __DIR__ . '/includes/auth.php';
try {
db()->query('SELECT 1 FROM roles LIMIT 1');
} catch (Exception $e) {
ob_start();
require_once __DIR__ . '/db/migrations/migrate_roles.php';
ob_end_clean();
header('Location: ' . app_url('admin.php', ['page' => 'roles']));
exit;
}
require_permission('roles', 'view');
$pages = [

View File

@ -345,6 +345,7 @@ CREATE TABLE `users` (
`reset_token` varchar(255) DEFAULT NULL,
`reset_expires` datetime DEFAULT NULL,
`role` enum('admin','user') DEFAULT 'admin',
`role_id` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`phone` varchar(50) DEFAULT NULL,
`profile_picture` varchar(255) DEFAULT NULL,
@ -362,4 +363,39 @@ CREATE TABLE `users` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
--
-- Table structure for table `roles`
--
DROP TABLE IF EXISTS `roles`;
CREATE TABLE `roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text DEFAULT NULL,
`is_system` tinyint(1) DEFAULT 0,
`created_at` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table `role_permissions`
--
DROP TABLE IF EXISTS `role_permissions`;
CREATE TABLE `role_permissions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL,
`page` varchar(50) NOT NULL,
`can_view` tinyint(1) DEFAULT 0,
`can_add` tinyint(1) DEFAULT 0,
`can_edit` tinyint(1) DEFAULT 0,
`can_delete` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `role_page` (`role_id`,`page`),
KEY `role_id` (`role_id`),
CONSTRAINT `role_permissions_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Dump completed on 2026-04-07 13:32:08