| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Schema;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Database\Connection;
|
|
|
6 |
|
|
|
7 |
class PostgresSchemaState extends SchemaState
|
|
|
8 |
{
|
|
|
9 |
/**
|
|
|
10 |
* Dump the database's schema into a file.
|
|
|
11 |
*
|
|
|
12 |
* @param \Illuminate\Database\Connection $connection
|
|
|
13 |
* @param string $path
|
|
|
14 |
* @return void
|
|
|
15 |
*/
|
|
|
16 |
public function dump(Connection $connection, $path)
|
|
|
17 |
{
|
|
|
18 |
$excludedTables = collect($connection->getSchemaBuilder()->getAllTables())
|
|
|
19 |
->map->tablename
|
|
|
20 |
->reject(function ($table) {
|
|
|
21 |
return $table === $this->migrationTable;
|
|
|
22 |
})->map(function ($table) {
|
|
|
23 |
return '--exclude-table-data="*.'.$table.'"';
|
|
|
24 |
})->implode(' ');
|
|
|
25 |
|
|
|
26 |
$this->makeProcess(
|
|
|
27 |
$this->baseDumpCommand().' --file="${:LARAVEL_LOAD_PATH}" '.$excludedTables
|
|
|
28 |
)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
|
|
|
29 |
'LARAVEL_LOAD_PATH' => $path,
|
|
|
30 |
]));
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Load the given schema file into the database.
|
|
|
35 |
*
|
|
|
36 |
* @param string $path
|
|
|
37 |
* @return void
|
|
|
38 |
*/
|
|
|
39 |
public function load($path)
|
|
|
40 |
{
|
|
|
41 |
$command = 'pg_restore --no-owner --no-acl --clean --if-exists --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}" "${:LARAVEL_LOAD_PATH}"';
|
|
|
42 |
|
|
|
43 |
if (str_ends_with($path, '.sql')) {
|
|
|
44 |
$command = 'psql --file="${:LARAVEL_LOAD_PATH}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$process = $this->makeProcess($command);
|
|
|
48 |
|
|
|
49 |
$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
|
|
|
50 |
'LARAVEL_LOAD_PATH' => $path,
|
|
|
51 |
]));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Get the base dump command arguments for PostgreSQL as a string.
|
|
|
56 |
*
|
|
|
57 |
* @return string
|
|
|
58 |
*/
|
|
|
59 |
protected function baseDumpCommand()
|
|
|
60 |
{
|
|
|
61 |
return 'pg_dump --no-owner --no-acl -Fc --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Get the base variables for a dump / load command.
|
|
|
66 |
*
|
|
|
67 |
* @param array $config
|
|
|
68 |
* @return array
|
|
|
69 |
*/
|
|
|
70 |
protected function baseVariables(array $config)
|
|
|
71 |
{
|
|
|
72 |
$config['host'] ??= '';
|
|
|
73 |
|
|
|
74 |
return [
|
|
|
75 |
'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'],
|
|
|
76 |
'LARAVEL_LOAD_PORT' => $config['port'],
|
|
|
77 |
'LARAVEL_LOAD_USER' => $config['username'],
|
|
|
78 |
'PGPASSWORD' => $config['password'],
|
|
|
79 |
'LARAVEL_LOAD_DATABASE' => $config['database'],
|
|
|
80 |
];
|
|
|
81 |
}
|
|
|
82 |
}
|