Skip to content

Instantly share code, notes, and snippets.

@apinstein
Created January 12, 2012 16:05
Show Gist options
  • Save apinstein/1601324 to your computer and use it in GitHub Desktop.
Save apinstein/1601324 to your computer and use it in GitHub Desktop.
mp-helper.php; calculate all migration re-sequencing
<?php
$fromBranch = 'master';
$toBranch = 'videoClips-integration';
// find common ancestor
$commonAncestor = commonAncestor($fromBranch, $toBranch);
print "Common ancestor of {$fromBranch}..{$toBranch} is {$commonAncestor}\n";
$sharedMigrations = migrationsForBranch($commonAncestor);
$lastSharedMigration = lastMigrationFromArray($sharedMigrations);
print "Most recent migration on both branches is {$lastSharedMigration}.\n";
$fromBranchMigrations = migrationsForBranch($fromBranch);
$lastFromMigration = lastMigrationFromArray($fromBranchMigrations);
print "Last migration on {$fromBranch} is {$lastFromMigration}.\n";
$resequenceBase = migrationNameAfterMigration($lastFromMigration);
$toBranchMigrations = migrationsForBranch($toBranch);
$onlyOnToBranch = array_diff($toBranchMigrations, $sharedMigrations);
$migrationToBeat = $lastFromMigration;
$githelpers = array();
foreach ($onlyOnToBranch as $migration) {
if (compareMigrations($migration, $migrationToBeat) === -1)
{
// needs resequence
$resequencedMigration = migrationNameAfterMigration($migrationToBeat);
print "Need to reseqeunce {$migration} to {$resequencedMigration}\n";
$migrationToBeat = $resequencedMigration;
$githelpers[] = "git mv migrations/{$migration}.php migrations/{$resequencedMigration}.php && sed -i -e 's/{$migration}/{$resequencedMigration}/g' migrations/{$resequencedMigration}.php";
}
else
{
print "Migration {$migration} doesn't need re-sequencing.\n";
}
}
print join("\n", $githelpers) . "\n";
function commonAncestor($a, $b)
{
$cmd = "git merge-base {$a} {$b} 2>&1";
$commonAncestor = `$cmd`;
return trim($commonAncestor);
}
function migrationsForBranch($branch)
{
$cmd = "git ls-tree {$branch} --name-only -r -- migrations | sed -n -e 's/migrations.\([0-9_]\+\).*/\\1/gp' 2>&1";
$migrations = explode("\n", `$cmd`); // split lines into array
$migrations = array_map('trim', $migrations); // trim whitespace
$migrations = array_filter($migrations); // remove empty lines
return $migrations;
}
function lastMigrationFromArray($migrations)
{
if (count($migrations) === 0) return NULL;
$lastI = count($migrations) - 1;
return $migrations[$lastI];
}
function migrationNameAfterMigration($migration)
{
$dts = strtotime(str_replace('_', ' ', $migration));
if (!$dts) throw new Exception("couldn't parse $migration.");
// is last migration from today? if so, bump dts by 1 s
if (strtotime('today') < $dts)
{
$resequencedDts = $dts + 1;
}
// otherwise, use today_00000N
else
{
$resequencedDts = strtotime('today') + 1;
}
return date('Ymd_His', $resequencedDts);
}
function compareMigrations($a, $b)
{
// normalize & assert
$tA = strtotime(str_replace('_', ' ', $a));
$tB = strtotime(str_replace('_', ' ', $b));
if (!$tA) throw new Exception("couldn't parse {$a}");
if (!$tB) throw new Exception("couldn't parse {$b}");
// compare
if ($tA === $tB) return 0;
return ($tA < $tB) ? -1 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment