/** * Migrate to a specific version using steps from current version * * @param integer $steps number of versions to jump to * @param string $current_version current version * @param $string $direction direction to migrate to 'up'/'down' */ private function migrate_from_offset($steps, $current_version, $direction) { $migrations = $this->_migrator_util->get_migration_files($this->_migratorDirs, $direction); $current_index = $this->_migrator_util->find_version($migrations, $current_version, true); $current_index = $current_index !== null ? $current_index : -1; if ($this->_debug == true) { $this->_return .= print_r($migrations, true); $this->_return .= "\ncurrent_index: " . $current_index . "\n"; $this->_return .= "\ncurrent_version: " . $current_version . "\n"; $this->_return .= "\nsteps: " . $steps . " {$direction}\n"; } // If we are not at the bottom then adjust our index (to satisfy array_slice) if ($current_index == -1 && $direction === 'down') { $available = array(); } else { if ($direction === 'up') { $current_index += 1; } else { $current_index += $steps; } // check to see if we have enough migrations to run - the user // might have asked to run more than we have available $available = array_slice($migrations, $current_index, $steps); } $target = end($available); if ($this->_debug == true) { $this->_return .= "\n------------- TARGET ------------------\n"; $this->_return .= print_r($target, true); } $this->prepare_to_migrate(isset($target['version']) ? $target['version'] : null, $direction); }
/** * Primary task entry point * * @param array $args The current supplied options. */ public function execute($args) { $output = "Started: " . date('Y-m-d g:ia T') . "\n\n"; $output .= "[db:status]: \n"; $util = new Ruckusing_Util_Migrator($this->_adapter); $migrations = $util->get_executed_migrations(); $files = $util->get_migration_files($this->get_framework()->migrations_directory(), 'up'); $applied = array(); $not_applied = array(); foreach ($files as $file) { if (in_array($file['version'], $migrations)) { $applied[] = $file['class'] . ' [ ' . $file['version'] . ' ]'; } else { $not_applied[] = $file['class'] . ' [ ' . $file['version'] . ' ]'; } } if (count($applied) > 0) { $output .= $this->_displayMigrations($applied, 'APPLIED'); } if (count($not_applied) > 0) { $output .= $this->_displayMigrations($not_applied, 'NOT APPLIED'); } $output .= "\n\nFinished: " . date('Y-m-d g:ia T') . "\n\n"; return $output; }
/** * Update the local schema to handle multiple records versus the prior architecture * of storing a single version. In addition take all existing migration files * and register them in our new table, as they have already been executed. */ public function update_schema_for_timestamps() { //only create the table if it doesnt already exist $this->_adapter->create_schema_version_table(); //insert all existing records into our new table $migrator_util = new Ruckusing_Util_Migrator($this->_adapter); $files = $migrator_util->get_migration_files($this->migrations_directories(), 'up'); foreach ($files as $file) { if ((int) $file['version'] >= PHP_INT_MAX) { //its new style like '20081010170207' so its not a candidate continue; } //query old table, if it less than or equal to our max version, then its a candidate for insertion $query_sql = sprintf("SELECT version FROM %s WHERE version >= %d", RUCKUSING_SCHEMA_TBL_NAME, $file['version']); $existing_version_old_style = $this->_adapter->select_one($query_sql); if (count($existing_version_old_style) > 0) { //make sure it doesnt exist in our new table, who knows how it got inserted? $new_vers_sql = sprintf("SELECT version FROM %s WHERE version = %d", $this->_adapter->get_schema_version_table_name(), $file['version']); $existing_version_new_style = $this->_adapter->select_one($new_vers_sql); if (empty($existing_version_new_style)) { // use sprintf & %d to force it to be stripped of any leading zeros, we *know* this represents an old version style // so we dont have to worry about PHP and integer overflow $insert_sql = sprintf("INSERT INTO %s (version) VALUES (%d)", $this->_adapter->get_schema_version_table_name(), $file['version']); $this->_adapter->query($insert_sql); } } } }
/** * Indicate if a class name is already used * * @param string $classname The class name to test * @param string $migrationDir The directory of migration files * * @return bool */ public static function classNameIsDuplicated($classname, $migrationDir) { $migrationFiles = Ruckusing_Util_Migrator::get_migration_files($migrationDir, 'up'); $classname = strtolower($classname); foreach ($migrationFiles as $file) { if (strtolower($file['class']) == $classname) { return true; } } return false; }