/** * Checks for differences in mapping information compared to the database, and creates a migration if there are any * * @param string $name Name of module or package, unless this is just an 'app' migration * @param string $type Type of migration - 'app', 'module' or 'package' * @return void */ public static function sync($name = 'default', $type = 'app', $run = false, $reset = false) { try { set_time_limit(0); ignore_user_abort(true); ini_set('memory_limit', '256M'); } catch (\Exception $e) { // Nothing! } \Config::load("db", true, true); \D::$clear_cache = true; $em = \D::manager(); $files = glob(APPPATH . "migrations/*_*.php"); if ($reset) { $files[] = APPPATH . 'config/development/migrations.php'; $files[] = APPPATH . 'config/production/migrations.php'; $files[] = APPPATH . 'config/staging/migrations.php'; foreach ($files as $file) { try { \File::delete($file); } catch (\Exception $e) { } } $files = array(); \Config::load('migrations', true, true); } // This will create the migrations table if necessary Migrate::_init(); $last_file = end($files); if ($last_file) { // Try and find the last file migration in the DB $last_name = basename($last_file, ".php"); $migration_table = \Config::get('migrations.table', 'migration'); $last_migration = \DB::query("SELECT migration FROM {$migration_table} WHERE type = '{$type}' AND name = '{$name}' AND migration = '{$last_name}'")->execute(); if (count($last_migration) === 0) { if (\Fuel::$is_cli && \Cli::prompt('You have previous migrations to run - you must run these before generating a new one. Continue?', array('y', 'n')) == 'y') { \Cli::write("\tRunning previous migrations...", 'green'); Migrate::latest($name, $type); } else { return array('error' => 'There are previous migrations to run - please do this first before syncing.'); } } } $diff = static::getDiff(); if (array_key_exists('error', $diff)) { if (\Fuel::$is_cli) { \Cli::write($diff['error'], 'red'); return; } else { return $diff; } } static::generateMigration($diff['up'], $diff['down']); if (\Fuel::$is_cli && ($run === true || \Cli::prompt('Would you like to run the new migration now?', array('y', 'n')) == 'y') || !\Fuel::$is_cli && $run) { Migrate::latest($name, $type); static::createAllStaticInstances(); } return array('success' => 'The sync was completed and all migrations performed successfully'); }