Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created March 13, 2012 16:16
Show Gist options
  • Select an option

  • Save cviebrock/2029682 to your computer and use it in GitHub Desktop.

Select an option

Save cviebrock/2029682 to your computer and use it in GitHub Desktop.
Laravel database init task?
<?php
/**
* /application/tasks/db.php
*/
class DB_Task {
/**
* php artisan db:init [connection] [user]
*/
public function init($args)
{
$connection = Config::get('database.default');
$run_as_user = 'root';
switch (count($args)) {
case 0:
break;
case 1:
$connection = $args[0];
break;
case 2:
default:
$connection = $args[0];
$run_as_user = $args[1];
break;
}
$db = Config::get('database.connections.'.$connection);
if (!$db) {
throw new Exception ("Database connection '$connection' does not exist.");
}
$commands = array(
sprintf('CREATE DATABASE %s',
$db['database'] ),
sprintf('GRANT ALL ON %s.* TO %s@localhost INDENTIFIED BY "%s"',
$db['database'],
$db['username'],
$db['password'] )
);
// Do it ... (just echo commands for now)
echo 'mysql -u '.$run_as_user.' -p -e "' .
addslashes(join("; ", $commands)) . ';"';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment