Skip to content

Instantly share code, notes, and snippets.

@imvision
Created October 26, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imvision/09258082d469f5bc21d8dcdcdb5baa99 to your computer and use it in GitHub Desktop.
Save imvision/09258082d469f5bc21d8dcdcdb5baa99 to your computer and use it in GitHub Desktop.
GUI for CI Migrations
<?php
class Tools extends CI_Controller {
public function __construct() {
parent::__construct();
// can only be called from the command line
if (!$this->input->is_cli_request()) {
exit('Direct access is not allowed. This is a command line tool, use the terminal');
}
$this->load->dbforge();
// initiate faker
$this->faker = Faker\Factory::create();
}
public function message($to = 'World') {
echo "Hello {$to}!" . PHP_EOL;
}
public function help() {
$result = "The following are the available command line interface commands\n\n";
$result .= "php index.php tools migration \"file_name\" Create new migration file\n";
$result .= "php index.php tools migrate [\"version_number\"] Run all migrations. The version number is optional.\n";
$result .= "php index.php tools seeder \"file_name\" Creates a new seed file.\n";
$result .= "php index.php tools seed \"file_name\" Run the specified seed file.\n";
echo $result . PHP_EOL;
}
public function migration($name) {
$this->make_migration_file($name);
}
public function migrate($version = null) {
$this->load->library('migration');
if ($version != null) {
if ($this->migration->version($version) === FALSE) {
show_error($this->migration->error_string());
} else {
echo "Migrations run successfully" . PHP_EOL;
}
return;
}
if ($this->migration->latest() === FALSE) {
show_error($this->migration->error_string());
} else {
echo "Migrations run successfully" . PHP_EOL;
}
}
public function seeder($name) {
$this->make_seed_file($name);
}
public function seed($name) {
$seeder = new Seeder();
$seeder->call($name);
}
protected function make_migration_file($name) {
$date = new DateTime();
$timestamp = $date->format('YmdHis');
$table_name = strtolower($name);
$path = APPPATH . "database/migrations/$timestamp" . "_" . "$name.php";
$my_migration = fopen($path, "w") or die("Unable to create migration file!");
$migration_template = "<?php
class Migration_$name extends CI_Migration {
public function up() {
\$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'auto_increment' => TRUE
)
));
\$this->dbforge->add_key('id', TRUE);
\$this->dbforge->create_table('$table_name');
}
public function down() {
\$this->dbforge->drop_table('$table_name');
}
}";
fwrite($my_migration, $migration_template);
fclose($my_migration);
echo "$path migration has successfully been created." . PHP_EOL;
}
protected function make_seed_file($name) {
$path = APPPATH . "database/seeds/$name.php";
$my_seed = fopen($path, "w") or die("Unable to create seed file!");
$seed_template = "<?php
class $name extends Seeder {
private \$table = 'users';
public function run() {
\$this->db->truncate(\$this->table);
//seed records manually
\$data = [
'user_name' => 'admin',
'password' => '9871'
];
\$this->db->insert(\$this->table, \$data);
//seed many records using faker
\$limit = 33;
echo \"seeding \$limit user accounts\";
for (\$i = 0; \$i < \$limit; \$i++) {
echo \".\";
\$data = array(
'user_name' => \$this->faker->unique()->userName,
'password' => '1234',
);
\$this->db->insert(\$this->table, \$data);
}
echo PHP_EOL;
}
}
";
fwrite($my_seed, $seed_template);
fclose($my_seed);
echo "$path seeder has successfully been created." . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment