Skip to content

Make Dynamic file.sql #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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