Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Generate::migration() #180

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 32 additions & 58 deletions classes/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ public static function migration($args, $build = true)
throw new Exception("Command is invalid.".PHP_EOL."\tphp oil g migration <migrationname> [<fieldname1>:<type1> |<fieldname2>:<type2> |..]");
}

// Keep the original migration name for the correct table name.
$original_migration_name = $migration_name;

$migration_name = str_replace('.', '_', $migration_name);

// Check if a migration with this name already exists
if (($duplicates = glob(APPPATH."migrations/*_{$migration_name}*")) === false)
{
Expand Down Expand Up @@ -522,71 +527,40 @@ public static function migration($args, $build = true)
*
*/
$subjects = array(false, false);
$matches = explode('_', str_replace($method_name . '_', '', $migration_name));

// create_{table}
if (count($matches) == 1)
{
$subjects = array(false, $matches[0]);
}
// Get the sentence of migration using original_migration_name.
$sentence = substr($original_migration_name, strlen($method_name) + 1);

// add_{field}_to_{table}
else if (count($matches) == 3 && $matches[1] == 'to')
{
$subjects = array($matches[0], $matches[2]);
}
// If the sentence doesn't contain "." then replaces "_" to "." for compatibility.
strpos($sentence, '.') === false and $sentence = str_replace('_', '.', $sentence);

// delete_{field}_from_{table}
else if (count($matches) == 3 && $matches[1] == 'from')
{
$subjects = array($matches[0], $matches[2]);
}

// rename_field_{field}_to_{field}_in_{table} (with underscores in field names)
else if (count($matches) >= 5 && in_array('to', $matches) && in_array('in', $matches))
{
$subjects = array(
implode('_', array_slice($matches, array_search('in', $matches)+1)),
implode('_', array_slice($matches, 0, array_search('to', $matches))),
implode('_', array_slice($matches, array_search('to', $matches)+1, array_search('in', $matches)-2))
);
}

// rename_table
else if ($method_name == 'rename_table')
{
$subjects = array(
implode('_', array_slice($matches, 0, array_search('to', $matches))),
implode('_', array_slice($matches, array_search('to', $matches)+1))
);
}

// create_{table} or drop_{table} (with underscores in table name)
else if (count($matches) !== 0)
{
$name = str_replace(array('create_', 'add_', '_to_'), array('create-', 'add-', '-to-'), $migration_name);

if (preg_match('/^(create|add)\-([a-z0-9\_]*)(\-to\-)?([a-z0-9\_]*)?$/i', $name, $deep_matches))
{
switch ($deep_matches[1])
{
case 'create' :
$subjects = array(false, $deep_matches[2]);
break;

case 'add' :
$subjects = array($deep_matches[2], $deep_matches[4]);
break;
}
}
}

// There is no subject here so just carry on with a normal empty migration
else
// Processing based on the method_name.
switch ($method_name)
{
case 'create':
$subjects = array(false, $sentence);
break;
case 'add':
$subjects = explode('.to.', $sentence);
break;
case 'delete':
$subjects = explode('.from.', $sentence);
break;
case 'rename_field':
$subjects = sscanf(str_replace('.', ' ', $sentence), '%s to %s in %s');
break;
case 'rename_table':
$subjects = explode('.to.', $sentence);
break;
case 'drop':
$subjects = array(false, $sentence);
break;
default :
break 2;
}

$subjects = str_replace('.', '_', $subjects);

// We always pass in fields to a migration, so lets sort them out here.
$fields = array();
foreach ($args as $field)
Expand Down