Created
March 13, 2012 16:16
-
-
Save cviebrock/2029682 to your computer and use it in GitHub Desktop.
Laravel database init task?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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