Skip to content

Commit c089a3d

Browse files
authored
Merge pull request #6 from hamidafghan/dybamic-file-name
Make Dynamic file.sql
2 parents 736509b + 33a8c15 commit c089a3d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ You can publish the config file with:
2323

2424
```
2525
php artisan vendor:publish --provider="Dcblogdev\DbSync\DbSyncServiceProvider" --tag="config"
26-
```
26+
```
2727

28-
## .env
28+
## .env
2929

3030
Set the remote database credentials in your .env file
3131

@@ -59,6 +59,8 @@ REMOTE_IMPORT_FILE=true
5959

6060
Set a comma seperate list of tables NOT to export in `REMOTE_DATABASE_IGNORE_TABLES`
6161

62+
To generate a SQL with a custom file name `REMOTE_DEFAULT_FILE_NAME`
63+
6264
## Usage
6365

6466
To export a remote database to OVERRIDE your local database by running:

config/dbsync.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@
4848
'importSqlFile' => env('REMOTE_IMPORT_FILE', 'true'),
4949

5050
/**
51-
* Sets if the generated file.sql will be deleted after it has been imported.
51+
* Sets if the generated SQL file will be deleted after it has been imported.
5252
*/
5353
'removeFileAfterImport' => env('REMOTE_REMOVE_FILE_AFTER_IMPORT', 'true'),
54+
55+
/**
56+
* Sets the default name for SQL file if --filename is not provided
57+
*/
58+
'defaultFileName' => env('REMOTE_DEFAULT_FILE_NAME', 'file.sql'),
5459
];

src/Console/DbSyncCommand.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function handle(): bool
3131
$ignoreTables = explode(',', $ignore);
3232
$importSqlFile = config('dbsync.importSqlFile');
3333
$removeFileAfterImport = config('dbsync.removeFileAfterImport');
34+
$fileName = $this->argument('filename') ?? config('dbsync.defaultFileName');
3435

3536
if (empty($host) || empty($username) || empty($database)) {
3637
$this->error("DB credentials not set, have you published the config and set ENV variables?");
@@ -45,19 +46,19 @@ public function handle(): bool
4546
}
4647

4748
if ($useSsh === true) {
48-
exec("ssh $sshUsername@$host -p$sshPort mysqldump -u $username -p$password $database $ignoreString > file.sql", $output);
49+
exec("ssh $sshUsername@$host -p$sshPort mysqldump -u $username -p$password $database $ignoreString > $fileName", $output);
4950
} else {
50-
exec("mysqldump -h$host -u $username -p$password $database $ignoreString --column-statistics=0 > file.sql", $output);
51+
exec("mysqldump -h$host -u $username -p$password $database $ignoreString --column-statistics=0 > $fileName", $output);
5152
}
5253

5354
$this->comment(implode(PHP_EOL, $output));
5455

5556
if ($importSqlFile === true) {
56-
DB::unprepared(file_get_contents(base_path('file.sql')));
57+
DB::unprepared(file_get_contents(base_path($fileName)));
5758
}
5859

5960
if ($removeFileAfterImport === true) {
60-
unlink('file.sql');
61+
unlink($fileName);
6162
}
6263
}
6364

0 commit comments

Comments
 (0)