diff --git a/README.md b/README.md index 579fa1e..7172ceb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ab1b4c3..d8fd46c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -5,8 +5,6 @@ parameters: level: 5 paths: - src - - config - - database tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php new file mode 100644 index 0000000..e11c4f6 --- /dev/null +++ b/src/Commands/SqliteBackupCommand.php @@ -0,0 +1,54 @@ +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; + } + } +} diff --git a/src/Commands/SqliteOptimizeCommand.php b/src/Commands/SqliteWalEnableCommand.php similarity index 98% rename from src/Commands/SqliteOptimizeCommand.php rename to src/Commands/SqliteWalEnableCommand.php index 0a4cad0..6dac165 100644 --- a/src/Commands/SqliteOptimizeCommand.php +++ b/src/Commands/SqliteWalEnableCommand.php @@ -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. diff --git a/src/SqliteOptimizeServiceProvider.php b/src/SqliteOptimizeServiceProvider.php index cdf8bc1..9ea03f9 100644 --- a/src/SqliteOptimizeServiceProvider.php +++ b/src/SqliteOptimizeServiceProvider.php @@ -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; @@ -17,6 +18,7 @@ public function configurePackage(Package $package): void */ $package ->name('laravel-sqlite-optimize') - ->hasCommand(SqliteOptimizeCommand::class); + ->hasCommand(SqliteWalEnableCommand::class) + ->hasCommand(SqliteBackupCommand::class); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 73e58d5..cf2158d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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';