-
Notifications
You must be signed in to change notification settings - Fork 8
/
Sqltoci.php
348 lines (292 loc) · 11.2 KB
/
Sqltoci.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
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* CI Migrations Generator Library
*
* Create a base file for migrations to start off with;
*
* @author Fastworkx S.R.L. <[email protected]>
* @license Free to use and abuse
* @version 0.1.0 Beta
*
*/
class Sqltoci {
var $db_user;
var $db_pass;
var $db_host;
var $db_name;
var $email;
var $tables = '*';
var $newline = '\n';
var $write_file = true;
var $file_name = '';
var $file_per_table = true;
var $path = 'migrations';
var $skip_tables = array();
var $add_view = false;
/*
* defaults;
*/
function __construct($params = null) {
// parent::__construct();
isset($this->ci) OR $this->ci = get_instance();
$this->ci->db_master = $this->ci->db;
$this->db_user = $this->ci->db_master->username;
$this->db_pass = $this->ci->db_master->password;
$this->db_host = $this->ci->db_master->hostname;
$this->db_name = $this->ci->db_master->database;
$this->path = APPPATH . $this->path;
if ($params)
$this->init_config($params);
}
/**
* Init Config if there is any passed
*
*
* @param type $params
*/
function init_config($params = array()) { //apply config
if (count($params) > 0)
{
foreach ($params as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
/**
* Generate the file.
*
* @param string $tables
* @return boolean|string
*/
function generate($tables = null) {
if ($tables)
$this->tables = $tables;
$return = '';
/* open file */
if ($this->write_file)
{
if (!is_dir($this->path) OR !is_really_writable($this->path))
{
$msg = "Unable to write migration file: " . $this->path;
log_message('error', $msg);
echo $msg;
return;
}
if (!$this->file_per_table)
{
$file_path = $this->path . '/' . $this->file_name . '.sql';
$file = fopen($file_path, 'w+');
if (!$file)
{
$msg = "no file";
log_message('error', $msg);
echo $msg;
return FALSE;
}
}
}
// if default, then run all tables, otherwise just do the list provided
if ($this->tables == '*')
{
$query = $this->ci->db_master->query('SHOW full TABLES FROM ' . $this->ci->db_master->protect_identifiers($this->ci->db_master->database));
$retval = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$tablename = 'Tables_in_' . $this->ci->db_master->database;
if (isset($row[$tablename]))
{
/* check if table in skip arrays, if so, go next */
if (in_array($row[$tablename], $this->skip_tables))
continue;
/* check if views to be migrated */
if ($this->add_view)
{
## not implemented ##
//$retval[] = $row[$tablename];
} else
{
/* skip views */
if (strtolower($row['Table_type']) == 'view')
{
continue;
}
$retval[] = $row[$tablename];
}
}
}
}
$this->tables = array();
$this->tables = $retval;
} else
{
$this->tables = is_array($tables) ? $tables : explode(',', $tables);
}
## if write file, check if we can
if ($this->write_file)
{
/* make subdir */
$path = $this->path . '/' . $this->file_name;
if (!@is_dir($path))
{
if (!@mkdir($path, DIR_WRITE_MODE, true))
{
return FALSE;
}
@chmod($path, DIR_WRITE_MODE);
}
if (!is_dir($path) OR !is_really_writable($path))
{
$msg = "Unable to write backup per table file: " . $path;
log_message('error', $msg);
return;
}
//$file_path = $path . '/001_create_' . $table . '.php';
$file_path = $path . '/001_create_base.php';
$file = fopen($file_path, 'w+');
if (!$file)
{
$msg = 'No File';
log_message('error', $msg);
echo $msg;
return FALSE;
}
}
$up = '';
$down = '';
//loop through tables
foreach ($this->tables as $table)
{
log_message('debug', print_r($table, true));
$q = $this->ci->db_master->query('describe ' . $this->ci->db_master->protect_identifiers($this->ci->db_master->database . '.' . $table));
// No result means the table name was invalid
if ($q === FALSE)
{
continue;
}
$columns = $q->result_array();
$q = $this->ci->db_master->query(' SHOW TABLE STATUS WHERE Name = \'' . $table . '\'');
$engines = $q->row_array();
$key = '';
$up .= PHP_EOL."\n\t\t" . '## Create Table ' . $table . "\n";
$up .= "\t\t" . '$this->dbforge->add_field(array(';
foreach ($columns as $column)
{
$column_type = '';
$column_constraint = '';
$column_null = ( $column['Null'] == 'NO' ? 'FALSE' : 'TRUE' );
$column_default = $column['Default'];
$column_unsigned = FALSE;
$unsigned = 'unsigned';
//si tiene constraint
if ( strpos($column['Type'], '(') )
{
//verificar si tiene 'unsigned'
if( strpos($column['Type'], $unsigned) )
{
$column_unsigned = $unsigned;
$column['Type'] = substr( $column['Type'], 0, strpos( $column['Type'], ')' ) );
}
$column_type = strtoupper( substr($column['Type'], 0, strpos($column['Type'], '(') ) );
//si tiene un valores enum o set
if( $column_type == 'ENUM' || $column_type == 'SET' )
{
//reemplazamos comilla simple por doole
$column['Type'] = str_replace('\'', '"', substr( $column['Type'], strpos( $column['Type'], '(') ) );
//concadenamos
$column_type = $column_type.$column['Type'];
$column_constraint = FALSE;
}
else
{
$column['Type'] = substr( $column['Type'], strpos( $column['Type'], '(') + 1 );
$column_constraint = substr($column['Type'], 0, -1 );
}
}
else
{
$column_type = strtoupper( $column['Type'] );
$column_constraint = FALSE;
}
//si tiene DEAFAULT generar sql texto plano para escapar el strin en caso e.g. CURRENT_TIMESTAMP
if( $column_default == 'CURRENT_TIMESTAMP' )
{
$up .= PHP_EOL."\t\t\t"."'`$column[Field]` $column[Type] " . ($column['Null'] == 'NO' ? 'NOT NULL' : 'NULL') .
(
# if its timestamp column, don't '' around default value .... crap way, but should work for now
$column['Default'] ? ' DEFAULT ' . ($column['Type'] == 'timestamp' ? $column['Default'] : '' . $column['Default'] . '') : ''
)
. " $column[Extra]',";
}
else
{
$up .= PHP_EOL.
"\t\t\t".'\'' . $column['Field'] . '\' => array('.PHP_EOL.
"\t\t\t\t".'\'type\' => \'' . $column_type . '\','.PHP_EOL.
(
$column_constraint ?
"\t\t\t\t".'\'constraint\' => ' . $column_constraint . ','.PHP_EOL :
''
).
(
$column_unsigned ?
"\t\t\t\t".'\''.$unsigned.'\' => TRUE,'.PHP_EOL :
''
).
"\t\t\t\t".'\'null\' => ' . $column_null . ','.PHP_EOL.
(
$column_default != NULL ?
"\t\t\t\t".'\'default\' => \'' . $column_default . '\','.PHP_EOL :
''
).
(
$column['Extra'] ?
"\t\t\t\t".'\''.$column['Extra'].'\' => TRUE'.PHP_EOL :
''.PHP_EOL
).
"\t\t\t".'),';
}
if ($column['Key'] == 'PRI')
$key = "\t\t" . '$this->dbforge->add_key("' . $column['Field'] . '",true);';
}
$up .= PHP_EOL."\t\t));". PHP_EOL.$key.PHP_EOL."\t\t" . '$this->dbforge->create_table("' . $table . '", TRUE);' . PHP_EOL;
if (isset($engines['Engine']) and $engines['Engine'])
$up .= "\t\t" . '$this->db->query(\'ALTER TABLE ' . $this->ci->db_master->protect_identifiers($table) .
' ENGINE = ' . $engines['Engine']. '\');';
$down .= "\t\t" . '### Drop table ' . $table . ' ##' . "\n";
$down .= "\t\t" . '$this->dbforge->drop_table("' . $table . '", TRUE);' . "\n";
/* clear some mem */
$q->free_result();
}
### generate the text ##
$return .= '<?php ';
$return .= 'defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
$return .= 'class Migration_create_base extends CI_Migration {' . "\n";
$return .= "\n\t" . 'public function up() {';
$return .= $up;
$return .= "\n\t" . ' }' . "\n";
$return .= "\n\t" . 'public function down()';
$return .= "\t" . '{' . "\n";
$return .= $down . "\n";
$return .= "\t" . '}' . "\n" . '}';
## write the file, or simply return if write_file false
if ($this->write_file)
{
fwrite($file, $return);
fclose($file);
echo "Create file migration with success!";
return true;
} else
{
return $return;
}
}
}
?>