Skip to content

Instantly share code, notes, and snippets.

@lukearmstrong
Created October 22, 2012 15:33
Show Gist options
  • Save lukearmstrong/3932101 to your computer and use it in GitHub Desktop.
Save lukearmstrong/3932101 to your computer and use it in GitHub Desktop.
Generate Migrations Config file from the database
<?php
namespace Fuel\Tasks;
class Generate_Migrations_Config
{
public function run()
{
// Backup the migrations.php config files, wherever the hell they may be.
$this->backupConfig();
$this->backupConfig(\Fuel::$env);
// Read the migration table to get the current state of the database.
$migrations = \DB::select('*')->from('migration')->order_by('type', 'ASC')->order_by('name', 'ASC')->order_by('migration', 'ASC')->execute()->as_array();
// Setup the config
$config = array(
'version' => array(),
'folder' => 'migrations/',
'table' => 'migration',
);
// Add all migrations to the config
foreach ($migrations as $migration) {
$type = $migration['type'];
$name = $migration['name'];
$config['version'][$type][$name][] = $migration['migration'];
}
// Save config array to app/config/<env>/migrations.php
\Config::save(APPPATH.'config'.DS.\Fuel::$env.DS.'migrations.php', $config);
}
private function backupConfig($env=null)
{
if (null === $env) {
// Global, app folder
$env_path = '';
} else {
$env_path = $env.DS;
}
if (file_exists(APPPATH.'config'.DS.$env_path.'migrations.php')) {
$datetime = date('YmdHis');
if (\File::rename(APPPATH.'config'.DS.$env_path.'migrations.php', APPPATH.'config'.DS.$env_path.'migrations.backup-'.$datetime.'.php')) {
\Cli::write('mv '.APPPATH.'config'.DS.$env_path.'migrations.php '.APPPATH.'config'.DS.$env_path.'migrations.backup-'.$datetime.'.php');
}
else {
\Cli::write('mv '.APPPATH.'config'.DS.$env_path.'migrations.php '.APPPATH.'config'.DS.$env_path.'migrations.backup-'.$datetime.'.php');
\Cli::error('Unable to backup the current migration config, needs write permissions.');
exit;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment