function main($args) { //input sanity check if (!is_array($args) || is_array($args) && !array_key_exists('name', $args)) { print_help(true); } $migration_name = $args['name']; //clear any filesystem stats cache clearstatcache(); //check to make sure our migration directory exists if (!is_dir(MIGRATION_DIR)) { die_with_error("ERROR: migration directory '" . MIGRATION_DIR . "' does not exist. Specify MIGRATION_DIR in config/config.inc.php and try again."); } //generate a complete complete $highest_version = VersionUtil::get_highest_migration(MIGRATION_DIR); $next_version = VersionUtil::to_migration_number($highest_version + 1); $klass = NamingUtil::camelcase($migration_name); $file_name = $next_version . '_' . $klass . '.php'; $full_path = realpath(MIGRATION_DIR) . '/' . $file_name; $template_str = get_template($klass); //check to make sure our destination directory is writable if (!is_writable(MIGRATION_DIR . '/')) { die_with_error("ERROR: migration directory '" . MIGRATION_DIR . "' is not writable by the current user. Check permissions and try again."); } //write it out! $file_result = file_put_contents($full_path, $template_str); if ($file_result === FALSE) { die_with_error("Error writing to migrations directory/file. Do you have sufficient privileges?"); } else { echo "\nCreated migration: {$file_name}\n\n"; } }
public function test_index_name() { $column = "first_name"; $this->assertEquals("idx_users_first_name", NamingUtil::index_name("users", $column)); $column = "age"; $this->assertEquals("idx_users_age", NamingUtil::index_name("users", $column)); $column = array('listing_id', 'review_id'); $this->assertEquals("idx_users_listing_id_and_review_id", NamingUtil::index_name("users", $column)); }
public static function index_name($table_name, $column_name) { $name = sprintf("idx_%s", NamingUtil::underscore($table_name)); //if the column parameter is an array then the user wants to create a multi-column //index if (is_array($column_name)) { $column_str = join("_and_", $column_name); } else { $column_str = $column_name; } $name .= sprintf("_%s", $column_str); return $name; }
public function has_index($table_name, $column_name, $options = array()) { if (empty($table_name)) { throw new ArgumentException("Missing table name parameter"); } if (empty($column_name)) { throw new ArgumentException("Missing column name parameter"); } //did the user specify an index name? if (is_array($options) && array_key_exists('name', $options)) { $index_name = $options['name']; } else { $index_name = NamingUtil::index_name($table_name, $column_name); } $indexes = $this->indexes($table_name); foreach ($indexes as $idx) { if ($idx['name'] == $index_name) { return true; } } return false; }
private function load_all_tasks($task_dir) { if (!is_dir($task_dir)) { throw new Exception(sprintf("Task dir: %s does not exist", $task_dir)); return false; } $files = scandir($task_dir); $regex = '/^class\\.(\\w+)\\.php$/'; foreach ($files as $f) { //skip over invalid files if ($f == '.' || $f == ".." || !preg_match($regex, $f, $matches)) { continue; } require_once $task_dir . '/' . $f; $task_name = NamingUtil::task_from_class_name($matches[1]); $klass = NamingUtil::class_from_file_name($f); $this->register_task($task_name, new $klass($this->get_adapter())); } }
private function run_migrations($migrations, $target_method, $destination) { $last_version = -1; foreach ($migrations as $file) { $full_path = MIGRATION_DIR . '/' . $file['file']; if (is_file($full_path) && is_readable($full_path)) { require_once $full_path; $klass = NamingUtil::class_from_migration_file($file['file']); $obj = new $klass(); $refl = new ReflectionObject($obj); if ($refl->hasMethod($target_method)) { $obj->set_adapter($this->adapter); $start = $this->start_timer(); try { //start transaction $this->adapter->start_transaction(); $result = $obj->{$target_method}(); //successfully ran migration, update our version and commit $version = $this->resolve_versions($destination, $file['version'], $target_method); $this->adapter->set_current_version($version); $this->adapter->commit_transaction(); } catch (Exception $e) { $this->adapter->rollback_transaction(); //wrap the caught exception in our own $ex = new Exception(sprintf("%s - %s", $file['class'], $e->getMessage())); throw $ex; } $end = $this->end_timer(); $diff = $this->diff_timer($start, $end); printf("========= %s ======== (%.2f)\n", $file['class'], $diff); $last_version = $file['version']; $exec = true; } else { trigger_error("ERROR: {$klass} does not have a '{$target_method}' method defined!"); } } //is_file } //foreach //update the schema info $result = array('last_version' => $last_version); return $result; }