Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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
    {
399 lars 18
        $commands = collect([
19
            $this->baseDumpCommand().' --schema-only > '.$path,
20
            $this->baseDumpCommand().' -t '.$this->migrationTable.' --data-only >> '.$path,
21
        ]);
148 lars 22
 
399 lars 23
        $commands->map(function ($command, $path) {
24
            $this->makeProcess($command)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
25
                'LARAVEL_LOAD_PATH' => $path,
26
            ]));
27
        });
148 lars 28
    }
29
 
30
    /**
31
     * Load the given schema file into the database.
32
     *
33
     * @param  string  $path
34
     * @return void
35
     */
36
    public function load($path)
37
    {
38
        $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}"';
39
 
40
        if (str_ends_with($path, '.sql')) {
41
            $command = 'psql --file="${:LARAVEL_LOAD_PATH}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
42
        }
43
 
44
        $process = $this->makeProcess($command);
45
 
46
        $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
47
            'LARAVEL_LOAD_PATH' => $path,
48
        ]));
49
    }
50
 
51
    /**
52
     * Get the base dump command arguments for PostgreSQL as a string.
53
     *
54
     * @return string
55
     */
56
    protected function baseDumpCommand()
57
    {
399 lars 58
        return 'pg_dump --no-owner --no-acl --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
148 lars 59
    }
60
 
61
    /**
62
     * Get the base variables for a dump / load command.
63
     *
64
     * @param  array  $config
65
     * @return array
66
     */
67
    protected function baseVariables(array $config)
68
    {
69
        $config['host'] ??= '';
70
 
71
        return [
72
            'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'],
73
            'LARAVEL_LOAD_PORT' => $config['port'],
74
            'LARAVEL_LOAD_USER' => $config['username'],
75
            'PGPASSWORD' => $config['password'],
76
            'LARAVEL_LOAD_DATABASE' => $config['database'],
77
        ];
78
    }
79
}