-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMigration_lib.php
390 lines (314 loc) · 11 KB
/
Migration_lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* User: Ian Jiang
* Date: 2016/10/29
*/
class Migration_lib
{
/**
* @var object $_ci codeigniter
*/
private $_ci = NULL;
/**
* @var string migration folder name
*/
private $_migration_folder_name = 'migrations';
/**
* @var array collect used timestamp
*/
private $_timestamp_set = array();
/**
* @var array migration table set
*/
protected $migration_table_set = [];
/**
* @var string path
*/
protected $path = '';
/**
* @var array skip table name set
*/
protected $skip_tables = ['migrations'];
/**
* @var bool add view
*/
protected $add_view = FALSE;
/**
* @var string database name
*/
private $_db_name = '';
/**
* Migration_lib constructor.
*/
public function __construct()
{
// Get Codeigniter Object
if( ! isset($this->_ci))
{
$this->_ci =& get_instance();
}
$this->path = APPPATH . $this->_migration_folder_name;
$this->_ci->load->database();
}
/**
* Generate Migrations Files
*
* @param string $tables
*
* @return boolean|string
*/
public function generate($tables = '*')
{
// check tables not empty
if(empty($tables))
{
echo 'InvalidParameter::tables';
return FALSE;
}
// get database name
$this->_db_name = $this->_ci->db->database;
if ($tables === '*')
{
$query = $this->_ci->db->query('SHOW FULL TABLES FROM ' . $this->_ci->db->protect_identifiers($this->_db_name));
// collect tables of migration
$migration_table_set = array();
// confirm table num
if ($query->num_rows() > 0)
{
$table_name_key = "Tables_in_{$this->_db_name}";
foreach ($query->result_array() as $table_info)
{
if (isset($table_info[$table_name_key]) && $table_info[$table_name_key] !== '')
{
$table_name = $table_info[$table_name_key];
// check if table in skip arrays, if so, go next
if (in_array($table_info[$table_name_key], $this->skip_tables))
{
continue;
}
// skip views
if (strtolower($table_info['Table_type']) == 'view')
{
continue;
}
$migration_table_set[] = $table_info["Tables_in_{$this->_db_name}"];
}
}
}
if($this->_ci->db->dbprefix($this->_db_name) !== '')
{
array_walk($migration_table_set, [$this, '_remove_database_prefix']);
}
$this->migration_table_set = $migration_table_set;
}
else
{
$this->migration_table_set = is_array($tables) ? $tables : explode(',', $tables);
}
if( ! empty($this->migration_table_set))
{
// create migration file or override it.
foreach ($this->migration_table_set as $table_name)
{
$file_content = $this->get_file_content($table_name);
if(! empty($file_content))
{
$this->write_file($table_name, $file_content);
continue;
}
}
echo "Create migration success!";
return TRUE;
}
else
{
echo "Empty table set!";
return FALSE;
}
}
/**
* _remove_database_prefix
*
* @param string $table_name
*
* @return void
*/
private function _remove_database_prefix(&$table_name)
{
// insensitive replace
$table_name = str_ireplace($this->_ci->db->dbprefix, '', $table_name);
}
/**
* get_file_content
*
* @param string $table_name table name
*
* @return string $file_content
*/
public function get_file_content($table_name)
{
$file_content = '<?php ';
$file_content .= 'defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
$file_content .= "class Migration_create_{$table_name} extends CI_Migration" . "\n";
$file_content .= '{' . "\n";
$file_content .= $this->get_function_up_content($table_name);
$file_content .= $this->get_function_down_content($table_name);
$file_content .= "\n" . '}' . "\n";
// replace tab into 4 space
$file_content = str_replace("\t", ' ', $file_content);
return $file_content;
}
/**
* writeFile
*
* @param string $table_name table name
* @param string $file_content file content
*
* @return void
*/
public function write_file($table_name, $file_content)
{
$file = $this->open_file($table_name);
fwrite($file, $file_content);
fclose($file);
}
/**
* openFile
*
* @param string $table_name table name
*
* @return bool|resource
*/
public function open_file($table_name)
{
$timestamp = $this->_get_timestamp($table_name);
$file_path = $this->path . '/' . $timestamp .'_create_' . $table_name . '.php';
// Open for reading and writing.
// Place the file pointer at the beginning of the file and truncate the file to zero length.
// If the file does not exist, attempt to create it.
$file = fopen($file_path, 'w+');
if ( ! $file)
{
return FALSE;
}
// add this timestamp to timestamp ser
$this->_timestamp_set[] = $timestamp;
return $file;
}
/**
* _get_timestamp get
*
* @param $table_name
* @return string
*/
private function _get_timestamp($table_name)
{
// get timestamp
$query = $this->_ci->db->query(' SHOW TABLE STATUS WHERE Name = \'' . $table_name .'\'');
$engines = $query->row_array();
$timestamp = date('YmdHis', strtotime($engines['Create_time']));
while(in_array($timestamp, $this->_timestamp_set))
{
$timestamp += 1;
}
return $timestamp;
}
/**
* Base on table name create migration up function
*
* @param $table_name
*
* @return string
*/
public function get_function_up_content($table_name)
{
$str = "\n\t" . '/**' . "\n";
$str .= "\t" . ' * up (create table)' . "\n";
$str .= "\t" . ' *' . "\n";
$str .= "\t" . ' * @return void' . "\n";
$str .= "\t" . ' */' . "\n";
$str .= "\t" . 'public function up()' . "\n";
$str .= "\t" . '{' . "\n";
$query = $this->_ci->db->query("SHOW FULL FIELDS FROM {$this->_ci->db->dbprefix($table_name)} FROM {$this->_db_name}");
// 如果没有结果,直接返回
if ($query->result() === NULL)
{
return FALSE;
}
$columns = $query->result_array();//获取列数据
$add_key_str = '';
$add_field_str = "\t\t" . '$this->dbforge->add_field(array(' . "\n";
foreach ($columns as $column)
{
// field name
$add_field_str .= "\t\t\t'{$column['Field']}' => array(" . "\n";
preg_match('/^(\w+)\(([\d]+(?:,[\d]+)*)\)/', $column['Type'], $match);
if($match === [])
{
preg_match('/^(\w+)/', $column['Type'], $match);
}
$add_field_str .= "\t\t\t\t'type' => '" . strtoupper($match[1]) . "'," . "\n";
if(isset($match[2]))
{
switch (strtoupper($match[1]))
{
//type enum need extra handle
case 'ENUM':
$enum_constraint_str = str_replace(',', ', ', $match[2]);
$add_field_str .= "\t\t\t\t'constraint' => [" . $enum_constraint_str . "],\n";
break;
default:
$add_field_str .= "\t\t\t\t'constraint' => '" . strtoupper($match[2]) . "'," . "\n";
break;
}
}
$add_field_str .= (strstr($column['Type'], 'unsigned')) ? "\t\t\t\t'unsigned' => TRUE," . "\n" : '';
$add_field_str .= ((string) $column['Default'] !== '') ? "\t\t\t\t'default' => '" . $column['Default'] . "'," . "\n" : '';
$add_field_str .= ((string) $column['Comment'] !== '') ? "\t\t\t\t'comment' => '" . str_replace("'", "\\'", $column['Comment']) . "',\n" : '';
$add_field_str .= ($column['Null'] !== 'NO') ? "\t\t\t\t'null' => TRUE," . "\n" : '';
$add_field_str .= "\t\t\t)," . "\n";
if ($column['Key'] == 'PRI')
{
$add_key_str .= "\t\t" . '$this->dbforge->add_key("' . $column['Field'] . '", TRUE);' . "\n";
}
}
$add_field_str .= "\t\t));" . "\n";
$str .= "\n\t\t" . '// Add Fields.' . "\n";
$str .= $add_field_str;
$str .= ($add_key_str !== '') ? "\n\t\t" . '// Add Primary Key.' . "\n" . $add_key_str : '';
// create db
$query = $this->_ci->db->query(' SHOW TABLE STATUS WHERE Name = \'' . $table_name . '\'');
$engines = $query->row_array();
$attributes_str = "\n\t\t" . '$attributes = array(' . "\n";;
$attributes_str .= ((string) $engines['Engine'] !== '') ? "\t\t\t'ENGINE' => '" . $engines['Engine'] . "'," . "\n" : '';
$attributes_str .= ((string) $engines['Comment'] !== '') ? "\t\t\t'COMMENT' => '\\'" . str_replace("'", "\\'", $engines['Comment']) . "'\\'',\n" : '';
$attributes_str .= "\t\t" . ');' . "\n";
$str .= "\n\t\t" . '// Table attributes.' . "\n";
$str .= $attributes_str;
$str .= "\n\t\t" . '// Create Table ' . $table_name . "\n";
$str .= "\t\t" . '$this->dbforge->create_table("' . $table_name . '", TRUE, $attributes);' . "\n";
$str .= "\n\t" . '}' . "\n";
return $str;
}
/**
* Base on table name create migration down function
*
* @param string $table_name table name
*
* @return string
*/
public function get_function_down_content($table_name)
{
$function_content = "\n\t" . '/**' . "\n";
$function_content .= "\t" . ' * down (drop table)' . "\n";
$function_content .= "\t" . ' *' . "\n";
$function_content .= "\t" . ' * @return void' . "\n";
$function_content .= "\t" . ' */' . "\n";
$function_content .= "\t" . 'public function down()' . "\n";
$function_content .= "\t" . '{' . "\n";
$function_content .= "\t\t" . '// Drop table ' . $table_name . "\n";
$function_content .= "\t\t" . '$this->dbforge->drop_table("' . $table_name . '", TRUE);' . "\n";
$function_content .= "\t" . '}' . "\n";
return $function_content;
}
}