Skip to content
Browse files

Merge pull request #65 from crynobone/patch/generate-migration-indent…

…ation

Update generate migration code and generated file readibility
  • Loading branch information...
2 parents 0533d6e + c850fee commit 8bcf70939e4b766e963d32fc7a92ef4fb401cb96 @philsturgeon philsturgeon committed Apr 15, 2012
Showing with 84 additions and 71 deletions.
  1. +84 −71 classes/generate/migration/actions.php
View
155 classes/generate/migration/actions.php
@@ -64,7 +64,7 @@ public static function create($subjects, $fields)
}
// ID Field
- $field_str = "\t\t\t'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),".PHP_EOL . $field_str;
+ $field_str = "\t\t\t'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),".PHP_EOL . $field_str;
$up = <<<UP
\DBUtil::create_table('{$subjects[1]}', array(
@@ -89,6 +89,7 @@ public static function add($subjects, $fields)
$name = array_shift($field);
$field_opts = array();
+
foreach($field as $option => $val)
{
if($val === true)
@@ -107,23 +108,26 @@ public static function add($subjects, $fields)
}
}
}
+
$field_opts = implode(', ', $field_opts);
$field_up_str .= "\t\t\t'$name' => array({$field_opts}),".PHP_EOL;
- $field_down[] = "'$name'";
+ $field_down[] = "\t\t\t'$name'".PHP_EOL;
}
- $field_down_str = implode(',', $field_down);
+
+ $field_down_str = implode(',', $field_down);
+
$up = <<<UP
- \DBUtil::add_fields('{$subjects[1]}', array(
-\t\t\t$field_up_str
- ));
+ \DBUtil::add_fields('{$subjects[1]}', array(
+$field_up_str
+ ));
UP;
- $down = <<<DOWN
- \DBUtil::drop_fields('{$subjects[1]}', array(
-\t\t\t$field_down_str
- ));
+ $down = <<<DOWN
+ \DBUtil::drop_fields('{$subjects[1]}', array(
+$field_down_str
+ ));
DOWN;
- return array($up, $down);
+ return array($up, $down);
}
// rename_field_{fieldname}_to_{newfieldname}_in_{table}
@@ -147,7 +151,7 @@ public static function rename_field($subjects, $fields)
case 'varbinary':
case 'char':
case 'varchar':
- $constraint = $column['character_maximum_length'];
+ $constraint = $column['character_maximum_length'];
break;
case 'enum':
@@ -157,18 +161,20 @@ public static function rename_field($subjects, $fields)
}
break;
}
+
$constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
+
$up = <<<UP
\DBUtil::modify_fields('{$subjects[0]}', array(
\t\t\t'{$subjects[1]}' => array('name' => '{$subjects[2]}', 'type' => '{$column['data_type']}'$constraint_str)
));
UP;
- $down = <<<DOWN
- \DBUtil::modify_fields('{$subjects[0]}', array(
+ $down = <<<DOWN
+ \DBUtil::modify_fields('{$subjects[0]}', array(
\t\t\t'{$subjects[2]}' => array('name' => '{$subjects[1]}', 'type' => '{$column['data_type']}'$constraint_str)
));
DOWN;
- return array($up, $down);
+ return array($up, $down);
}
// rename_table_{tablename}_to_{newtablename}
@@ -191,66 +197,73 @@ public static function drop($subjects, $fields)
$up = <<<UP
\DBUtil::drop_table('{$subjects[1]}');
UP;
- $field_str = '';
- $column_list = \DB::list_columns($subjects[1]);
- foreach ($column_list as $column)
- {
- switch ($column['type'])
- {
- case 'float':
- $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
- break;
- case 'int':
- $constraint = $column['display'];
- break;
- case 'string':
- switch ($column['data_type'])
- {
- case 'binary':
- case 'varbinary':
- case 'char':
- case 'varchar':
- $constraint = $column['character_maximum_length'];
- break;
-
- case 'enum':
- case 'set':
- $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
- break;
- }
- break;
- }
- $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
- $auto_increment = $column['extra'] == 'auto_increment' ? ", 'auto_increment' => true" : '';
- $default_str = $column['default'] != null ? ", 'default' => '{$column['default']}'" : ", 'null' => true";
- if ($column['key'] == 'PRI')
- {
- $primary_keys[] = "'{$column['name']}'";
- }
- else if ($column['key'] == 'MUL')
- {
- $indexes[] = $column['name'];
- }
- $field_str .= "\t\t\t'{$column['name']}' => array('type' => '{$column['data_type']}'{$default_str}{$constraint_str}{$auto_increment}),".PHP_EOL;
- unset($constraint);
- }
- $primary_keys = implode(',', $primary_keys);
- $down = <<<DOWN
+ $field_str = '';
+ $column_list = \DB::list_columns($subjects[1]);
+
+ foreach ($column_list as $column)
+ {
+ switch ($column['type'])
+ {
+ case 'float':
+ $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
+ break;
+ case 'int':
+ $constraint = $column['display'];
+ break;
+ case 'string':
+ switch ($column['data_type'])
+ {
+ case 'binary':
+ case 'varbinary':
+ case 'char':
+ case 'varchar':
+ $constraint = $column['character_maximum_length'];
+ break;
+
+ case 'enum':
+ case 'set':
+ $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
+ break;
+ }
+ break;
+ }
+
+ $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
+ $auto_increment = $column['extra'] == 'auto_increment' ? ", 'auto_increment' => true" : '';
+ $default_str = $column['default'] != null ? ", 'default' => '{$column['default']}'" : ", 'null' => true";
+
+ if ($column['key'] == 'PRI')
+ {
+ $primary_keys[] = "'{$column['name']}'";
+ }
+ else if ($column['key'] == 'MUL')
+ {
+ $indexes[] = $column['name'];
+ }
+
+ $field_str .= "\t\t\t'{$column['name']}' => array('type' => '{$column['data_type']}'{$default_str}{$constraint_str}{$auto_increment}),".PHP_EOL;
+ unset($constraint);
+ }
+
+ $primary_keys = implode(',', $primary_keys);
+ $down = <<<DOWN
\DBUtil::create_table('{$subjects[1]}', array(
$field_str
), array($primary_keys));
DOWN;
- $down .= PHP_EOL;
-
- $active_db = \Config::get('db.active');
- $table_prefix = \Config::get('db.'.$active_db.'.table_prefix');
- if (isset($indexes))
- {
- foreach ($indexes as $field)
- {
- $down .= "\t\t\\DB::query(\"CREATE INDEX {$field}_idx ON {$table_prefix}{$subjects[1]} (`{$field}`)\")->execute();".PHP_EOL;
- }
- }
+ $down .= PHP_EOL;
+
+ $active_db = \Config::get('db.active');
+ $table_prefix = \Config::get('db.'.$active_db.'.table_prefix');
+
+ if (isset($indexes))
+ {
+ foreach ($indexes as $field)
+ {
+ $down .= "\t\t\\DB::query(\"CREATE INDEX {$field}_idx ON {$table_prefix}{$subjects[1]} (`{$field}`)\")->execute();".PHP_EOL;
+ }
+ }
+
return array($up, $down);
}

0 comments on commit 8bcf709

Please sign in to comment.
Something went wrong with that request. Please try again.