Skip to content

Commit efc338d

Browse files
authored
Merge pull request #15 from VanOns/feature/configurable-db-connections
Configurable local and remote db connection types
2 parents 7848e99 + 5e79a9a commit efc338d

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

UPGRADING.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ across such a case, please let us know by [opening an issue][issues], or by addi
1616
* The `db_timeout` configuration option was added (default: `60`, same as the default shell command timeout).
1717
Set the option to `null` to disable the timeout.
1818

19-
* The `db_ssh_tunnel_timeout` configuration was added (default: `30` seconds). This sets the timeout for establishing
19+
* The `db_ssh_tunnel_timeout` configuration option was added (default: `30` seconds). This sets the timeout for establishing
2020
an SSH tunnel to the remote database server. Set the option to `null` to disable the timeout.
2121

22+
* The `db_type` configuration option was added to `environments.*`. This allows specifying the database type of the remote
23+
environment (`mysql` (default), `mariadb`, `mongodb`, `pgsql` or `sqlite`).
24+
2225
## v0.6.0
2326

2427
* The configuration for the `AnonymizeUsers` processor was changed. Make sure to update your config file accordingly, so

config/environment-importer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@
99
|
1010
| Environments that should be available for import.
1111
|
12+
| Each environment supports the following settings:
13+
| - ssh_host: The SSH host to connect to.
14+
| - ssh_username: The SSH username to use.
15+
| - ssh_key: The SSH private key to use (optional if ssh_password is used).
16+
| - ssh_password: The SSH password to use (overrules ssh_key if present).
17+
| - ssh_base_path: The base path of the project on the remote server.
18+
|
19+
| - db_type: The database connection type. Supported types: `mysql` (default), `mariadb`, `mongodb`, `pgsql` and `sqlite`.
20+
| - db_host: The database host.
21+
| - db_name: The database name.
22+
| - db_username: The database username.
23+
| - db_password: The database password.
24+
| - db_port: The database port.
25+
| - db_use_ssh: Whether to use an SSH tunnel to connect to the database.
26+
| - db_ssh_tunnel_port: The local port to use for the SSH tunnel.
27+
|
1228
*/
1329

1430
'environments' => [
1531
'staging' => [
1632
'ssh_host' => env('LEI_STAGING_SSH_HOST'),
1733
'ssh_username' => env('LEI_STAGING_SSH_USERNAME'),
1834
'ssh_key' => env('LEI_STAGING_SSH_KEY', '~/.ssh/id_rsa'),
19-
'ssh_password' => env('LEI_STAGING_SSH_PASSWORD'), // Overrules ssh_key if present
35+
'ssh_password' => env('LEI_STAGING_SSH_PASSWORD'),
2036
'ssh_base_path' => env('LEI_STAGING_SSH_BASE_PATH'),
2137

38+
'db_type' => env('LEI_STAGING_DB_TYPE', 'mysql'),
2239
'db_host' => env('LEI_STAGING_DB_HOST'),
2340
'db_name' => env('LEI_STAGING_DB_NAME'),
2441
'db_username' => env('LEI_STAGING_DB_USERNAME'),
@@ -32,9 +49,10 @@
3249
'ssh_host' => env('LEI_PRODUCTION_SSH_HOST'),
3350
'ssh_username' => env('LEI_PRODUCTION_SSH_USERNAME'),
3451
'ssh_key' => env('LEI_PRODUCTION_SSH_KEY', '~/.ssh/id_rsa'),
35-
'ssh_password' => env('LEI_PRODUCTION_SSH_PASSWORD'), // Overrules ssh_key if present
52+
'ssh_password' => env('LEI_PRODUCTION_SSH_PASSWORD'),
3653
'ssh_base_path' => env('LEI_PRODUCTION_SSH_BASE_PATH'),
3754

55+
'db_type' => env('LEI_PRODUCTION_DB_TYPE', 'mysql'),
3856
'db_host' => env('LEI_PRODUCTION_DB_HOST'),
3957
'db_name' => env('LEI_PRODUCTION_DB_NAME'),
4058
'db_username' => env('LEI_PRODUCTION_DB_USERNAME'),

src/Commands/ImportEnvironmentCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
use Illuminate\Support\Facades\File;
1212
use Illuminate\Support\Facades\Notification;
1313
use Illuminate\Support\Facades\Schema;
14+
use Spatie\DbDumper\Databases\MariaDb;
15+
use Spatie\DbDumper\Databases\MongoDb;
1416
use Spatie\DbDumper\Databases\MySql;
17+
use Spatie\DbDumper\Databases\PostgreSql;
18+
use Spatie\DbDumper\Databases\Sqlite;
1519
use Spatie\DbDumper\Exceptions\CannotSetParameter;
1620
use Symfony\Component\Process\Process;
1721
use VanOns\LaravelEnvironmentImporter\Exceptions\ImportEnvironmentException;
@@ -157,6 +161,7 @@ protected function getEnvironments(): array
157161
'ssh_host',
158162
'ssh_username',
159163
'ssh_base_path',
164+
'db_type',
160165
'db_host',
161166
'db_name',
162167
'db_username',
@@ -321,16 +326,28 @@ protected function getDatabaseDumpClient(bool $local = false): MySql
321326
default => $this->dbPort(),
322327
};
323328

329+
$dbType = match ($local) {
330+
true => DB::getDriverName(),
331+
false => $this->getEnvironmentConfigValue('db_type', 'mysql'),
332+
};
333+
334+
/** @var class-string<MariaDb|MongoDb|MySql|PostgreSql|Sqlite> $db */
335+
$db = match ($dbType) {
336+
'mariadb' => MariaDb::class,
337+
'mongodb' => MongoDb::class,
338+
'pgsql' => PostgreSql::class,
339+
'sqlite' => Sqlite::class,
340+
default => MySql::class,
341+
};
342+
324343
/** @phpstan-ignore-next-line */
325-
return MySql::create()
344+
return $db::create()
326345
->setHost($local ? DB::getConfig('host') : $this->getEnvironmentConfigValue('db_host'))
327346
->setDbName($local ? DB::getConfig('database') : $this->getEnvironmentConfigValue('db_name'))
328347
->setUserName($local ? DB::getConfig('username') : $this->getEnvironmentConfigValue('db_username'))
329348
->setPassword($local ? DB::getConfig('password') : $this->getEnvironmentConfigValue('db_password'))
330349
->setPort($port)
331-
->setDumpBinaryPath($this->getConfigValue('db_dump_binary_path', '/usr/bin'))
332-
// Disable column statistics to prevent issues with older MySQL or MariaDB versions.
333-
->addExtraOption('--column-statistics=0');
350+
->setDumpBinaryPath($this->getConfigValue('db_dump_binary_path', '/usr/bin'));
334351
}
335352

336353
/**

0 commit comments

Comments
 (0)