From 8896b8d05a1102bfc0d9ada3c548f62302546da4 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 9 May 2026 13:50:18 +0000 Subject: [PATCH] new installation fix --- db/seed.sql | 14 ++------ includes/DatabaseInstaller.php | 63 ++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/db/seed.sql b/db/seed.sql index 541053b..c8cb114 100644 --- a/db/seed.sql +++ b/db/seed.sql @@ -303,17 +303,9 @@ INSERT INTO `role_permissions` VALUES UNLOCK TABLES; -- --- Dumping data for table `users` --- - -LOCK TABLES `users` WRITE; -/*!40000 ALTER TABLE `users` DISABLE KEYS */; -INSERT INTO `users` VALUES -(1,1,'admin','$2y$10$uLxB5VJa9nZ3nGhukx7jOeBotQ/PtumL3ndbzTZ.TjrAr9yJH/hIq','admin@example.com',NULL,'uploads/profile_1_1771401598.png','default','active','2026-02-18 05:30:12'), -(2,1,'admin2','$2y$10$A299UOd8VZWXyuKcbcLwWewIg.7V2ti3oomGaKH9qhMm4vdG6a/xi',NULL,NULL,NULL,'default','active','2026-02-18 06:44:10'), -(5,1,'moosa','$2y$10$Ho5NrFTAb6HUH4VvCd4NZuCsKGMl4v1pvWiesEuC29fDDu6Pkg7AS','aalabry@gmail.com',NULL,NULL,'default','active','2026-02-18 09:30:38'); -/*!40000 ALTER TABLE `users` ENABLE KEYS */; -UNLOCK TABLES; +-- Users are created during installation step 3. +-- Keep seed data user-free so fresh installs do not ship demo admin accounts +-- and stay compatible with the latest users table structure. -- -- Dumping data for table `payment_methods` diff --git a/includes/DatabaseInstaller.php b/includes/DatabaseInstaller.php index 08f7a4e..3edb6c0 100644 --- a/includes/DatabaseInstaller.php +++ b/includes/DatabaseInstaller.php @@ -138,28 +138,49 @@ class DatabaseInstaller { private static function executeSqlFile(string $filePath): void { require_once __DIR__ . '/../db/config.php'; - $host = DB_HOST; - $name = DB_NAME; - $user = DB_USER; - $pass = DB_PASS; - - $passwordSegment = $pass !== '' ? ' -p' . escapeshellarg($pass) : ''; - $command = sprintf( - 'mysql -h %s -u %s%s %s < %s', - escapeshellarg($host), - escapeshellarg($user), - $passwordSegment, - escapeshellarg($name), - escapeshellarg($filePath) - ); - - $output = []; - $returnVar = 0; - exec($command . ' 2>&1', $output, $returnVar); - - if ($returnVar !== 0) { - throw new Exception('Failed to execute SQL file: ' . implode("\n", $output)); + $pdo = db(); + $sql = file_get_contents($filePath); + if ($sql === false) { + throw new RuntimeException('Unable to read SQL file: ' . basename($filePath)); } + + $statements = self::splitSqlStatements($sql); + foreach ($statements as $index => $statement) { + if (self::shouldSkipSqlFileStatement($statement, $filePath)) { + continue; + } + + try { + $pdo->exec($statement); + } catch (PDOException $exception) { + if (self::isIgnorableMigrationError($pdo, $exception, $statement)) { + continue; + } + + throw new RuntimeException( + 'Failed to execute SQL file ' . basename($filePath) . ' at statement ' . ($index + 1) . ': ' . $exception->getMessage(), + 0, + $exception + ); + } + } + } + + private static function shouldSkipSqlFileStatement(string $statement, string $filePath): bool { + $normalized = strtoupper(trim($statement)); + if ($normalized === '') { + return true; + } + + if (str_starts_with($normalized, 'LOCK TABLES ') || str_starts_with($normalized, 'UNLOCK TABLES')) { + return true; + } + + if (basename($filePath) === 'seed.sql' && preg_match('/^INSERT\s+INTO\s+`?users`?/i', $statement) === 1) { + return true; + } + + return false; } private static function ensureMigrationsTable(PDO $pdo): void {