#!/usr/bin/env php exec(file_get_contents('resources/schemas/migrations.sql')); } $files = glob('resources/schemas/' . DB::driver() . '/*.sql'); $names = array_map(function ($path) { return basename($path); }, $files); $in = str_repeat('?, ', count($names) - 1) . '?'; $inMigrationsTable = DB::query("SELECT * FROM `migrations` WHERE `name` IN ($in)", $names)->fetchAll(); foreach ($files as $file) { $continue = false; $exists = false; foreach ($inMigrationsTable as $migration) { if (basename($file) === $migration->name && $migration->migrated) { $continue = true; break; } elseif (basename($file) === $migration->name && !$migration->migrated) { $exists = true; break; } } if ($continue) continue; $sql = file_get_contents($file); try { DB::raw()->exec($sql); if (!$exists) { DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 1]); } else { DB::query('UPDATE `migrations` SET `migrated`=? WHERE `name`=?', [1, basename($file)]); } echo "Migrated '$file'" . PHP_EOL; } catch (PDOException $exception) { if (!$exists) { DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 0]); } echo "Error migrating '$file' (" . $exception->getMessage() . ')' . PHP_EOL; echo $exception->getTraceAsString() . PHP_EOL; } } if (isset($argv[1]) && $argv[1] === '--install') { DB::query("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES ('admin@example.com', 'admin', ?, 1, ?)", [password_hash('admin', PASSWORD_DEFAULT), substr(md5(microtime()), rand(0, 26), 5)]); } echo 'Done.' . PHP_EOL;