<commit_before><?php
/**
 * This file is part of Floe, a minimalist PHP framework.
 * Copyright (C) 2007-2009 <NAME> <http://maetl.net>
 *
 * See the LICENSE file distributed with this software for full copyright, disclaimer
 * of liability, and the specific limitations that govern the use of this software.
 *
 * @version $Id$
 * @package tools
 * @subpackage tasks
 */

/**
 * Runs a migration script.
 *
 * @package tools
 * @subpackage tasks
 */
class SchemaMigrateTask {
	
	/**
	 * @description performs a schema migration script
	 */
	function process($args) {
		if (empty($args)) {
			ConsoleText::printLine("No migration specified. Defaulting to latest version.");
			$args[0] = 'up';
		}
		$db = StorageAdaptor::gateway();
		if (!$db->hasTable("schema")) {
			echo "Creating schema table...\n";
			$db->createTable("schema", array("version"=>"integer"));
		}
		
		$db->query("select version from `schema` order by version desc");
		$version = $db->getValue();
		if (!$version) {
			$db->insert("schema", array("version"=>0));
			$version = 0;
		}
		
		$versionName = str_pad($version, 3, '0', STR_PAD_LEFT);
		$versions = $this->readMigrationDir();
		if ($args[0] == 'up') {
			sort($versions);
			$offset = array_search($versionName, $versions);
			$migrations = ($version == 0) ? $versions : array_slice($versions, $offset+1);
		} elseif ($args[0] == 'down') {
			if (!isset($args[1])) die("No version specified for rollback.\n");
			rsort($versions);
			$offset = array_search($args[1], $versions);
			$total = count($versions);
			$migrations = array_slice($versions, 0, ($total-$offset)+1);
		} elseif ($args[0] == 'version') {
			ConsoleText::printLine("The installed schema is at version $versionName.");
			exit;
		} else {
			throw new Exception("Invalid migration method. Must be up or down.");
		}
		foreach($migrations as $migration) {
			$this->runMigration($migration, $args[0]);
		}
		$currentVersion = (count($versions) == 0) ? 1 : (int)$versions[count($versions)-1];
		$db->update("schema", array("id"=>1), array("version"=>$currentVersion));
	}
	
	function runMigration($version, $direction) {
		require_once DEV_DIR.'/migrations/'.$version.'.migration.php';
		$className = "Migration$version";
		if (class_exists($className)) {
			$migration = new $className;
			$migration->$direction();
			ConsoleText::printLine("Migrating $direction to $version");
		} else {
			throw new Exception("Class $className not found.");
		}<commit_msg>fixing validation of version args<commit_after><?php
/**
 * This file is part of Floe, a minimalist PHP framework.
 * Copyright (C) 2007-2009 <NAME> <http://maetl.net>
 *
 * See the LICENSE file distributed with this software for full copyright, disclaimer
 * of liability, and the specific limitations that govern the use of this software.
 *
 * @version $Id$
 * @package tools
 * @subpackage tasks
 */

/**
 * Runs a migration script.
 *
 * @package tools
 * @subpackage tasks
 */
class SchemaMigrateTask {
	
	/**
	 * @description performs a schema migration script
	 */
	function process($args) {
		if (empty($args)) {
			ConsoleText::printLine("No migration specified. Defaulting to latest version.");
			$args[0] = 'up';
		}
		
		$db = StorageAdaptor::gateway();
		if (!$db->hasTable("schema")) {
			echo "Creating schema table...\n";
			$db->createTable("schema", array("version"=>"integer"));
		}
		
		$db->query("select version from `schema` order by version desc");
		$version = $db->getValue();
		if (!$version) {
			$db->insert("schema", array("version"=>0));
			$version = 0;
		}
		
		$versionName = str_pad($version, 3, '0', STR_PAD_LEFT);
		$versions = $this->readMigrationDir();
		if ($args[0] == 'up') {
			sort($versions);
			$offset = array_search($versionName, $versions);
			$migrations = ($version == 0) ? $versions : array_slice($versions, $offset+1);
		
		} elseif ($args[0] == 'down') {
			if (!isset($args[1])) die("No version specified for rollback.\n");
			if (!is_numeric($args[1])) die("Invalid schema version (must be numeric).\n");
			
			rsort($versions);
			$offset = array_search($args[1], $versions);
			$total = count($versions);
			$migrations = array_slice($versions, 0, ($total-$offset)+1);
			
		} elseif ($args[0] == 'version') {
			ConsoleText::printLine("The installed schema is at version $versionName.");
			exit;
			
		} else {
			throw new Exception("Invalid migration method. Must be up or down.");
		}
		
		foreach($migrations as $migration) {
			$this->runMigration($migration, $args[0]);
		}
		
		$currentVersion = (count($versions) == 0) ? 1 : (int)$versions[count($versions)-1];
		$db->update("schema", array("id"=>1), array("version"=>$currentVersion));
	}
	
	function runMigration($version, $direction) {
		require_once DEV_DIR.'/migrations/'.$version.'.migration.php';
		$className = "Migration$version";
		if (class_exists($className)) {
			$migration = new $className;
			$migration->$direction();
			ConsoleText::printLine("Migrating $direction to $version");
		} else {
			throw new Exception("Class $className not found.");
		}
{'fraction_non_alphanumeric': 0.1792079207920792, 'fraction_numerical': 0.01089108910891089, 'mean_word_length': 4.250519750519751, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308508', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1577, 'n_words': 414}