Skip to content

Commit

Permalink
Merge pull request #2 from ahoicloud/feat/optimize
Browse files Browse the repository at this point in the history
Add ability to create a database backup
  • Loading branch information
arnebr authored Jun 16, 2024
2 parents a21cac8 + 00ce301 commit d4f0769
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ php artisan sqlite:wal-enable sqlite

Where `sqlite` is the connection name.

### Perform database backup
```php
php artisan sqlite:backup
```

Puts a backup for the sqlite file in the directory database_path(backup/) path

## Testing

```bash
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ parameters:
level: 5
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
54 changes: 54 additions & 0 deletions src/Commands/SqliteBackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace AHOI\SqliteOptimize\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class SqliteBackupCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sqlite:backup';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Backup SQLite database';

/**
* Execute the console command.
*/
public function handle(): int
{
$database = config('database.connections.sqlite.database');
$filename = 'backup-'.now()->timestamp.'.sql';
$this->info('Starting SQLite backup...');
$backupPath = database_path('backups');

// Create the backup directory if it doesn't exist
if (File::isDirectory($backupPath)) {
$this->info('Backup directory created at: '.$backupPath);
File::makeDirectory($backupPath);
}

try {
// Create a backup of the SQLite database
$file_path = database_path('backups/'.$filename);
File::copy($database, $file_path);

$this->info('SQLite backup created successfully at: '.$file_path);

return Command::SUCCESS;
} catch (\Exception $e) {
$this->error('Failed to create SQLite backup: '.$e->getMessage());

return Command::FAILURE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Database\SQLiteConnection;
use LogicException;

class SqliteOptimizeCommand extends Command
class SqliteWalEnableCommand extends Command
{
/**
* The name and signature of the console command.
Expand Down
6 changes: 4 additions & 2 deletions src/SqliteOptimizeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace AHOI\SqliteOptimize;

use AHOI\SqliteOptimize\Commands\SqliteOptimizeCommand;
use AHOI\SqliteOptimize\Commands\SqliteBackupCommand;
use AHOI\SqliteOptimize\Commands\SqliteWalEnableCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -17,6 +18,7 @@ public function configurePackage(Package $package): void
*/
$package
->name('laravel-sqlite-optimize')
->hasCommand(SqliteOptimizeCommand::class);
->hasCommand(SqliteWalEnableCommand::class)
->hasCommand(SqliteBackupCommand::class);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function getPackageProviders($app)

public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
//config()->set('database.default', 'testing');

/*
$migration = include __DIR__.'/../database/migrations/create_laravel-sqlite-optimize_table.php.stub';
Expand Down

0 comments on commit d4f0769

Please sign in to comment.