@@ -70,6 +70,13 @@ class Migration extends ConsoleController
7070 */
7171 private $ migration ;
7272
73+ /**
74+ * Schema Directory Exports
75+ *
76+ * @var string
77+ */
78+ private $ schemaExportDirectory = 'schema-exports ' ;
79+
7380 public function __construct ()
7481 {
7582 parent ::__construct ();
@@ -440,7 +447,7 @@ public function usefile($file, $key)
440447 echo $ this ->success ("\t$ file done " .PHP_EOL );
441448
442449 $ elapsedTime = round (microtime (true ) - $ startTime , 3 ) * 1000 ;
443-
450+
444451 echo $ this ->warning ("\tTook $ elapsedTime ms to run migrations " , 1 );
445452
446453 } catch (\Exception $ e ) {
@@ -450,6 +457,83 @@ public function usefile($file, $key)
450457
451458 }
452459
460+ /**
461+ * Export Migrated Schema As SQL
462+ *
463+ * @param string $name
464+ * @param string $removeTables
465+ * @return void
466+ */
467+ public function exportSchema ($ name = null , $ removeTables = null )
468+ {
469+
470+ $ tables = $ this ->db ->list_tables ();
471+
472+ if ($ name === null ) {
473+ echo $ this ->error ("\tName cannot be empty please provide a name for file to be exported " , 1 );
474+ exit ;
475+ }
476+
477+ if ($ removeTables != null ) {
478+ $ removeTables = str_replace ('__ ' , ', ' , $ removeTables );
479+ $ removeTables = explode (', ' , $ removeTables );
480+ $ tables = array_diff ($ tables , $ removeTables );
481+ }
482+
483+ $ webbyVersion = WEBBY_VERSION ;
484+ $ appUrl = base_url ();
485+ $ dateGenerated = date ('M d, Y ' ) .' at ' . date ('H:i A ' );
486+ $ phpVersion = PHP_VERSION ;
487+
488+ $ desc = <<<DESC
489+ -- Webby Schema Dump
490+ -- Version: {$ webbyVersion }
491+ --
492+ -- App Url: {$ appUrl }
493+ -- Generation Time: {$ dateGenerated }
494+ -- PHP Version: $ phpVersion
495+
496+ SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
497+ START TRANSACTION;
498+ SET time_zone = "+00:00";
499+ DESC ;
500+
501+ $ content = $ desc ;
502+ $ content .= "\r\n" ;
503+
504+ $ filepath = ROOTPATH .'database ' . DS . $ this ->schemaExportDirectory . DS ;
505+ $ filename = '' ;
506+
507+ if ($ tables ) {
508+ foreach ($ tables as $ table ) {
509+ $ content .= "\r\n" ;
510+ $ content .= 'DROP TABLE IF EXISTS ` ' . $ table . '`; ' ;
511+ $ content .= "\r\n\n" ;
512+ $ content .= $ this ->db ->query ('SHOW CREATE TABLE ' . $ table )->row_array ()['Create Table ' ] . '; ' ;
513+ $ content .= "\r\n" ;
514+ }
515+
516+ $ filename = $ name . "-schema.sql " ;
517+ }
518+
519+ if (!is_dir ($ filepath )) {
520+ mkdir ($ filepath , 0775 );
521+ }
522+
523+ $ this ->load ->helper ('file ' );
524+ $ saved = write_file ($ filepath . $ filename , $ content );
525+
526+ if ($ saved ) {
527+ echo $ this ->success ("\n\tDatabase Schema " , 0 ). $ this ->info ("[ " .$ filename ."] " , 0 ). $ this ->success (" Exported successfully \n" , 1 );
528+ exit ;
529+ }
530+
531+ if ($ saved ) {
532+ echo $ this ->error ("\n\tDatabase Schema " , 0 ). $ this ->info ("[ " .$ filename ."] " ,0 ). $ this ->error (" Was Not Exported \n" , 1 );
533+ exit ;
534+ }
535+
536+ }
453537
454538 /**
455539 * Truncate Migrations Table
0 commit comments