generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* CockroachSchemaState - add dump/load cache schema Signed-off-by: Kamil Michalak <[email protected]> * wip * Adds fleshed out functionality for schema dumps * Fix PHP styling * Fix broken CI * CI fixes * Fix tests of older Laravel versions * Fix breaking tests * Fixes for CI * Fix PHP styling * Another CI fix * Fix PHPStan --------- Signed-off-by: Kamil Michalak <[email protected]> Co-authored-by: Kamil Michalak <[email protected]> Co-authored-by: peterfox <[email protected]>
- Loading branch information
1 parent
f3383bb
commit aa42814
Showing
15 changed files
with
299 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\CockroachDb\Schema; | ||
|
||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
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): void | ||
{ | ||
$query = $connection->getPdo()->query('SHOW CREATE ALL TABLES'); | ||
$query->execute(); | ||
|
||
$file = collect($query->fetchAll(\PDO::FETCH_COLUMN))->join(PHP_EOL); | ||
|
||
$migrationRows = $connection | ||
->table($this->migrationTable) | ||
->get() | ||
->map(fn (\stdClass $row) => (array) $row); | ||
|
||
$statements = $connection->pretend(function (Connection $connection) use ($migrationRows) { | ||
$connection->table($this->migrationTable) | ||
->insert($migrationRows->all()); | ||
}); | ||
|
||
if ($statements !== []) { | ||
$file .= PHP_EOL . $statements[0]['query']; | ||
} | ||
|
||
$this->files->put($path, $file); | ||
} | ||
|
||
/** | ||
* Load the given schema file into the database. | ||
* | ||
* @param string $path | ||
* @return void | ||
* @throws FileNotFoundException | ||
*/ | ||
public function load($path): void | ||
{ | ||
$fileContents = $this->files->get($path); | ||
if ($fileContents === '') { | ||
throw new \RuntimeException(sprintf('file %s is empty', $path)); | ||
} | ||
|
||
$pdo = $this->connection->getPdo(); | ||
$pdo->exec($fileContents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\CockroachDb\Tests\Integration\Database; | ||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
class DumpAndLoadSchemaTest extends DatabaseTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function test_exporting_an_sql_dump() | ||
{ | ||
File::ensureDirectoryExists(database_path('schema-test')); | ||
|
||
if ($this->app['config']->get('database.default') !== 'testing') { | ||
$this->artisan('db:wipe', ['--drop-views' => true]); | ||
} | ||
|
||
$options = [ | ||
'--path' => realpath(__DIR__.'/stubs/schema-dump-migrations'), | ||
'--realpath' => true, | ||
]; | ||
|
||
$this->artisan('migrate', $options); | ||
|
||
$this->beforeApplicationDestroyed(function () use ($options) { | ||
File::delete(database_path('schema-test/crdb-schema.sql')); | ||
|
||
$this->artisan('migrate:rollback', $options); | ||
}); | ||
|
||
$this->artisan('schema:dump', [ | ||
'--database' => 'crdb', | ||
'--path' => database_path('schema-test/crdb-schema.sql'), | ||
]) | ||
->assertSuccessful(); | ||
|
||
$this->assertFileExists(database_path('schema-test/crdb-schema.sql')); | ||
} | ||
|
||
public function test_importing_an_sql_dump() | ||
{ | ||
// Make sure the schema direct exists first | ||
File::ensureDirectoryExists(database_path('schema-test')); | ||
File::copy(__DIR__ . '/stubs/schema-dump.sql', database_path('schema-test/crdb-schema.sql')); | ||
|
||
if ($this->app['config']->get('database.default') !== 'testing') { | ||
$this->artisan('db:wipe', ['--drop-views' => true]); | ||
} | ||
|
||
$options = [ | ||
'--path' => realpath(__DIR__.'/stubs/schema-dump-migrations'), | ||
'--realpath' => true, | ||
]; | ||
|
||
$this->beforeApplicationDestroyed(function () use ($options) { | ||
File::delete(database_path('schema/crdb-schema.sql')); | ||
|
||
$this->artisan('migrate:rollback', $options); | ||
}); | ||
|
||
$this->artisan('migrate', [ | ||
...$options, | ||
...['--schema-path' => database_path('schema-test/crdb-schema.sql')], | ||
]); | ||
|
||
$this->assertDatabaseCount('migrations', 2); | ||
|
||
$this->assertDatabaseHas('migrations', [ | ||
'migration' => '2014_10_12_000000_create_members_table', | ||
]); | ||
|
||
$this->assertDatabaseHas('migrations', [ | ||
'migration' => '2014_10_12_000000_create_users_table', | ||
]); | ||
|
||
$this->assertDatabaseCount('users', 0); | ||
$this->assertDatabaseCount('members', 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.