├── README.md └── VpxMigration.php /README.md: -------------------------------------------------------------------------------- 1 | #Create a Base Migration File from current DB 2 | 3 | 4 | 5 | When all goes well it will create a file under migrations called 001_create_base.php under your migrations folder 6 | 7 | 8 | #To use: 9 | 10 | 1: Enable migrations and set version to 1; 11 | 12 | 2: Enable to write migration folder; 13 | 14 | 3: Copy VpxMigration.php to your library folder (/application/library); 15 | 16 | 4: In controller: 17 | 18 | 19 | function make_base(){ 20 | 21 | $this->load->library('VpxMigration'); 22 | 23 | // All Tables: 24 | 25 | $this->vpxmigration->generate(); 26 | 27 | //Single Table: 28 | 29 | $this->vpxmigration->generate('table'); 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /VpxMigration.php: -------------------------------------------------------------------------------- 1 | 12 | * @license Free to use and abuse 13 | * @version 0.4 Beta 14 | * 15 | */ 16 | class VpxMigration { 17 | 18 | var $db_user; 19 | var $db_pass; 20 | var $db_host; 21 | var $db_name; 22 | var $email; 23 | var $tables = '*'; 24 | var $newline = '\n'; 25 | var $write_file = true; 26 | var $file_name = ''; 27 | var $file_per_table = true; 28 | var $path = 'migrations'; 29 | var $skip_tables = array(); 30 | var $add_view = false; 31 | 32 | /* 33 | * defaults; 34 | */ 35 | 36 | function __construct($params = null) { 37 | // parent::__construct(); 38 | isset($this->ci) OR $this->ci = get_instance(); 39 | $this->ci->db_master = $this->ci->db; 40 | $this->db_user = $this->ci->db_master->username; 41 | $this->db_pass = $this->ci->db_master->password; 42 | $this->db_host = $this->ci->db_master->hostname; 43 | $this->db_name = $this->ci->db_master->database; 44 | $this->path = APPPATH . $this->path; 45 | if ($params) 46 | $this->init_config($params); 47 | } 48 | 49 | /** 50 | * Init Config if there is any passed 51 | * 52 | * 53 | * @param type $params 54 | */ 55 | function init_config($params = array()) { //apply config 56 | if (count($params) > 0) 57 | { 58 | foreach ($params as $key => $val) 59 | { 60 | if (isset($this->$key)) 61 | { 62 | $this->$key = $val; 63 | } 64 | } 65 | } 66 | } 67 | 68 | /** 69 | * Generate the file. 70 | * 71 | * @param string $tables 72 | * @return boolean|string 73 | */ 74 | function generate($tables = null) { 75 | if ($tables) 76 | $this->tables = $tables; 77 | 78 | $return = ''; 79 | /* open file */ 80 | if ($this->write_file) 81 | { 82 | if (!is_dir($this->path) OR !is_really_writable($this->path)) 83 | { 84 | $msg = "Unable to write migration file: " . $this->path; 85 | log_message('error', $msg); 86 | echo $msg; 87 | return; 88 | } 89 | 90 | if (!$this->file_per_table) 91 | { 92 | $file_path = $this->path . '/' . $this->file_name . '.sql'; 93 | $file = fopen($file_path, 'w+'); 94 | 95 | if (!$file) 96 | { 97 | $msg = "no file"; 98 | log_message('error', $msg); 99 | echo $msg; 100 | return FALSE; 101 | } 102 | } 103 | } 104 | 105 | 106 | // if default, then run all tables, otherwise just do the list provided 107 | if ($this->tables == '*') 108 | { 109 | 110 | $query = $this->ci->db_master->query('SHOW full TABLES FROM ' . $this->ci->db_master->protect_identifiers($this->ci->db_master->database)); 111 | 112 | $retval = array(); 113 | 114 | 115 | if ($query->num_rows() > 0) 116 | { 117 | 118 | foreach ($query->result_array() as $row) 119 | { 120 | 121 | $tablename = 'Tables_in_' . $this->ci->db_master->database; 122 | 123 | if (isset($row[$tablename])) 124 | { 125 | /* check if table in skip arrays, if so, go next */ 126 | if (in_array($row[$tablename], $this->skip_tables)) 127 | continue; 128 | 129 | /* check if views to be migrated */ 130 | if ($this->add_view) 131 | { 132 | ## not implemented ## 133 | //$retval[] = $row[$tablename]; 134 | } else 135 | { 136 | /* skip views */ 137 | if (strtolower($row['Table_type']) == 'view') 138 | { 139 | continue; 140 | } 141 | $retval[] = $row[$tablename]; 142 | } 143 | } 144 | } 145 | } 146 | 147 | $this->tables = array(); 148 | $this->tables = $retval; 149 | } else 150 | { 151 | $this->tables = is_array($tables) ? $tables : explode(',', $tables); 152 | } 153 | 154 | ## if write file, check if we can 155 | if ($this->write_file) 156 | { 157 | 158 | /* make subdir */ 159 | $path = $this->path . '/' . $this->file_name; 160 | 161 | if (!@is_dir($path)) 162 | { 163 | 164 | if (!@mkdir($path, DIR_WRITE_MODE, true)) 165 | { 166 | return FALSE; 167 | } 168 | 169 | @chmod($path, DIR_WRITE_MODE); 170 | } 171 | 172 | if (!is_dir($path) OR !is_really_writable($path)) 173 | { 174 | $msg = "Unable to write backup per table file: " . $path; 175 | log_message('error', $msg); 176 | 177 | return; 178 | } 179 | 180 | //$file_path = $path . '/001_create_' . $table . '.php'; 181 | $file_path = $path . '/001_create_base.php'; 182 | $file = fopen($file_path, 'w+'); 183 | 184 | if (!$file) 185 | { 186 | $msg = 'No File'; 187 | log_message('error', $msg); 188 | echo $msg; 189 | 190 | return FALSE; 191 | } 192 | } 193 | 194 | 195 | $up = ''; 196 | $down = ''; 197 | //loop through tables 198 | foreach ($this->tables as $table) 199 | { 200 | 201 | log_message('debug', print_r($table, true)); 202 | 203 | $q = $this->ci->db_master->query('describe ' . $this->ci->db_master->protect_identifiers($this->ci->db_master->database . '.' . $table)); 204 | // No result means the table name was invalid 205 | if ($q === FALSE) 206 | { 207 | continue; 208 | } 209 | 210 | $columns = $q->result_array(); 211 | 212 | $q = $this->ci->db_master->query(' SHOW TABLE STATUS WHERE Name = \'' . $table . '\''); 213 | $engines = $q->row_array(); 214 | 215 | 216 | $up .= "\n\t\t" . '## Create Table ' . $table . "\n"; 217 | foreach ($columns as $column) 218 | { 219 | $up .= "\t\t" . '$this->dbforge->add_field("' . "`$column[Field]` $column[Type] " . ($column['Null'] == 'NO' ? 'NOT NULL' : 'NULL') . 220 | ( 221 | # if its timestamp column, don't '' around default value .... crap way, but should work for now 222 | $column['Default'] ? ' DEFAULT ' . ($column['Type'] == 'timestamp' ? $column['Default'] : '\'' . $column['Default'] . '\'') : '' 223 | ) 224 | . " $column[Extra]\");" . "\n"; 225 | 226 | if ($column['Key'] == 'PRI') 227 | $up .= "\t\t" . '$this->dbforge->add_key("' . $column['Field'] . '",true);' . "\n"; 228 | } 229 | $up .= "\t\t" . '$this->dbforge->create_table("' . $table . '", TRUE);' . "\n"; 230 | if (isset($engines['Engine']) and $engines['Engine']) 231 | $up .= "\t\t" . '$this->db->query(\'ALTER TABLE ' . $this->ci->db_master->protect_identifiers($table) . ' ENGINE = ' . $engines['Engine']. '\');'; 232 | 233 | 234 | $down .= "\t\t" . '### Drop table ' . $table . ' ##' . "\n"; 235 | $down .= "\t\t" . '$this->dbforge->drop_table("' . $table . '", TRUE);' . "\n"; 236 | 237 | /* clear some mem */ 238 | $q->free_result(); 239 | } 240 | 241 | ### generate the text ## 242 | $return .= 'write_file) 257 | { 258 | fwrite($file, $return); 259 | fclose($file); 260 | echo "Create file migration with success!"; 261 | return true; 262 | } else 263 | { 264 | return $return; 265 | } 266 | } 267 | 268 | } 269 | 270 | ?> 271 | --------------------------------------------------------------------------------