Skip to content
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

Add config option database_dump_file_use_connection_name #1785

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions config/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
*/
'database_dump_file_timestamp_format' => null,

/*
* If specified, the database dumped file name will contain the connection name in place of the database name.
*/
'database_dump_file_use_connection_name' => null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is a bool value, shoud it be false by default instead of null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree, false would be a better default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Had a thought though to change the config to a string, either 'database' or 'connection' and default it to connection.

I updated to that. It feels better that the 'database' option is visible in the config as default instead of a boolean to change behavior.

I'm happy to change it back to a boolean if that is preferred.


/*
* The file extension used for the database dump files.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Tasks/Backup/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,12 @@ protected function dumpDatabases(): array

$dbType = mb_strtolower(basename(str_replace('\\', '/', get_class($dbDumper))));

$dbName = $dbDumper->getDbName();
if ($dbDumper instanceof Sqlite) {
if (config('backup.backup.database_dump_file_use_connection_name')) {
$dbName = $key;
} else if ($dbDumper instanceof Sqlite) {
$dbName = $key . '-database';
} else {
$dbName = $dbDumper->getDbName();
}

$timeStamp = '';
Expand Down
17 changes: 17 additions & 0 deletions tests/Commands/BackupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,20 @@
Sleep::for(3)->seconds(),
]);
});

it('uses connection name in place of database name for dump filename', function () {
config()->set('backup.backup.source.databases', ['db1']);
config()->set('backup.backup.database_dump_file_use_connection_name', true);

$this->setUpDatabase(app());

$this->artisan('backup:run --only-db')->assertExitCode(0);

$this->assertExactPathExistsInZip('local', $this->expectedZipPath, 'db-dumps/sqlite-db1.sql');

/*
* Close the database connection to unlock the sqlite file for deletion.
* This prevents the errors from other tests trying to delete and recreate the folder.
*/
app()['db']->disconnect();
});