Skip to content

Instantly share code, notes, and snippets.

@pringlized
Forked from bruceoutdoors/DbmigrateController.php
Last active August 29, 2015 14:15
Show Gist options
  • Save pringlized/cbe9b79fa50481e93478 to your computer and use it in GitHub Desktop.
Save pringlized/cbe9b79fa50481e93478 to your computer and use it in GitHub Desktop.
MySQL database script for full schema migrations in Laravel 5. Unlike the gist this was forked from, this script will write out each table to an individual file.
<?php namespace App\Http\Controllers;
/* * **
*
* MySQL database script for full schema migrations in Laravel 5.
*
* 1. Place this file inside app/Http/controllers/
*
* 2. In this file, edit the index() method to customize this script to your needs.
* - inside $migrate->ignore(), you pass in an array of table
* names that you want to ignore. Note that Laravel's 'migrations'
* table is ignored by default.
* - inside $migrate->convert(), pass in your database name.
*
* 3. Add to your app/Http/routes.php:
*
* get('dbmigrate', 'DbmigrateController@index');
*
* 4. run this script by going to http://your-site.com/dbmigrate, the resulting
* migration file will be generated in app/database/migrations/
*
* @author Jim Pringle <pringlizeds@gmail.com>
* credits to @Christopher Pitt, @michaeljcalkins, and @Lee Zhen Yong whom this gist is forked off
*
* ** */
use App\Http\Controllers\Controller;
use \Illuminate\Support\Facades\DB;
use \Illuminate\Support\Str;
class DbmigrateController extends Controller
{
public function index()
{
$migrate = new SqlMigrations;
$migrate->ignore(['ignore_this_table', 'and_this_table']);
$migrate->convert('dbname');
$migrate->write();
}
}
class SqlMigrations
{
private static $ignore = array('migrations');
private static $database = "";
private static $migrations = false;
private static $schema = array();
private static $selects = array('column_name as Field', 'column_type as Type', 'is_nullable as Null', 'column_key as Key', 'column_default as Default', 'extra as Extra', 'data_type as Data_Type');
private static $instance;
private static $up = "";
private static $down = "";
private static function getTables()
{
return DB::select('SELECT table_name FROM information_schema.tables WHERE table_schema="' . self::$database . '"');
}
private static function getTableDescribes($table)
{
return DB::table('information_schema.columns')
->where('table_schema', '=', self::$database)
->where('table_name', '=', $table)
->get(self::$selects);
}
private static function getForeignTables()
{
return DB::table('information_schema.KEY_COLUMN_USAGE')
->where('CONSTRAINT_SCHEMA', '=', self::$database)
->where('REFERENCED_TABLE_SCHEMA', '=', self::$database)
->select('TABLE_NAME')->distinct()
->get();
}
private static function getForeigns($table)
{
return DB::table('information_schema.KEY_COLUMN_USAGE')
->where('CONSTRAINT_SCHEMA', '=', self::$database)
->where('REFERENCED_TABLE_SCHEMA', '=', self::$database)
->where('TABLE_NAME', '=', $table)
->select('COLUMN_NAME', 'REFERENCED_TABLE_NAME', 'REFERENCED_COLUMN_NAME')
->get();
}
private static function compileSchema($name, $values)
{
$upSchema = "";
$downSchema = "";
$newSchema = "";
$schema = "<?php
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Database\\Migrations\\Migration;
//
// Autogenerated Migration Created: " . date("Y-m-d H:i:s") . "
// ------------------------------------------------------------
class Create" . str_replace('_', '', Str::title($name)) . "Table extends Migration {
\t/**
\t * Run the migrations.
\t *
\t * @return void
\t*/
\tpublic function up()
\t{
{$values['up']}
\t}
\t/**
\t * Reverse the migrations.
\t *
\t * @return void
\t*/
\tpublic function down()
\t{
{$values['down']}
\t}
}";
return $schema;
}
public function up($up)
{
self::$up = $up;
return self::$instance;
}
public function down($down)
{
self::$down = $down;
return self::$instance;
}
public function ignore($tables)
{
self::$ignore = array_merge($tables, self::$ignore);
return self::$instance;
}
public function migrations()
{
self::$migrations = true;
return self::$instance;
}
public function write()
{
foreach (self::$schema as $name => $values) {
if (in_array($name, self::$ignore)) {
continue;
}
$schema = self::compileSchema($name, $values);
$filename = date('Y_m_d_His') . "_create_" . $name . "_table.php";
file_put_contents("../database/migrations/{$filename}", $schema);
}
}
/*
public function get()
{
return self::compileSchema();
}
*/
public function convert($database)
{
self::$instance = new self();
self::$database = $database;
$table_headers = array('Field', 'Type', 'Null', 'Key', 'Default', 'Extra');
$tables = self::getTables();
foreach ($tables as $key => $value) {
if (in_array($value->table_name, self::$ignore)) {
continue;
}
$down = "\t\tSchema::drop('{$value->table_name}');";
$up = "\t\tSchema::create('{$value->table_name}', function($" . "table) {\n";
$tableDescribes = self::getTableDescribes($value->table_name);
foreach ($tableDescribes as $values) {
$method = "";
$para = strpos($values->Type, '(');
$type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type;
$numbers = "";
$nullable = $values->Null == "NO" ? "" : "->nullable()";
$default = empty($values->Default) ? "" : "->default(\"{$values->Default}\")";
$unsigned = strpos($values->Type, "unsigned") === false ? '' : '->unsigned()';
$unique = $values->Key == 'UNI' ? "->unique()" : "";
switch ($type) {
case 'int' :
$method = 'unsignedInteger';
break;
case 'char' :
case 'varchar' :
$para = strpos($values->Type, '(');
$numbers = ", " . substr($values->Type, $para + 1, -1);
$method = 'string';
break;
case 'float' :
$method = 'float';
break;
case 'decimal' :
$para = strpos($values->Type, '(');
$numbers = ", " . substr($values->Type, $para + 1, -1);
$method = 'decimal';
break;
case 'bigint' :
$method = 'bigInteger';
break;
case 'smallint' :
$method = 'smallInteger';
break;
case 'tinyint' :
$method = 'boolean';
break;
case 'date':
$method = 'date';
break;
case 'timestamp' :
$method = 'timestamp';
break;
case 'datetime' :
$method = 'dateTime';
break;
case 'mediumtext' :
$method = 'mediumtext';
break;
case 'text' :
$method = 'text';
break;
}
if ($values->Key == 'PRI') {
$method = 'increments';
}
$up .= "\t\t\t$" . "table->{$method}('{$values->Field}'{$numbers}){$nullable}{$default}{$unsigned}{$unique};\n";
}
$up .= "\t\t});\n";
self::$schema[$value->table_name] = array(
'up' => $up,
'down' => $down
);
}
// add foreign constraints, if any
$tableForeigns = self::getForeignTables();
if (sizeof($tableForeigns) !== 0) {
foreach ($tableForeigns as $key => $value) {
$up = "Schema::table('{$value->TABLE_NAME}', function($" . "table) {\n";
$foreign = self::getForeigns($value->TABLE_NAME);
foreach ($foreign as $k => $v) {
$up .= "\t\t\t$" . "table->foreign('{$v->COLUMN_NAME}')->references('{$v->REFERENCED_COLUMN_NAME}')->on('{$v->REFERENCED_TABLE_NAME}');\n";
}
$up .= "\t\t});\n";
self::$schema[$value->TABLE_NAME . '_foreign'] = array(
'up' => $up,
'down' => $down
);
}
}
return self::$instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment