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