Skip to content

Commit e5fafef

Browse files
committed
✨ add dumpDb() method to export the whole database to sql file
Signed-off-by: otengkwame <[email protected]>
1 parent 78d94be commit e5fafef

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Core/controllers/commands/Migration.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class Migration extends ConsoleController
7777
*/
7878
private $schemaExportDirectory = 'schema-exports';
7979

80+
/**
81+
* Database Dumps
82+
*
83+
* @var string
84+
*/
85+
private $databaseDumpDirectory = 'dumps';
86+
8087
public function __construct()
8188
{
8289
parent::__construct();
@@ -535,6 +542,117 @@ public function exportSchema($name = null, $removeTables = null)
535542

536543
}
537544

545+
/**
546+
* Dump Migrated Schema
547+
*
548+
* @param string $name
549+
* @return void
550+
*/
551+
public function dumpDb($name = null)
552+
{
553+
$tables = $this->db->list_tables();
554+
555+
if ($name === null) {
556+
echo $this->error("\tName cannot be empty please provide a name for file to be dumped", 1);
557+
exit;
558+
}
559+
560+
$webbyVersion = WEBBY_VERSION;
561+
$appUrl = base_url();
562+
$dateGenerated = date('M d, Y') .' at '. date('H:i A');
563+
$phpVersion = PHP_VERSION;
564+
565+
$desc = <<<DESC
566+
-- Webby Database Dump
567+
-- Version: {$webbyVersion}
568+
--
569+
-- App Url: {$appUrl}
570+
-- Generation Time: {$dateGenerated}
571+
-- PHP Version: $phpVersion
572+
573+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
574+
START TRANSACTION;
575+
SET time_zone = "+00:00";
576+
DESC;
577+
578+
$content = $desc;
579+
$content .= "\r\n";
580+
581+
$filepath = ROOTPATH .'database'. DS . $this->databaseDumpDirectory . DS;
582+
$filename = '';
583+
584+
if ($tables) {
585+
$i = 0;
586+
foreach ($tables as $table) {
587+
588+
$content .= "\r\n";
589+
$content .= 'DROP TABLE IF EXISTS `' . $table . '`;';
590+
$content .= "\r\n\n";
591+
$content .= $this->db->query('SHOW CREATE TABLE ' . $table)->row_array()['Create Table'] . ';';
592+
$content .= "\r\n";
593+
594+
$fields = $this->db->list_fields($table);
595+
$table_data = $this->db->query('SELECT * FROM ' . $table)->result_array();
596+
$insert_field = '';
597+
$insert_values = '';
598+
599+
if ($fields && $table_data) {
600+
601+
$insert_field .= "\r\n";
602+
$insert_field .= 'INSERT INTO `' . $table . '` (';
603+
604+
foreach ($fields as $field) {
605+
$insert_field .= '`' . $field . '`,';
606+
}
607+
608+
$insert_field = substr($insert_field, 0, -1);
609+
$insert_field .= ')';
610+
611+
$insert_values .= ' VALUES ';
612+
$insert_values .= "\r\n";
613+
614+
foreach ($table_data as $table_row) {
615+
$insert_values .= '(';
616+
foreach ($table_row as $column => $value) {
617+
$insert_values .= "'" . addslashes($value) . "',";
618+
}
619+
$insert_values = substr($insert_values, 0, -1);
620+
$insert_values .= '),';
621+
$insert_values .= "\r\n";
622+
}
623+
624+
$insert_values = substr($insert_values, 0, -3) . ';';
625+
$insert_values .= "\r\n";
626+
627+
}
628+
629+
$content .= $insert_field . $insert_values;
630+
631+
$i++;
632+
}
633+
634+
$filename = $name . "-dump.sql";
635+
}
636+
637+
if (!is_dir($filepath)) {
638+
mkdir($filepath, 0775);
639+
}
640+
641+
$this->load->helper('file');
642+
$saved = write_file($filepath . $filename, $content);
643+
644+
if ($saved) {
645+
echo $this->success("\n\tDatabase Schema ", 0). $this->info("[".$filename."]", 0). $this->success(" Exported successfully\n", 1);
646+
exit;
647+
}
648+
649+
if ($saved) {
650+
echo $this->error("\n\tDatabase Schema ", 0). $this->info("[".$filename."]",0). $this->error(" Was Not Exported\n", 1);
651+
exit;
652+
}
653+
654+
}
655+
538656
/**
539657
* Truncate Migrations Table
540658
*

0 commit comments

Comments
 (0)