/
Migrate.php
722 lines (591 loc) · 19.7 KB
/
Migrate.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
<?php
/**
* Migration Controller Class for Codeigniter
*
* This class provides a Command Line Interface
* for developers to use Codeigniter's Migrations
* functionality. It has been configured to only
* respond to CLI requests hence not accessible
* via the browser. However running it via a browser
* is very much possible with a few tweaks in the code
* A detailed info about the available methods
* can be found via $this::help()
*
* No configurations are required before using this
* class as it configures everything requried by
* itself apart from a working database connection.
*
* @package Migrations for Codeigniter
* @subpackage
* @category
* @author Muhammad Aimal <aimal.azmi.13@gmail.com>
* @link
*/
class Migrate extends CI_Controller
{
/**
* Migration File Name
*
* @var string
*/
protected $_migration_file_name = '';
/**
* Migration Path
*
* @var string
*/
protected $_migration_path = '';
/**
* Database table with migration info
*
* @var string
*/
protected $_migration_table = 'migrations';
/**
* Table name to be used in the migration file
*
* @var string
*/
protected $_migration_file_table = '';
/**
* Class Version Info
*
* @var string
*/
protected $_class_version = '1.0';
/**
* Info file
*
* @var string
*/
protected $_info_file_name = 'migrate_help.txt';
/**
* Class constructor
*
* @return void
*/
public function __construct()
{
parent::__construct();
// The migration controller will only be accessible
// throught the CLI.
if (is_cli() === FALSE)
{
show_404();
}
// Load the Helper classes
$this->load->helper('file');
// Check whether migrations are turned off
$this->_migrations_status();
// Load the libraries
$this->load->library('migration');
// Set the Migration Path to the migration folder
$this->_migration_path = $this->config->item('migration_path');
// Check if the application/migrations folder
// exist and create it if it doesnot
if ( ! is_dir($this->_migration_path))
{
// The migration directory wasnot found
echo 'The application/migrations folder doesnot exist' . PHP_EOL;
echo 'Creating the application/migrations folder...' . PHP_EOL;
if ( ! mkdir($this->_migration_path))
{
// There was an eror creating the path
show_error('Could not create the directory application/migrations! Try again and if the error persists try creating it manually!');
}
// Directory created successfully
echo 'Directory application/migrations created successfully!' . PHP_EOL;
}
// Customize the migrations table
// created by CI to suit our needs
$this->_customize_migrations_table();
}
// --------------------------------------------------------------------
/**
* Creates Migration Files
*
* @param string $file_name Migration File Name
* @return void
*/
public function index( $file_name = '' )
{
echo 'Enter migration file name: ';
$file_name = trim(fgets(STDIN));
// Check if a valid File Name was passed
if (empty($file_name) OR $file_name === "")
{
show_error('Please enter a valid migration file name');
}
// Set the value
$this->_migration_file_name = $file_name;
echo 'Database Table name: ';
$migration_file_table = trim(fgets(STDIN));
// Check if a valid File Name was passed
if (empty($migration_file_table) OR $migration_file_table === "")
{
show_error('Please enter a valid table name for the migration file!');
}
// Migration File table name
$this->_migration_file_table = $migration_file_table;
// Format the file name
$file_name = $this->_format_file_name($file_name);
// Get the migration version
$migration_version = $this->_get_migration_version();
// Append the migration number/timestamp
// to the file name
$file_full_name = $migration_version . '_' . $file_name . '.php';
// Create the complete path to the file
$file = $this->_migration_path . $file_full_name;
// Get the default migration file content
$file_content = $this->_get_file_content($file_name);
// Create the file and throw error if could not create
if ( ! write_file($file, $file_content))
{
// Error creating the file
$error = 'Could not create file!' . PHP_EOL;
show_error($error);
}
// Update the migration version in Schema
$this->_update_latest_file_version($migration_version);
// File created successfully
echo 'Migration File "' . $file_full_name . '" successfully created!' . PHP_EOL;
return;
}
// --------------------------------------------------------------------
/**
* Calls the Migration::current()
*
* @return void
*/
public function current()
{
if ($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
echo "Migrated to the current version!";
}
// --------------------------------------------------------------------
/**
* Calls the Migration::latest()
*
* @return void
*/
public function latest()
{
if ($this->migration->latest() === FALSE)
{
show_error($this->migration->error_string());
}
echo "Migrated to the latest version!";
}
// --------------------------------------------------------------------
/**
* Rollback to a migration version
*
* @param int $version Rollback Migration Version
* @return void
*/
public function rollback($version = 0)
{
if ($this->migration->version(abs($version)) === FALSE)
{
show_error($this->migration->error_string());
}
echo "Migrated to the version - " . $version;
}
// --------------------------------------------------------------------
/**
* Resets the migration table and
* Deletes all the migartion files
* Deletes all the tables created
* by the migrations aswell
*
* @param bool $rollback rollback to zero
* @return void
*/
public function reset($rollback = TRUE)
{
$this->_prompt("This function will delete all the files in the migrations folder,".
" delete all relevant tables and also reset the migrations table in DB.");
// Rollback migrations to version '0'
if (($rollback === TRUE) && ($this->db->table_exists($this->_migration_table) === TRUE))
{
if ($this->rollback(0) === FALSE)
{
show_error("There was an error resetting the migrations!");
}
}
// DROP the migartions table
if ($this->dbforge->drop_table($this->_migration_table,TRUE) === FALSE )
{
show_error("Table 'Migrations' could not be deleted!");
}
//Delete all the migartion files
if (delete_files($this->_migration_path) === FALSE)
{
show_error("Could not all the migration files!");
}
echo "Migartions have been reset!";
}
// --------------------------------------------------------------------
/**
* Rollbacks the migrations one step back
*
* @return void
*/
public function last()
{
$rollback_version = $this->_get_version() == 0 ? 0 : (int) ($this->_get_version() - 1);
if ($rollback_version)
{
$this->rollback($rollback_version);
}
else
{
show_error("Looks like your migrations table hasn't been configured properly!" .
" Try reseting the migarations using migrate::reset() and try again!");
}
}
// --------------------------------------------------------------------
/**
* Information about using this class
*
* @return void
*/
public function help()
{
if ( ! file_exists(__DIR__ . '/' . $this->_info_file_name))
show_error("Couldn't find the info file!");
$file_content = file_get_contents(__DIR__ . '/' . $this->_info_file_name);
if ($file_content)
{
$file_content = str_replace('#', $this->_class_version, $file_content);
echo $file_content;
}
}
// --------------------------------------------------------------------
/**
* Information about the migrations
*
* @return void
*/
public function info()
{
$content = 'No migrations found!';
$headings = ['Ran','Migration'];
$migrations = get_filenames($this->_migration_path);
if ((is_array($migrations)) && (count($migrations) > 0))
{
$row_width = 0;
$content = '';
$header = '';
$rows = [];
$version = $this->_get_version('version');
foreach($headings as $key => $value)
{
$header .= '| ' . $value . ' ';
}
foreach ($migrations as $key => $value)
{
if ( ! empty($value))
{
$string = '| T | ';
if ( (int) $this->_get_migration_number($value) > $version)
{
$string = '| F | ';
}
$string = $string . str_replace('.php', '', $value) . ' ';
$rows[] = $string;
$row_width = (strlen($string) > $row_width) ? strlen($string) : $row_width;
}
}
if (count($rows) > 0)
{
foreach ($rows as $row)
{
$content .= str_pad($row, $row_width, " ", STR_PAD_RIGHT) . '|' . PHP_EOL;
}
// Lower layer
$content = $content . '+' . str_pad("", $row_width - 1, "-", STR_PAD_LEFT) . '+' . PHP_EOL;
}
// Paddings
$header = str_pad($header, $row_width, " ", STR_PAD_RIGHT) . '|' . PHP_EOL;
// Upper layer
$header = '+' . str_pad("", $row_width - 1, "-", STR_PAD_LEFT) . '+' . PHP_EOL . $header;
// Lower layer
$header = $header . '+' . str_pad("", $row_width - 1, "-", STR_PAD_LEFT) . '+' . PHP_EOL;
// Mix it up
$content = PHP_EOL . $header . $content;
}
echo $content;
}
// --------------------------------------------------------------------
/**
* Checks whether the migrations are
* ENABLED or DISABLED in the application
* Enables if disabled after user's permission
*
* @return void
*/
protected function _migrations_status()
{
$migration_config_file = APPPATH . 'config/migration.php';
// Check if config/migration.php exists
if ( ! file_exists($migration_config_file))
{
show_error("It seems the config/migration.php file doesnot exist. That file is necessary!");
}
// Migrations Config File exists, load it
$this->config->load('migration');
// Migration Config file exists
// and migrations are enabled
if ($this->config->item('migration_enabled') === TRUE)
return;
// Recheck if the migrations aren't enabled
if ($this->config->item('migration_enabled') !== TRUE)
{
echo PHP_EOL . 'It looks like the migrations have been disabled in the config file.' . PHP_EOL;
// Ask to enable migrations
$this->_prompt("Enable migrations?");
$migration_file_content = file_get_contents($migration_config_file);
if ( ! empty($migration_file_content))
{
$migration_file_content = str_replace("config['migration_enabled'] = FALSE", "config['migration_enabled'] = TRUE", $migration_file_content);
// write the changes to the file
if ( ! write_file($migration_config_file,$migration_file_content))
{
$error = 'There was an error while configuring changes in the config/migration.php file!';
$error .= PHP_EOL . 'If the error persists simply goto projectfolder/application/config/migration.php' . PHP_EOL;
$error .= ' and set $'.'config[migration_enabled] = TRUE';
show_error($error);
}
// Reload configuration
echo PHP_EOL . 'Reloading Configurations...' . PHP_EOL;
$this->config->set_item('migration_enabled',TRUE);
// Configurations were changed successfully
if ($this->config->item('migration_enabled') === TRUE)
{
echo PHP_EOL . 'Migrations Enabled!' . PHP_EOL . PHP_EOL;
return;
}
}
// If the program has reached till here it
// means that the migration configurations
// were not set properly at all
show_error("Looks like your migration configurations aren't set properly. Try getting a fresh copy of config/migration.php and retry!");
}
}
// --------------------------------------------------------------------
/**
* Performs regex and only allows
* letters and underscore.
*
* @return Mixed. File name on success, show_error() on wrong file name
*/
protected function _customize_migrations_table()
{
// The CI will take care of validating
// and creating the table. We only need
// to alter it to add 'latest_file_version'
// in the table
// Check if the field has already
// been added to the table
if ($this->db->field_exists('latest_file_version',$this->_migration_table) === TRUE)
{
return;
}
$fields = array(
'latest_file_version' => array('type' => 'INT','default' => 0)
);
if ($this->dbforge->add_column($this->_migration_table, $fields) === FALSE)
{
show_error("Coud not add field 'latest_file_version' to the migrations table!");
}
}
// --------------------------------------------------------------------
/**
* Replaces spaces with '_'
*
* @return string
*/
protected function _format_file_name($file_name = '')
{
return str_replace(' ', '_', $file_name);
}
// --------------------------------------------------------------------
/**
* Returns the default code found in the
* migration file. The best way to do this
* is not from a function but since I didn't
* want to create an extra file as sometimes
* it causes developers the hassle to locate
* and validate the files and everything.
*
* However I recommend cutting $file_content
* from the function and and pasting it to a
* file and then returning it from there so
* that this controller doesnt contain this
* extra bit of code
*
* @param string $file_name Name of Migration File
* @return string
*/
protected function _get_file_content($file_name = '')
{
// Php opening tag
$file_content = '<?php' . PHP_EOL . PHP_EOL; // <?php
$file_content .= 'defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . PHP_EOL;
// Class
$file_content .= PHP_EOL . 'class Migration_' . $file_name; // class File_name
$file_content .= ' extends CI_Migration {' . PHP_EOL . PHP_EOL; // extends CI_Migration {
// Private string table name
$file_content .= ' /**' . PHP_EOL;
$file_content .= ' * Name of the table to be used in this migration!' . PHP_EOL;
$file_content .= ' *' . PHP_EOL;
$file_content .= ' * @var string' . PHP_EOL;
$file_content .= ' */' . PHP_EOL;
$file_content .= ' protected $_'.'table_name = "' . trim($this->_migration_file_table) . '";' . PHP_EOL . PHP_EOL;
// Public function up content
$file_content .= ' public function up()' . PHP_EOL;
$file_content .= ' {' . PHP_EOL;
if (strpos($this->_migration_file_name, 'modify') !== FALSE)
{
$file_content .= ' $this->dbforge->add_column($'.'this->_table_name, $'.'this->_fields());' . PHP_EOL;
}
else
{
$file_content .= ' $this->dbforge->add_field(\'id\');' . PHP_EOL;
$file_content .= ' $this->dbforge->add_field("`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");' . PHP_EOL;
$file_content .= ' $this->dbforge->add_field("`updated_at` TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");' . PHP_EOL;
$file_content .= ' $this->dbforge->create_table($'.'this->_table_name, TRUE);' . PHP_EOL;
}
$file_content .= ' }' . PHP_EOL;
$file_content .= PHP_EOL;
// Public function down content
$file_content .= ' public function down()' . PHP_EOL;
$file_content .= ' {' . PHP_EOL;
if (strpos($this->_migration_file_name, 'modify') !== FALSE)
{
$file_content .= ' if (is_array($'.'this->_fields()))' . PHP_EOL;
$file_content .= ' {' . PHP_EOL;
$file_content .= ' foreach($this->_fields() as $key => $val)' . PHP_EOL;
$file_content .= ' {' . PHP_EOL;
$file_content .= ' $this->dbforge->drop_column($'.'this->_table_name,$'.'key);' . PHP_EOL;
$file_content .= ' }' . PHP_EOL;
$file_content .= ' }' . PHP_EOL;
}
else
{
$file_content .= ' $this->dbforge->drop_table($'.'this->_table_name, TRUE);' . PHP_EOL;
}
$file_content .= ' }' . PHP_EOL;
// The file name consists modify
if (strpos($this->_migration_file_name, 'modify') !== FALSE)
{
$file_content .= PHP_EOL;
$file_content .= ' /**' . PHP_EOL;
$file_content .= ' * Returns an array of the fields to be used within the up and down functions!' . PHP_EOL;
$file_content .= ' *' . PHP_EOL;
$file_content .= ' * @return array' . PHP_EOL;
$file_content .= ' */' . PHP_EOL;
$file_content .= ' protected function _fields()' . PHP_EOL;
$file_content .= ' {' . PHP_EOL;
$file_content .= ' return array();' . PHP_EOL;
$file_content .= ' }' . PHP_EOL;
}
// Class closing tag
$file_content .= PHP_EOL . '}' . PHP_EOL . PHP_EOL; // }
// Php closing tag
$file_content .= '?>';
return $file_content;
}
// --------------------------------------------------------------------
/**
* Returns the latest migration version number
*
* @return int $migration_version Migration Version
*/
protected function _get_migration_version()
{
// By default we'll use the timestamps
// for version control
$migration_version = date("YmdHis");
// If the migration type has been set to
// sequential, we'll set the migration
// version to sequential
if ($this->config->item('migration_type') === 'sequential')
{
$migration_version = (int) ( (int) $this->_get_version() + 1);
$migration_version = str_pad($migration_version, 3, "0", STR_PAD_LEFT);
}
return $migration_version;
}
// --------------------------------------------------------------------
/**
* Returns the latest file version
* from the migrations table
*
* @return int last migration file version
* @param string $select Name of row to return
*/
protected function _get_version($select = 'latest_file_version')
{
$row = $this->db->select($select)->get($this->_migration_table)->row();
return $row ? $row->$select : '0';
}
// --------------------------------------------------------------------
/**
* Stores the latest file version
*
* @param string $migration Migration reached
* @return void
*/
protected function _update_latest_file_version($file_version)
{
$this->db->update($this->_migration_table, array(
'latest_file_version' => $file_version
));
}
// --------------------------------------------------------------------
/**
* Revere the migration type to the
* given type. i.e. if the migration
* files were sequenced through timestamps
*/
protected function _reverse_migration_type()
{
// Later
}
// --------------------------------------------------------------------
/**
* Prompt Function
*
* @return void
*/
protected function _prompt($msg = '')
{
echo PHP_EOL . $msg . PHP_EOL;
echo "Continue? (Y/N) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y' && $response != 'y') {
echo "Opertaion Terminated!.\n";
exit;
}
}
// --------------------------------------------------------------------
/**
* Extracts the migration number from a filename
*
* @param string $migration
* @return string Numeric portion of a migration filename
*/
protected function _get_migration_number($migration)
{
return sscanf($migration, '%[0-9]+', $number)
? $number : '0';
}
// --------------------------------------------------------------------
}
?>