From 139e3c9149e3f6df52948f9dc6873ee4ac3f9e48 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:29:29 +0000 Subject: [PATCH 1/7] chore: Add SqliteBackupCommand for SQLite database backup --- src/Commands/SqliteBackupCommand.php | 45 +++++++++++++++++++ ...Command.php => SqliteWalEnableCommand.php} | 2 +- src/SqliteOptimizeServiceProvider.php | 6 ++- tests/TestCase.php | 2 +- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/Commands/SqliteBackupCommand.php rename src/Commands/{SqliteOptimizeCommand.php => SqliteWalEnableCommand.php} (98%) diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php new file mode 100644 index 0000000..16a3813 --- /dev/null +++ b/src/Commands/SqliteBackupCommand.php @@ -0,0 +1,45 @@ +timestamp.'.sql'; + $this->info('Starting SQLite backup...'); + + try { + // Create a backup of the SQLite database + File::copy( $database, database_path('backups/'.$filename)); + + $this->info('SQLite backup created successfully at: ' . database_path('backups/'.$filename)); + 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'; From 8b124d3eafdc4a12f24dbd396448359fd72d43e9 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:29:57 +0000 Subject: [PATCH 2/7] Fix styling --- src/Commands/SqliteBackupCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php index 16a3813..b510215 100644 --- a/src/Commands/SqliteBackupCommand.php +++ b/src/Commands/SqliteBackupCommand.php @@ -1,6 +1,7 @@ info('SQLite backup created successfully at: '.database_path('backups/'.$filename)); - $this->info('SQLite backup created successfully at: ' . database_path('backups/'.$filename)); return Command::SUCCESS; } catch (\Exception $e) { - $this->error('Failed to create SQLite backup: ' . $e->getMessage()); + $this->error('Failed to create SQLite backup: '.$e->getMessage()); + return Command::FAILURE; } } From 49c9348f18b1a67579b3446659f676f788b93f20 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:31:21 +0000 Subject: [PATCH 3/7] chore: Remove unnecessary code and files related to SqliteOptimize package --- phpstan.neon.dist | 2 -- src/Commands/SqliteBackupCommand.php | 11 ++++++----- 2 files changed, 6 insertions(+), 7 deletions(-) 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 index 16a3813..b510215 100644 --- a/src/Commands/SqliteBackupCommand.php +++ b/src/Commands/SqliteBackupCommand.php @@ -1,6 +1,7 @@ info('SQLite backup created successfully at: '.database_path('backups/'.$filename)); - $this->info('SQLite backup created successfully at: ' . database_path('backups/'.$filename)); return Command::SUCCESS; } catch (\Exception $e) { - $this->error('Failed to create SQLite backup: ' . $e->getMessage()); + $this->error('Failed to create SQLite backup: '.$e->getMessage()); + return Command::FAILURE; } } From c0e35d43bd4290a52f82671851a4f14e576a5227 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:33:30 +0000 Subject: [PATCH 4/7] readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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 From 5d9f342dc1ae49b867c266692e9c3008c27d6695 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:36:40 +0000 Subject: [PATCH 5/7] chore: Create backup directory if it doesn't exist --- src/Commands/SqliteBackupCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php index b510215..39b9967 100644 --- a/src/Commands/SqliteBackupCommand.php +++ b/src/Commands/SqliteBackupCommand.php @@ -29,6 +29,12 @@ 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::ensureDirectoryExists($backupPath)) { + File::makeDirectory($backupPath); + } try { // Create a backup of the SQLite database From dff41c3eefa4fcb7aa99c3b7c78135a275ebec2d Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:37:00 +0000 Subject: [PATCH 6/7] chore: Create backup directory if it doesn't exist --- src/Commands/SqliteBackupCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php index 39b9967..762b4f0 100644 --- a/src/Commands/SqliteBackupCommand.php +++ b/src/Commands/SqliteBackupCommand.php @@ -33,6 +33,7 @@ public function handle(): int // Create the backup directory if it doesn't exist if (! File::ensureDirectoryExists($backupPath)) { + $this->info('Backup directory created at: '.$backupPath); File::makeDirectory($backupPath); } From 00ce301736500f2fd29218f357f95fa1acc857d2 Mon Sep 17 00:00:00 2001 From: arnebr Date: Sun, 16 Jun 2024 09:39:11 +0000 Subject: [PATCH 7/7] chore: Refactor SqliteBackupCommand to improve backup directory creation and file path handling --- src/Commands/SqliteBackupCommand.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Commands/SqliteBackupCommand.php b/src/Commands/SqliteBackupCommand.php index 762b4f0..e11c4f6 100644 --- a/src/Commands/SqliteBackupCommand.php +++ b/src/Commands/SqliteBackupCommand.php @@ -32,16 +32,17 @@ public function handle(): int $backupPath = database_path('backups'); // Create the backup directory if it doesn't exist - if (! File::ensureDirectoryExists($backupPath)) { + if (File::isDirectory($backupPath)) { $this->info('Backup directory created at: '.$backupPath); File::makeDirectory($backupPath); } try { // Create a backup of the SQLite database - File::copy($database, database_path('backups/'.$filename)); + $file_path = database_path('backups/'.$filename); + File::copy($database, $file_path); - $this->info('SQLite backup created successfully at: '.database_path('backups/'.$filename)); + $this->info('SQLite backup created successfully at: '.$file_path); return Command::SUCCESS; } catch (\Exception $e) {