-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-> Finish the migration and the rebuild process!
- Loading branch information
Showing
7 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters