Skip to content

Implement artisan schema:dump #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/CockroachDbConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/Schema/CockroachSchemaState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace YlsIdeas\CockroachDb\Schema;

use Illuminate\Database\Connection;
use Illuminate\Database\Schema\SchemaState;

class CockroachSchemaState extends SchemaState
{
/**
* Dump the database's schema into a file.
*
* @param \Illuminate\Database\Connection $connection
* @param string $path
* @return void
*/
public function dump(Connection $connection, $path)
{
$pdo = $connection->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));
}
}