From ac77f45430ed8081cb2302a02d5acdf9746ada6b Mon Sep 17 00:00:00 2001 From: Kamil Michalak Date: Wed, 13 Dec 2023 10:02:22 +0000 Subject: [PATCH] CockroachSchemaState - add dump/load cache schema Signed-off-by: Kamil Michalak --- src/CockroachDbConnection.php | 8 ++-- src/Schema/CockroachSchemaState.php | 57 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/Schema/CockroachSchemaState.php diff --git a/src/CockroachDbConnection.php b/src/CockroachDbConnection.php index 607bcff..0ddb4d2 100644 --- a/src/CockroachDbConnection.php +++ b/src/CockroachDbConnection.php @@ -6,12 +6,12 @@ use Illuminate\Database\Grammar; use Illuminate\Database\PDO\PostgresDriver; use Illuminate\Database\PostgresConnection; -use Illuminate\Database\Schema\PostgresSchemaState; use Illuminate\Filesystem\Filesystem; use YlsIdeas\CockroachDb\Builder\CockroachDbBuilder; use YlsIdeas\CockroachDb\Processor\CockroachDbProcessor; use YlsIdeas\CockroachDb\Query\CockroachGrammar as QueryGrammar; use YlsIdeas\CockroachDb\Schema\CockroachGrammar as SchemaGrammar; +use YlsIdeas\CockroachDb\Schema\CockroachSchemaState; class CockroachDbConnection extends PostgresConnection implements ConnectionInterface { @@ -54,11 +54,11 @@ protected function getDefaultSchemaGrammar(): Grammar * * @param \Illuminate\Filesystem\Filesystem|null $files * @param callable|null $processFactory - * @return \Illuminate\Database\Schema\PostgresSchemaState + * @return \YlsIdeas\CockroachDb\Schema\CockroachSchemaState */ - public function getSchemaState(Filesystem $files = null, callable $processFactory = null): PostgresSchemaState + public function getSchemaState(Filesystem $files = null, callable $processFactory = null): CockroachSchemaState { - return new PostgresSchemaState($this, $files, $processFactory); + return new CockroachSchemaState($this, $files, $processFactory); } /** diff --git a/src/Schema/CockroachSchemaState.php b/src/Schema/CockroachSchemaState.php new file mode 100644 index 0000000..1b4e86a --- /dev/null +++ b/src/Schema/CockroachSchemaState.php @@ -0,0 +1,57 @@ +getPdo(); + + // tables + $query = $pdo->query("SHOW CREATE ALL TABLES"); + $query->execute(); + file_put_contents($path, $query->fetchAll(\PDO::FETCH_COLUMN)); + + // migration statuses + $query = $pdo->query(sprintf('Select * from "%s"', $this->migrationTable)); + $query->execute(); + + $migrations = []; + while ($migration = $query->fetch(\PDO::FETCH_ASSOC)) { + $migrations[] = sprintf( + 'Insert into "%s" (%s) values (%s);', + $this->migrationTable, + join( + ', ', + array_map(fn ($value) => '"' . $value . '"', array_keys($migration)) + ), + join(', ', array_map(fn ($value) => is_string($value) ? "'" . $value . "'" : $value, $migration)) + ); + } + + file_put_contents($path, "\n\n" . join("\n", $migrations) . "\n\n", \FILE_APPEND); + } + + /** + * Load the given schema file into the database. + * + * @param string $path + * @return void + */ + public function load($path) + { + $pdo = $this->connection->getPdo(); + $pdo->exec(file_get_contents($path)); + } +}