Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Finish the migration and the rebuild process!
  • Loading branch information
NaysKutzu committed May 4, 2024
1 parent 601e84b commit 3702ceb
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 4 deletions.
1 change: 0 additions & 1 deletion commands/core/database/dbconfigure.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function configure(): void
$password = readline();
echo color::translateColorsCode("&fEnter the database name: ");
$database = readline();

// Perform validation
if (empty($username) || empty($password) || empty($database)) {
echo color::translateColorsCode("&cPlease provide all the required information.&o");
Expand Down
73 changes: 73 additions & 0 deletions commands/core/database/dbmigrate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
use MythicalSystemsFramework\Cli\Colors as color;
use MythicalSystemsFramework\Database\MySQL;
use MythicalSystemsFramework\Managers\ConfigManager as cfg;

class dbmigrateCommand
{
public function execute()
{
$db = new MySQL();
if ($db->tryConnection(cfg::get("database", "host"), cfg::get("database", "port"), cfg::get("database", "username"), cfg::get("database", "password"), cfg::get("database", "name")) == true) {
echo color::translateColorsCode("&fConnection to the database was &asuccessful!&o");
echo color::NewLine();
echo color::translateColorsCode("&fDo you want to migrate the database? (&ey&f/&en&f): ");
$confirm = readline();
if (strtolower($confirm) == 'y') {
$this->migrate();
} else {
die(color::translateColorsCode("&fExiting...&o"));
}
return;
} else {
die(color::translateColorsCode("&cFailed to connect to the database!&o"));
}
}

public function migrate()
{
try {
$mysql = new MySQL();
$db = $mysql->connect();

$db->exec("
CREATE TABLE IF NOT EXISTS migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
script VARCHAR(255) NOT NULL,
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
");

$sqlFiles = glob(__DIR__.'/../../../migrate/database/*.sql');

if (count($sqlFiles) > 0) {
foreach ($sqlFiles as $sqlFile) {
$script = file_get_contents($sqlFile);

$stmt = $db->prepare("SELECT COUNT(*) FROM migrations WHERE script = ?");
$stmt->execute([$sqlFile]);
$count = $stmt->fetchColumn();

if ($count == 0) {
$db->exec($script);

$stmt = $db->prepare("INSERT INTO migrations (script) VALUES (?)");
$stmt->execute([$sqlFile]);

echo color::translateColorsCode("&fExecuted migration: &e" . basename($sqlFile) . "&o");
echo color::NewLine();
} else {
echo color::translateColorsCode("&fSkipping migration: &e" . basename($sqlFile) . " &f(&ealready executed&f)&o");
echo color::NewLine();
}
}
} else {
echo color::translateColorsCode("&cNo migrations found!&o");
echo color::NewLine();
}
} catch (PDOException $e) {
echo color::translateColorsCode("&cFailed to migrate the database: " . $e->getMessage() . "&o");
echo color::NewLine();
}
}
}
56 changes: 56 additions & 0 deletions commands/core/database/dbrebuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
use MythicalSystemsFramework\Cli\Colors as color;
use MythicalSystemsFramework\Database\MySQL;
use MythicalSystemsFramework\Managers\ConfigManager as cfg;

class dbrebuildCommand
{
public function execute()
{
$db = new MySQL();
if ($db->tryConnection(cfg::get("database", "host"), cfg::get("database", "port"), cfg::get("database", "username"), cfg::get("database", "password"), cfg::get("database", "name")) == true) {
echo color::translateColorsCode("&fConnection to the database was &asuccessful!&o");
echo color::NewLine();
echo color::translateColorsCode("&4&lWARNING: &fThis option will wipe the database. &o");
echo color::translateColorsCode("&4&lWARNING: &fOnly use this function if you know what you are doing &o");
echo color::translateColorsCode("&4&lWARNING: &fOnce you wipe the database there is no going back! &o");
echo color::translateColorsCode("&4&lWARNING: &fPlease be careful and don't play around with commands! &o");
echo color::translateColorsCode("&4&lWARNING: &fThere is no other message then this so keep in mind! &o");
echo color::translateColorsCode("&4&lWARNING: &fDo you really want to wipe the database? (&ey&f/&en&f): ");

$confirm = readline();
if (strtolower($confirm) == 'y') {
$this->wipe();
} else {
die(color::translateColorsCode("&fExiting...&o"));
}
return;
} else {
die(color::translateColorsCode("&cFailed to connect to the database!&o"));
}
}

public function wipe() {
try {
$mysql = new MySQL();
$db = $mysql->connect();

$db->exec("SET FOREIGN_KEY_CHECKS = 0");
$tables = $db->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$db->exec("DROP TABLE IF EXISTS $table");
}
$db->exec("SET FOREIGN_KEY_CHECKS = 1");
echo color::NewLine();
echo color::NewLine();
echo color::NewLine();
echo color::NewLine();
echo color::NewLine();
echo color::translateColorsCode("&fDatabase wiped!!&o");
echo color::translateColorsCode("&fPlease run the migration command to rebuild the database!&o");
} catch (PDOException $e) {
echo color::translateColorsCode("&fFailed to drop tables: &c" . $e->getMessage() . "&o");
echo color::NewLine();
}
}
}
21 changes: 20 additions & 1 deletion commands/core/encryption/newkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ class newkeyCommand
public function execute()
{
if (cfg::get("encryption","key") == "") {

$this->generate();
} else {
echo color::translateColorsCode("&4&lWARNING: &fA key already exists. Do you want to generate a new one?&o");
echo color::translateColorsCode("&4&lWARNING: &fGenerating a new key will make all encrypted data unreadable. &o");
echo color::translateColorsCode("&4&lWARNING: &fType &ey&f to continue or &en&f to exit:");
$confirm = readline();
if (strtolower($confirm) === 'y') {
$this->generate();
} else {
die(color::translateColorsCode("&fExiting...&o"));
}
}
}
/**
*
*/
public function generate()
{
$key = "mythicalcore_".bin2hex(random_bytes(64*32));
cfg::set("encryption","key",$key);
echo color::translateColorsCode("&fKey generated successfully!&o");
}
}
2 changes: 2 additions & 0 deletions migrate/database/00:12 05.05.2024.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (`id` INT NOT NULL AUTO_INCREMENT , `username` TEXT NOT NULL , `email` TEXT NOT NULL , `first_name` TEXT NOT NULL , `last_name` TEXT NOT NULL , `uuid` TEXT NOT NULL , `password` TEXT NOT NULL , `token` TEXT NOT NULL , `avatar` TEXT NOT NULL , `role` ENUM('Administrator','Admin','Support','Member') NOT NULL DEFAULT 'Member' , `first_ip` TEXT NOT NULL , `last_ip` TEXT NOT NULL , `banned` TEXT NULL DEFAULT NULL , `last_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , `first_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB;
5 changes: 5 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

$router = new \Router\Router();
$renderer = new Smarty();

if (cfg::get("encryption","key") == "") {
die("We are sorry but you are missing the encryption key!");
}

if (!is_writable(__DIR__)) {
die("We have no access to the framework directory!");
}
Expand Down
5 changes: 3 additions & 2 deletions settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"name": ""
},
"encryption": {
"key": "",
"method": "MythicalCore",
"key": ""
},
"__last_updated": "2024-05-04 14:45:10"
"__last_updated": "2024-05-04 22:06:15"
}

0 comments on commit 3702ceb

Please sign in to comment.