Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Last active August 29, 2015 14:03
Show Gist options
  • Save wayneashleyberry/836f37728adb5e860540 to your computer and use it in GitHub Desktop.
Save wayneashleyberry/836f37728adb5e860540 to your computer and use it in GitHub Desktop.
sqlite helper commands

Usage:

❯ php artisan sqlite:clean --touch
<?php
Artisan::add(new SqliteCleanCommand);
Artisan::add(new SqliteTouchCommand);
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class SqliteCleanCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'sqlite:clean';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove the sqlite database file.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
if (App::environment() !== 'local') {
$this->error('Refusing to run in non-local environment.');
exit;
}
$file = Config::get('database.connections.sqlite.database');
if (! File::exists($file)) {
$this->info('Database file not found.');
} else {
File::delete($file);
$this->info('Removed file: '.$file);
}
if ($this->option('touch')) {
$this->call('sqlite:touch');
}
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['touch', null, InputOption::VALUE_NONE, 'Touch the database file after cleaning it.', null],
];
}
}
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class SqliteTouchCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'sqlite:touch';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create the local sqlite database file.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
if (App::environment() !== 'local') {
$this->error('Refusing to run in non-local environment.');
exit;
}
$file = Config::get('database.connections.sqlite.database');
if (File::exists($file)) {
return $this->info('Database file already exists.');
}
File::put($file, '');
$this->info('Created file: '.$file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment