master

laravel/framework

Last updated at: 29/12/2023 09:23

PostgresSchemaState.php

TLDR

The PostgresSchemaState.php file is a part of the Illuminate\Database\Schema namespace in the Demo Projects project. It contains a class called PostgresSchemaState, which extends the SchemaState class. This file provides methods for dumping and loading a PostgreSQL database schema.

Methods

dump

This method is used to dump the database schema into a file. It takes two parameters: $connection of type Illuminate\Database\Connection and $path of type string. The method generates two dump commands, one for the schema-only and one for the data-only, and executes them.

load

This method is used to load a schema file into the database. It takes a single parameter $path of type string. The method checks the file extension and generates the appropriate command, either pg_restore or psql, and executes it.

baseDumpCommand

This protected method returns the base dump command arguments for PostgreSQL as a string. It generates the command with the necessary variables.

baseVariables

This protected method returns the base variables for a dump / load command. It takes an array $config as a parameter and returns an array of variables required for the command.

Classes

There are no additional classes in this file.

<?php

namespace Illuminate\Database\Schema;

use Illuminate\Database\Connection;

class PostgresSchemaState extends SchemaState
{
    /**
     * Dump the database's schema into a file.
     *
     * @param  \Illuminate\Database\Connection  $connection
     * @param  string  $path
     * @return void
     */
    public function dump(Connection $connection, $path)
    {
        $commands = collect([
            $this->baseDumpCommand().' --schema-only > '.$path,
            $this->baseDumpCommand().' -t '.$this->migrationTable.' --data-only >> '.$path,
        ]);

        $commands->map(function ($command, $path) {
            $this->makeProcess($command)->mustRun($this->output, array_merge($this->baseVariables($this->connection->getConfig()), [
                'LARAVEL_LOAD_PATH' => $path,
            ]));
        });
    }

    /**
     * Load the given schema file into the database.
     *
     * @param  string  $path
     * @return void
     */
    public function load($path)
    {
        $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}"';

        if (str_ends_with($path, '.sql')) {
            $command = 'psql --file="${:LARAVEL_LOAD_PATH}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
        }

        $process = $this->makeProcess($command);

        $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
            'LARAVEL_LOAD_PATH' => $path,
        ]));
    }

    /**
     * Get the base dump command arguments for PostgreSQL as a string.
     *
     * @return string
     */
    protected function baseDumpCommand()
    {
        return 'pg_dump --no-owner --no-acl --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"';
    }

    /**
     * Get the base variables for a dump / load command.
     *
     * @param  array  $config
     * @return array
     */
    protected function baseVariables(array $config)
    {
        $config['host'] ??= '';

        return [
            'LARAVEL_LOAD_HOST' => is_array($config['host']) ? $config['host'][0] : $config['host'],
            'LARAVEL_LOAD_PORT' => $config['port'] ?? '',
            'LARAVEL_LOAD_USER' => $config['username'],
            'PGPASSWORD' => $config['password'],
            'LARAVEL_LOAD_DATABASE' => $config['database'],
        ];
    }
}