Created
August 2, 2013 17:22
-
-
Save spencerdeinum/6141670 to your computer and use it in GitHub Desktop.
Really terrible code, seriously just awful
This file contains hidden or 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
<?php | |
class ReverseMigration_Task | |
{ | |
function __construct() | |
{ | |
$this->sql_types = array( | |
'int' => 'integer', | |
'varchar' => 'string', | |
'date' => 'date', | |
'tinyint' => 'boolean', | |
'datetime' => 'date', | |
'text' => 'text', | |
); | |
} | |
public function run($arguments) | |
{ | |
if( empty( $arguments ) ) | |
{ | |
echo "Usage: php artisan reversemigration <tablename> <migration_name>"; | |
return; | |
} | |
if( !isset( $arguments[0] ) || !isset( $arguments[1] ) ) | |
{ | |
echo "Usage: php artisan reversemigration <tablename> <migration_name>"; | |
return; | |
} | |
// Grab the arguments from the command line | |
$table_name = $arguments[0]; | |
$migration_name = $arguments[1]; | |
// Grab the columns for the specified table | |
$columns = DBUtil::columns( $table_name, null, true ); | |
$columns = $this->codify_columns( $columns ); | |
// Grab the migration stub file so we can replace its contents. | |
$migration_stub = File::get(path('app').'tasks/reversemigration/stub'.EXT); | |
$class_name = Str::classify($migration_name); | |
$migration_stub = str_replace('{{class}}', $class_name, $migration_stub); | |
$migration_stub = str_replace('{{table_name}}', $table_name, $migration_stub); | |
$migration_stub = str_replace('{{up_columns}}', $columns, $migration_stub); | |
$file_name = date('Y_m_d_His').'_'.$migration_name.EXT; | |
File::put( path('app').'migrations/'.$file_name, $migration_stub ); | |
echo "Migration ".$file_name." created from the ".$table_name." table."; | |
} | |
private function codify_columns( $columns ) | |
{ | |
$code = array('$table->increments("id");'); | |
$matches = array(); | |
foreach( $columns as $c ) | |
{ | |
if( $c->name !== 'id' ) | |
{ | |
$line = "\t\t\t"; | |
preg_match("/(\w+)\(?(\d+)?\)? ?(\w+)?/", $c->type, $matches); | |
if( isset( $matches[1] ) ) | |
{ | |
$line .= '$table->'.$this->sql_types[$matches[1]].'("'.$c->name.'"'; | |
} | |
if( isset( $matches[2] ) && $matches[1] != 'int' ) | |
{ | |
$line .= ', '.$matches[2]; | |
} | |
$line .= ')'; | |
if( isset( $matches[3] ) && $matches[3] === 'unsigned' ) | |
{ | |
$line .= '->unsigned()'; | |
} | |
if( $c->null === "YES" ) | |
{ | |
$line .= '->nullable()'; | |
} | |
if( $c->default ) | |
{ | |
$line .= '->default("'.$c->default.'")'; | |
} | |
$code[] = $line.';'; | |
} | |
} | |
return implode("\n", $code); | |
} | |
} |
This file contains hidden or 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
<?php | |
class {{class}} { | |
/** | |
* Make changes to the database. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create("{{table_name}}", function($table) { | |
{{up_columns}} | |
}); | |
} | |
/** | |
* Revert the changes to the database. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop("{{table_name}}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment