public function deleteRelated($tables = []) { $function = new \ReflectionClass(get_called_class()); $table = strtolower($function->getShortName()); foreach ($tables as $relatedTable) { DB::query("DELETE FROM {$relatedTable} WHERE {$table}" . "_id = " . $this->id); } }
<?php use vendor\DB\DB; echo "Creating job table..." . PHP_EOL; DB::query("CREATE TABLE jobs (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n jobName VARCHAR(255),\n runType VARCHAR(255),\n runScript TEXT,\n cron VARCHAR(255),\n failScript TEXT,\n last_run DATETIME,\n last_result INTEGER,\n hash VARCHAR(255),\n sharedkey VARCHAR(32),\n view_private TINYINT(1),\n user_id INTEGER,\n comments TEXT,\n force_run TINYINT(1)\n);"); //DB::query("INSERT INTO jobs VALUES (null, 'test', 1, 'TESTING', '*/5 * * * *', 'TESTING', '2015-01-01', 0, '123', '123', 0, 1, 'TEST COMMENT', 0)"); //DB::query("INSERT INTO jobs VALUES (null, 'test2', 1, 'TESTING', '*/5 * * * *', 'TESTING', '2015-01-01', 0, '321', '321', 1, 1, 'TEST COMMENT2', 0)");
<?php use vendor\DB\DB; echo "Creating session table..." . PHP_EOL; DB::query("CREATE TABLE sessions (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n sessionid VARCHAR(255),\n ip VARCHAR(255),\n userAgent VARCHAR(255),\n data TEXT\n)"); echo "Creating users table..." . PHP_EOL; DB::query("CREATE TABLE users (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n email VARCHAR(255)\n)"); //echo "Adding user " . PHP_EOL; //DB::insert("users", ["email" => "*****@*****.**"]);
<?php use vendor\DB\DB; echo "Creating history table..."; DB::query("CREATE TABLE histories (\n id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n output TEXT,\n jobs_id INTEGER,\n run_date DATETIME,\n time_taken DECIMAL(10,5),\n result INTEGER,\n nonce VARCHAR(255)\n );"); //DB::query("INSERT INTO histories VALUES (null, 'THIS IS ONLY A TEST', 1, '2015-01-01', 10, 0, 'ABC')");
public function runMigrations() { global $argv; $this->setupDatabaseConnection(); DB::query("CREATE TABLE IF NOT EXISTS migrations (\n\t\t\t\t\t\t\t id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t\t migration INTEGER,\n\t\t\t\t\t\t\t ran_at DATETIME\n\t\t\t\t)"); switch ($argv[1]) { case "show": foreach (DB::fetch("SELECT migration, ran_at FROM migrations") as $migration) { echo $migration["migration"] . " => " . $migration["ran_at"] . PHP_EOL; } break; case "count": echo DB::column("SELECT COUNT(id) FROM migrations"); break; case "run": $migrations = DB::fetch("SELECT migration FROM migrations"); $migrationArray = []; foreach ($migrations as $migration) { $migrationArray[] = $migration["migration"]; } foreach (glob("application/migrations/*.php") as $filename) { if (!in_array($filename, $migrationArray)) { try { include $filename; DB::insert("migrations", ["migration" => $filename, "ran_at" => (new \DateTime())->format("Y-m-d")]); } catch (\Exception $e) { echo "[HF_Core] - Migration error - {$e}"; exit(1); } } } break; case "clear": DB::query("DELETE FROM migrations"); break; case "reset": switch ($this->config["DATABASE_TYPE"]) { case "SQLITE": DB::$c = null; unlink($this->config["DATABASE_FILE"]); break; case "MYSQL": DB::query("DROP DATABASE " . $this->config['MYSQL_DBNAME']); DB::query("CREATE DATABASE " . $this->config['MYSQL_DBNAME']); break; } break; } }
<?php spl_autoload_extensions(".php"); // comma-separated list spl_autoload_register(); foreach (glob("system/vendor/*.php") as $filename) { include $filename; } if (!is_cli()) { die("This script must be ran from the command line"); } $core = new \system\engine\HF_Core(true); $core->setupDatabaseConnection(); if (count($argv) == 1) { echo "Possible commands are all-clear or adduser"; exit(0); } switch ($argv[1]) { case "all-clear": \vendor\DB\DB::query("DELETE FROM histories"); \vendor\DB\DB::query("DELETE FROM users"); \vendor\DB\DB::query("DELETE FROM sessions"); \vendor\DB\DB::query("DELETE FROM jobs"); break; case "adduser": $user = $argv[2]; \vendor\DB\DB::query("INSERT INTO users VALUES (null, ?)", [$user]); break; }