Migrator

version_to_str($int)

4: 	function version_to_str($int) {
5: 		if ($int < 10) {
6: 			return '00' . $int;
7: 		}
8: 		elseif ($int < 100) {
9: 			return '0' . $int;
10: 		}
11: 	}

File: lib/model/Migrator.php, #line 4

current_version()

13: 	function current_version() {
14: 		global $Config;
15: 		$db_options = $Config->get_database_options($Config->get_environment());
16: 		$tables_list = mysql_list_tables($db_options['database']);
17: 		while (list ($table) = mysql_fetch_array($tables_list)) {
18: 			if ($table == 'schema_info') {
19: 				$query = mysql_query('select * from schema_info limit 1');
20: 				while ($schema = mysql_fetch_object($query)) {
21: 					return $schema->version / 1;
22: 				}
23: 			}
24: 		}
25: 		return 0;
26: 	}

File: lib/model/Migrator.php, #line 13

set_schema_version($version)

28: 	function set_schema_version($version) {
29: 		mysql_query("DROP TABLE `schema_info`");
30: 		mysql_query("CREATE TABLE `schema_info` (`version` INT NOT NULL default 0)");
31: 		mysql_query(" insert into schema_info values ($version)");
32: 	}

File: lib/model/Migrator.php, #line 28

get_migration_files()

34: 	function get_migration_files() {
35: 		$list = array ();
36: 		if ($models = opendir(DIR_MIGRATE)) {
37: 			while (false !== ($file_name = readdir($models))) {
38: 				if (preg_match('/.php/', $file_name)) {
39: 					$list[] = $file_name;
40: 				}
41: 			}
42: 		}
43: 		return $list;
44: 	}

File: lib/model/Migrator.php, #line 34

get_migration_file($target_version)

46: 	function get_migration_file($target_version) {
47: 		foreach ($this->get_migration_files() as $file_name) {
48: 			$version = substr($file_name, 0, 3) / 1 . "n";
49: 			if ($target_version == $version) {
50: 				return $file_name;
51: 			}
52: 		}
53: 		return false;
54: 	}

File: lib/model/Migrator.php, #line 46

up()

56: 	function up() {
57: 		$target_version = $this->_current_version + 1;
58: 		$file_name = $this->get_migration_file($target_version);
59: 		$version = substr($file_name, 0, 3) / 1;
60: 		if ($file_name) {
61: 			require_once DIR_MIGRATE . $file_name;
62: 			$class = Inflector :: camelize(substr($file_name, 4, -4));
63: 			$migrate = new $class;
64: 			$migrate->version = $version;
65: 			$migrate->up();
66: 		}
67: 		if (!$migrate) {
68: 			print "Nothing to do. n";
69: 		}
70: 	}

File: lib/model/Migrator.php, #line 56

down()

72: 	function down() {
73: 		$target_version = $this->_current_version;
74: 		$file_name = $this->get_migration_file($target_version);
75: 		$version = substr($file_name, 0, 3) / 1;
76: 		if ($file_name) {
77: 			require_once DIR_MIGRATE . $file_name;
78: 			$class = Inflector :: camelize(substr($file_name, 4, -4));
79: 			$migrate = new $class;
80: 			$migrate->version = $version -1 ;
81: 			$migrate->down();
82: 		}
83: 		if (!$migrate) {
84: 			print "Nothing to do. n";
85: 		}
86: 	}

File: lib/model/Migrator.php, #line 72

reverting($target_version)

88: 	function reverting($target_version) {
89: 		$list = $this->get_migration_files();
90: 		arsort($list);
91: 		foreach ($list as $file_name) {
92: 			$version = substr($file_name, 0, 3) / 1;
93: 			if ($this->_current_version >= $version && $version > $target_version) {
94: 				require_once DIR_MIGRATE . $file_name;
95: 				$class = Inflector :: camelize(substr($file_name, 4, -4));
96: 				$migrate = new $class ();
97: 				$migrate->version = $version - 1;
98: 				$migrate->down();
99: 			}
100: 		}
101: 	}

File: lib/model/Migrator.php, #line 88

migrating($target_version = false)

103: 	function migrating($target_version = false) {
104: 		$list = $this->get_migration_files();
105: 		asort($list);
106:
107: 		foreach ($list as $file_name) {
108: 			$version = substr($file_name, 0, 3) / 1;
109:
110: 			if ($target_version) {
111: 				if ($target_version >= $version && $version > $this->_current_version) {
112: 					require_once DIR_MIGRATE . $file_name;
113: 					$class = Inflector :: camelize(substr($file_name, 4, -4));
114: 					$migrate = new $class;
115: 					$migrate->version = $version;
116: 					$migrate->up();
117: 				}
118: 			} else {
119: 				if ($this->_current_version < $version) {
120: 					require_once DIR_MIGRATE . $file_name;
121: 					$class = Inflector :: camelize(substr($file_name, 4, -4));
122: 					$migrate = new $class;
123: 					$migrate->version = $version;
124: 					$migrate->up();
125: 				}
126: 			}
127: 		}
128: 		if (!$migrate) {
129: 			print "You are on the right version n";
130: 		}
131: 	}

File: lib/model/Migrator.php, #line 103

migrate($target_version)

133: 	function migrate($target_version) {
134: 		if ($this->_current_version > $target_version) {
135: 			$this->reverting($target_version);
136: 		}
137:
138: 		if ($this->_current_version < $target_version) {
139: 			$this->migrating($target_version);
140: 		}
141:
142: 		if ($this->_current_version == $target_version) {
143: 			print "You are on the right version n";
144: 		}
145: 	}

File: lib/model/Migrator.php, #line 133

RunQuery($query)

147: 	function RunQuery($query) {
148: 		global $Logger;
149: 		if (mysql_query($query)) {
150: 			print "n " . $Logger->green($query) . "n";
151: 			$this->set_schema_version($this->version);
152: 			print "n Current schema version is: " . $this->current_version() . "n";
153: 		} else {
154: 			print get_class($this) . ": Incorrect Query... n " . $Logger->red($query) ."n";
155: 			print $Logger->gray(mysql_error()) . "n";
156: 			print "n Current schema version is: " . $this->current_version() . "n";
157: 			exit;
158: 		}
159: 	}

File: lib/model/Migrator.php, #line 147

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>