Skip to content

Commit 8a06b14

Browse files
committed
add database dump command
1 parent b70f885 commit 8a06b14

File tree

4 files changed

+199
-13
lines changed

4 files changed

+199
-13
lines changed

README.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,59 @@ php artisan cmd:db:drop databasename
3434
You can use ***--help*** option to have more informations about this command
3535

3636

37+
### Seed Command
38+
39+
```bash
40+
php artisan cmd:db:seed
41+
```
42+
43+
If you want to run a specific class you can use ***--class*** option
44+
45+
```bash
46+
php artisan cmd:db:seed --class=UsersTableSeeder
47+
```
48+
You can use ***--help*** option to have more informations about this command
49+
50+
51+
### Dump Command
52+
53+
```bash
54+
php artisan cmd:db:dump
55+
```
56+
Available drivers are ***mysql, pgsql, sqlite*** and default driver is ***mysql***
57+
58+
Host can be changed with ***--host*** option
59+
60+
```bash
61+
php artisan cmd:db:dump --host sqlite
62+
```
63+
64+
Username can be changed with ***--dbusername*** option
65+
66+
```bash
67+
php artisan cmd:db:dump --dbusername root
68+
```
69+
70+
Password can be changed with ***--dbpassword*** option
71+
72+
```bash
73+
php artisan cmd:db:dump --dbpassword root
74+
```
75+
76+
Database name can be changed with ***--dbname*** option
77+
78+
```bash
79+
php artisan cmd:db:dump --dbname databasename
80+
```
81+
82+
Dump filename can be changed with ***--filename*** option
83+
84+
```bash
85+
php artisan cmd:db:dump --filename dump.sql
86+
```
87+
88+
You can use ***--help*** option to have more informations about this command
89+
3790
### Trait Command
3891

3992
```bash
@@ -75,18 +128,6 @@ php artisan cmd:make:helper helpername --folder={folder}
75128
```
76129
You can use ***--help*** option to have more informations about this command
77130

78-
### Seed Command
79-
80-
```bash
81-
php artisan cmd:db:seed
82-
```
83-
84-
If you want to run a specific class you can use ***--class*** option
85-
86-
```bash
87-
php artisan cmd:db:seed --class=UsersTableSeeder
88-
```
89-
You can use ***--help*** option to have more informations about this command
90131

91132
### Security
92133

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
],
1717
"require": {
1818
"php": ">=7.2",
19-
"illuminate/support": "^6.0|^7.0|^8.0"
19+
"illuminate/support": "^6.0|^7.0|^8.0",
20+
"spatie/laravel-db-snapshots": "^2.0"
2021
},
2122
"require-dev": {
2223
"orchestra/testbench": "^4.0|^5.0|^6.0",
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
4+
namespace Guysolamour\Command\Console\Commands\Database;
5+
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
8+
use Illuminate\Console\Command;
9+
use Spatie\DbDumper\Databases\MySql;
10+
use Spatie\DbDumper\Databases\Sqlite;
11+
use Spatie\DbDumper\Databases\PostgreSql;
12+
13+
class DumpCommand extends Command
14+
{
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = "cmd:db:dump
21+
{--d|driver= : Database driver. Available drivers are ['mysql', 'pgsql', 'sqlite']}
22+
{--host=127.0.0.1 : Database host}
23+
{--dbusername= : Database user}
24+
{--dbpassword= : Database password}
25+
{--dbname= : Database name}
26+
{--f|filename=dump.sql : Dump filename}
27+
";
28+
29+
/**
30+
* The console command description.
31+
*
32+
* @var string
33+
*/
34+
protected $description = 'Create database dump with different drivers.';
35+
36+
/**
37+
* Suported database drivers
38+
*
39+
* @var string[]
40+
*/
41+
private const DRIVERS = ['mysql', 'pgsql', 'sqlite'];
42+
43+
/**
44+
* Execute the console command.
45+
*
46+
* @return int
47+
*/
48+
public function handle()
49+
{
50+
$this->line("Dumping {$this->getDbName()} database....");
51+
$this->dumpDb();
52+
$this->line("{$this->getDbName()} database dumped successfully.");
53+
}
54+
55+
private function dumpDb()
56+
{
57+
$dumper = $this->getDumper()->setDbName($this->getDbName());
58+
59+
if (!$this->isSqliteDriver()) {
60+
$dumper
61+
->setDbName($this->getDbName())
62+
->setUserName($this->getUserName())
63+
->setPassword($this->getPassword())
64+
->setHost($this->getHost());
65+
}
66+
67+
$dumper->dumpToFile($this->getDumpFilename());
68+
}
69+
70+
private function isSqliteDriver(): bool
71+
{
72+
return $this->getDriver() === 'sqlite';
73+
}
74+
75+
private function getDumper(): \Spatie\DbDumper\DbDumper
76+
{
77+
$driver = $this->getDriver();
78+
79+
switch ($driver) {
80+
case 'mysql':
81+
return MySql::create();
82+
break;
83+
case 'pgsql':
84+
return PostgreSql::create();
85+
break;
86+
case 'sqlite':
87+
return Sqlite::create();
88+
break;
89+
}
90+
}
91+
92+
private function getDriver(): ?string
93+
{
94+
$driver = $this->option('driver') ?? config('database.default');
95+
96+
if (!in_array($driver, self::DRIVERS)) {
97+
$this->error(sprintf("Current [%s] driver is not available. Available drivers are [%s].", $driver, join(', ', self::DRIVERS)));
98+
}
99+
100+
return Str::lower($driver);
101+
}
102+
103+
private function getHost(): ?string
104+
{
105+
return $this->option('host');
106+
}
107+
108+
private function getDumpFilename(): string
109+
{
110+
return $this->option('filename');
111+
}
112+
113+
private function getUserName(): ?string
114+
{
115+
return $this->option('dbusername') ?? $this->getDefaultDatabaseConnection('username');
116+
}
117+
118+
private function getPassword(): string
119+
{
120+
return $this->option('dbpassword') ?? $this->getDefaultDatabaseConnection('password');
121+
}
122+
123+
private function getDbName(): string
124+
{
125+
return $this->option('dbname') ?? $this->getDefaultDatabaseConnection('database');
126+
}
127+
128+
private function getDefaultDatabaseConnection(?string $key = null, $default = null)
129+
{
130+
$config = config("database.connections.{$this->getDriver()}", []);
131+
132+
if (empty($config)) {
133+
return $config;
134+
}
135+
136+
if (is_null($key)) {
137+
return $config;
138+
}
139+
140+
return Arr::get($config, $key, $default);
141+
}
142+
}

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Guysolamour\Command\Console\Commands\Database\DropDatabase;
66
use Guysolamour\Command\Console\Commands\Database\CreateDatabase;
7+
use Guysolamour\Command\Console\Commands\Database\DumpCommand;
78
use Guysolamour\Command\Console\Commands\Database\SeedCommand;
89
use Guysolamour\Command\Console\Commands\Helper\CreateHelperCommand;
910
use Guysolamour\Command\Console\Commands\Traits\CreateTraitCommand;
@@ -27,6 +28,7 @@ public function boot()
2728
CreateProviderCommand::class,
2829
CreateHelperCommand::class,
2930
SeedCommand::class,
31+
DumpCommand::class,
3032
]);
3133
}
3234
}

0 commit comments

Comments
 (0)