Grammars
3 weeks ago
Blueprint.php
3 weeks ago
Builder.php
3 weeks ago
ColumnDefinition.php
3 weeks ago
ForeignIdColumnDefinition.php
3 weeks ago
ForeignKeyDefinition.php
3 weeks ago
IndexDefinition.php
3 weeks ago
MySqlBuilder.php
3 weeks ago
MySqlSchemaState.php
3 weeks ago
PostgresBuilder.php
3 weeks ago
PostgresSchemaState.php
3 weeks ago
SQLiteBuilder.php
3 weeks ago
SchemaState.php
3 weeks ago
SqlServerBuilder.php
3 weeks ago
SqliteSchemaState.php
3 weeks ago
PostgresSchemaState.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Connection; |
| 6 | /** @internal */ |
| 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 | $commands = \IAWPSCOPED\collect([$this->baseDumpCommand() . ' --schema-only > ' . $path, $this->baseDumpCommand() . ' -t ' . $this->migrationTable . ' --data-only >> ' . $path]); |
| 19 | $commands->map(function ($command, $path) { |
| 20 | $this->makeProcess($command)->mustRun($this->output, \array_merge($this->baseVariables($this->connection->getConfig()), ['LARAVEL_LOAD_PATH' => $path])); |
| 21 | }); |
| 22 | } |
| 23 | /** |
| 24 | * Load the given schema file into the database. |
| 25 | * |
| 26 | * @param string $path |
| 27 | * @return void |
| 28 | */ |
| 29 | public function load($path) |
| 30 | { |
| 31 | $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}"'; |
| 32 | if (\str_ends_with($path, '.sql')) { |
| 33 | $command = 'psql --file="${:LARAVEL_LOAD_PATH}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"'; |
| 34 | } |
| 35 | $process = $this->makeProcess($command); |
| 36 | $process->mustRun(null, \array_merge($this->baseVariables($this->connection->getConfig()), ['LARAVEL_LOAD_PATH' => $path])); |
| 37 | } |
| 38 | /** |
| 39 | * Get the base dump command arguments for PostgreSQL as a string. |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | protected function baseDumpCommand() |
| 44 | { |
| 45 | return 'pg_dump --no-owner --no-acl --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --username="${:LARAVEL_LOAD_USER}" --dbname="${:LARAVEL_LOAD_DATABASE}"'; |
| 46 | } |
| 47 | /** |
| 48 | * Get the base variables for a dump / load command. |
| 49 | * |
| 50 | * @param array $config |
| 51 | * @return array |
| 52 | */ |
| 53 | protected function baseVariables(array $config) |
| 54 | { |
| 55 | $config['host'] ??= ''; |
| 56 | 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']]; |
| 57 | } |
| 58 | } |
| 59 |