Skip to content

Commit

Permalink
Merge pull request #6 from hamidafghan/dybamic-file-name
Browse files Browse the repository at this point in the history
Make Dynamic file.sql
  • Loading branch information
dcblogdev authored Jul 8, 2022
2 parents 736509b + 33a8c15 commit c089a3d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ You can publish the config file with:

```
php artisan vendor:publish --provider="Dcblogdev\DbSync\DbSyncServiceProvider" --tag="config"
```
```

## .env
## .env

Set the remote database credentials in your .env file

Expand Down Expand Up @@ -59,6 +59,8 @@ REMOTE_IMPORT_FILE=true

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

To generate a SQL with a custom file name `REMOTE_DEFAULT_FILE_NAME`

## Usage

To export a remote database to OVERRIDE your local database by running:
Expand Down
7 changes: 6 additions & 1 deletion config/dbsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
'importSqlFile' => env('REMOTE_IMPORT_FILE', 'true'),

/**
* Sets if the generated file.sql will be deleted after it has been imported.
* Sets if the generated SQL file will be deleted after it has been imported.
*/
'removeFileAfterImport' => env('REMOTE_REMOVE_FILE_AFTER_IMPORT', 'true'),

/**
* Sets the default name for SQL file if --filename is not provided
*/
'defaultFileName' => env('REMOTE_DEFAULT_FILE_NAME', 'file.sql'),
];
9 changes: 5 additions & 4 deletions src/Console/DbSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function handle(): bool
$ignoreTables = explode(',', $ignore);
$importSqlFile = config('dbsync.importSqlFile');
$removeFileAfterImport = config('dbsync.removeFileAfterImport');
$fileName = $this->argument('filename') ?? config('dbsync.defaultFileName');

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

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

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

if ($importSqlFile === true) {
DB::unprepared(file_get_contents(base_path('file.sql')));
DB::unprepared(file_get_contents(base_path($fileName)));
}

if ($removeFileAfterImport === true) {
unlink('file.sql');
unlink($fileName);
}
}

Expand Down

0 comments on commit c089a3d

Please sign in to comment.