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
MySqlSchemaState.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema; |
| 4 | |
| 5 | use Exception; |
| 6 | use IAWPSCOPED\Illuminate\Database\Connection; |
| 7 | use IAWPSCOPED\Illuminate\Support\Str; |
| 8 | use IAWPSCOPED\Symfony\Component\Process\Process; |
| 9 | /** @internal */ |
| 10 | class MySqlSchemaState extends SchemaState |
| 11 | { |
| 12 | /** |
| 13 | * Dump the database's schema into a file. |
| 14 | * |
| 15 | * @param \Illuminate\Database\Connection $connection |
| 16 | * @param string $path |
| 17 | * @return void |
| 18 | */ |
| 19 | public function dump(Connection $connection, $path) |
| 20 | { |
| 21 | $this->executeDumpProcess($this->makeProcess($this->baseDumpCommand() . ' --routines --result-file="${:LARAVEL_LOAD_PATH}" --no-data'), $this->output, \array_merge($this->baseVariables($this->connection->getConfig()), ['LARAVEL_LOAD_PATH' => $path])); |
| 22 | $this->removeAutoIncrementingState($path); |
| 23 | $this->appendMigrationData($path); |
| 24 | } |
| 25 | /** |
| 26 | * Remove the auto-incrementing state from the given schema dump. |
| 27 | * |
| 28 | * @param string $path |
| 29 | * @return void |
| 30 | */ |
| 31 | protected function removeAutoIncrementingState(string $path) |
| 32 | { |
| 33 | $this->files->put($path, \preg_replace('/\\s+AUTO_INCREMENT=[0-9]+/iu', '', $this->files->get($path))); |
| 34 | } |
| 35 | /** |
| 36 | * Append the migration data to the schema dump. |
| 37 | * |
| 38 | * @param string $path |
| 39 | * @return void |
| 40 | */ |
| 41 | protected function appendMigrationData(string $path) |
| 42 | { |
| 43 | $process = $this->executeDumpProcess($this->makeProcess($this->baseDumpCommand() . ' ' . $this->migrationTable . ' --no-create-info --skip-extended-insert --skip-routines --compact'), null, \array_merge($this->baseVariables($this->connection->getConfig()), [])); |
| 44 | $this->files->append($path, $process->getOutput()); |
| 45 | } |
| 46 | /** |
| 47 | * Load the given schema file into the database. |
| 48 | * |
| 49 | * @param string $path |
| 50 | * @return void |
| 51 | */ |
| 52 | public function load($path) |
| 53 | { |
| 54 | $command = 'mysql ' . $this->connectionString() . ' --database="${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'; |
| 55 | $process = $this->makeProcess($command)->setTimeout(null); |
| 56 | $process->mustRun(null, \array_merge($this->baseVariables($this->connection->getConfig()), ['LARAVEL_LOAD_PATH' => $path])); |
| 57 | } |
| 58 | /** |
| 59 | * Get the base dump command arguments for MySQL as a string. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | protected function baseDumpCommand() |
| 64 | { |
| 65 | $command = 'mysqldump ' . $this->connectionString() . ' --no-tablespaces --skip-add-locks --skip-comments --skip-set-charset --tz-utc --column-statistics=0'; |
| 66 | if (!$this->connection->isMaria()) { |
| 67 | $command .= ' --set-gtid-purged=OFF'; |
| 68 | } |
| 69 | return $command . ' "${:LARAVEL_LOAD_DATABASE}"'; |
| 70 | } |
| 71 | /** |
| 72 | * Generate a basic connection string (--socket, --host, --port, --user, --password) for the database. |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | protected function connectionString() |
| 77 | { |
| 78 | $value = ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}"'; |
| 79 | $config = $this->connection->getConfig(); |
| 80 | $value .= $config['unix_socket'] ?? \false ? ' --socket="${:LARAVEL_LOAD_SOCKET}"' : ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; |
| 81 | if (isset($config['options'][\PDO::MYSQL_ATTR_SSL_CA])) { |
| 82 | $value .= ' --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"'; |
| 83 | } |
| 84 | return $value; |
| 85 | } |
| 86 | /** |
| 87 | * Get the base variables for a dump / load command. |
| 88 | * |
| 89 | * @param array $config |
| 90 | * @return array |
| 91 | */ |
| 92 | protected function baseVariables(array $config) |
| 93 | { |
| 94 | $config['host'] ??= ''; |
| 95 | return ['LARAVEL_LOAD_SOCKET' => $config['unix_socket'] ?? '', 'LARAVEL_LOAD_HOST' => \is_array($config['host']) ? $config['host'][0] : $config['host'], 'LARAVEL_LOAD_PORT' => $config['port'] ?? '', 'LARAVEL_LOAD_USER' => $config['username'], 'LARAVEL_LOAD_PASSWORD' => $config['password'] ?? '', 'LARAVEL_LOAD_DATABASE' => $config['database'], 'LARAVEL_LOAD_SSL_CA' => $config['options'][\PDO::MYSQL_ATTR_SSL_CA] ?? '']; |
| 96 | } |
| 97 | /** |
| 98 | * Execute the given dump process. |
| 99 | * |
| 100 | * @param \Symfony\Component\Process\Process $process |
| 101 | * @param callable $output |
| 102 | * @param array $variables |
| 103 | * @return \Symfony\Component\Process\Process |
| 104 | */ |
| 105 | protected function executeDumpProcess(Process $process, $output, array $variables) |
| 106 | { |
| 107 | try { |
| 108 | $process->setTimeout(null)->mustRun($output, $variables); |
| 109 | } catch (Exception $e) { |
| 110 | if (Str::contains($e->getMessage(), ['column-statistics', 'column_statistics'])) { |
| 111 | return $this->executeDumpProcess(Process::fromShellCommandLine(\str_replace(' --column-statistics=0', '', $process->getCommandLine())), $output, $variables); |
| 112 | } |
| 113 | if (\str_contains($e->getMessage(), 'set-gtid-purged')) { |
| 114 | return $this->executeDumpProcess(Process::fromShellCommandLine(\str_replace(' --set-gtid-purged=OFF', '', $process->getCommandLine())), $output, $variables); |
| 115 | } |
| 116 | throw $e; |
| 117 | } |
| 118 | return $process; |
| 119 | } |
| 120 | } |
| 121 |