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: 		print "n Current schema version is: " . $this->current_version() . "n";
33: 	}

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

get_migration_files()

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

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

get_migration_file($target_version)

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

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

up()

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

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

down()

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

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

reverting($target_version)

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

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

migrating($target_version = false)

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

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

migrate($target_version)

138: 	function migrate($target_version) {
139: 		if ($this->_current_version > $target_version) {
140: 			$this->reverting($target_version);
141: 		}
142:
143: 		if ($this->_current_version < $target_version) {
144: 			$this->migrating($target_version);
145: 		}
146:
147: 		if ($this->_current_version == $target_version) {
148: 			print "You are on the right version n";
149: 		}
150: 	}

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

RunQuery($query)

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

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

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>