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
SqliteSchemaState.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Connection; |
| 6 | /** @internal */ |
| 7 | class SqliteSchemaState 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 | with($process = $this->makeProcess($this->baseCommand() . ' .schema'))->setTimeout(null)->mustRun(null, \array_merge($this->baseVariables($this->connection->getConfig()), [])); |
| 19 | $migrations = \IAWPSCOPED\collect(\preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { |
| 20 | return \stripos($line, 'sqlite_sequence') === \false && \strlen($line) > 0; |
| 21 | })->all(); |
| 22 | $this->files->put($path, \implode(\PHP_EOL, $migrations) . \PHP_EOL); |
| 23 | $this->appendMigrationData($path); |
| 24 | } |
| 25 | /** |
| 26 | * Append the migration data to the schema dump. |
| 27 | * |
| 28 | * @param string $path |
| 29 | * @return void |
| 30 | */ |
| 31 | protected function appendMigrationData(string $path) |
| 32 | { |
| 33 | with($process = $this->makeProcess($this->baseCommand() . ' ".dump \'' . $this->migrationTable . '\'"'))->mustRun(null, \array_merge($this->baseVariables($this->connection->getConfig()), [])); |
| 34 | $migrations = \IAWPSCOPED\collect(\preg_split("/\r\n|\n|\r/", $process->getOutput()))->filter(function ($line) { |
| 35 | return \preg_match('/^\\s*(--|INSERT\\s)/iu', $line) === 1 && \strlen($line) > 0; |
| 36 | })->all(); |
| 37 | $this->files->append($path, \implode(\PHP_EOL, $migrations) . \PHP_EOL); |
| 38 | } |
| 39 | /** |
| 40 | * Load the given schema file into the database. |
| 41 | * |
| 42 | * @param string $path |
| 43 | * @return void |
| 44 | */ |
| 45 | public function load($path) |
| 46 | { |
| 47 | if ($this->connection->getDatabaseName() === ':memory:') { |
| 48 | $this->connection->getPdo()->exec($this->files->get($path)); |
| 49 | return; |
| 50 | } |
| 51 | $process = $this->makeProcess($this->baseCommand() . ' < "${:LARAVEL_LOAD_PATH}"'); |
| 52 | $process->mustRun(null, \array_merge($this->baseVariables($this->connection->getConfig()), ['LARAVEL_LOAD_PATH' => $path])); |
| 53 | } |
| 54 | /** |
| 55 | * Get the base sqlite command arguments as a string. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | protected function baseCommand() |
| 60 | { |
| 61 | return 'sqlite3 "${:LARAVEL_LOAD_DATABASE}"'; |
| 62 | } |
| 63 | /** |
| 64 | * Get the base variables for a dump / load command. |
| 65 | * |
| 66 | * @param array $config |
| 67 | * @return array |
| 68 | */ |
| 69 | protected function baseVariables(array $config) |
| 70 | { |
| 71 | return ['LARAVEL_LOAD_DATABASE' => $config['database']]; |
| 72 | } |
| 73 | } |
| 74 |