generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 5
[BUGFIX] Add custom CockroachSchemaManager and override selectTableColumns() / fixes #35 #36
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
Open
Vision42
wants to merge
4
commits into
ylsideas:main
Choose a base branch
from
Vision42:task/35-renaming-columns-with-deleted-columns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,17 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\CockroachDb\Driver; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Illuminate\Database\PDO\PostgresDriver; | ||
use YlsIdeas\CockroachDb\Schema\CockroachSchemaManager; | ||
|
||
class CockroachDBDriver extends PostgresDriver | ||
{ | ||
public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager | ||
{ | ||
return new CockroachSchemaManager($conn, $platform); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,93 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\CockroachDb\Schema; | ||
|
||
use Doctrine\DBAL\Result; | ||
use Doctrine\DBAL\Schema\Identifier; | ||
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; | ||
|
||
class CockroachSchemaManager extends PostgreSQLSchemaManager | ||
{ | ||
protected function selectTableColumns(string $databaseName, ?string $tableName = null): Result | ||
{ | ||
$sql = 'SELECT'; | ||
|
||
if ($tableName === null) { | ||
$sql .= ' c.relname AS table_name, n.nspname AS schema_name,'; | ||
} | ||
|
||
$sql .= <<<'SQL' | ||
a.attnum, | ||
quote_ident(a.attname) AS field, | ||
t.typname AS type, | ||
format_type(a.atttypid, a.atttypmod) AS complete_type, | ||
(SELECT tc.collcollate FROM pg_catalog.pg_collation tc WHERE tc.oid = a.attcollation) AS collation, | ||
(SELECT t1.typname FROM pg_catalog.pg_type t1 WHERE t1.oid = t.typbasetype) AS domain_type, | ||
(SELECT format_type(t2.typbasetype, t2.typtypmod) FROM | ||
pg_catalog.pg_type t2 WHERE t2.typtype = 'd' AND t2.oid = a.atttypid) AS domain_complete_type, | ||
a.attnotnull AS isnotnull, | ||
(SELECT 't' | ||
FROM pg_index | ||
WHERE c.oid = pg_index.indrelid | ||
AND pg_index.indkey[0] = a.attnum | ||
AND pg_index.indisprimary = 't' | ||
) AS pri, | ||
(SELECT pg_get_expr(adbin, adrelid) | ||
FROM pg_attrdef | ||
WHERE c.oid = pg_attrdef.adrelid | ||
AND pg_attrdef.adnum=a.attnum | ||
) AS default, | ||
(SELECT pg_description.description | ||
FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid | ||
) AS comment | ||
FROM pg_attribute a | ||
INNER JOIN pg_class c | ||
ON c.oid = a.attrelid | ||
INNER JOIN pg_type t | ||
ON t.oid = a.atttypid | ||
INNER JOIN pg_namespace n | ||
ON n.oid = c.relnamespace | ||
LEFT JOIN pg_depend d | ||
ON d.objid = c.oid | ||
AND d.deptype = 'e' | ||
AND d.classid = (SELECT oid FROM pg_class WHERE relname = 'pg_class') | ||
SQL; | ||
|
||
$conditions = array_merge([ | ||
'a.attnum > 0', | ||
"c.relkind = 'r'", | ||
'd.refobjid IS NULL', | ||
'a.attisdropped = false', | ||
], $this->buildQueryConditions($tableName)); | ||
|
||
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum'; | ||
|
||
return $this->_conn->executeQuery($sql); | ||
} | ||
|
||
/** | ||
* @param string|null $tableName | ||
* | ||
* @return list<string> | ||
*/ | ||
private function buildQueryConditions($tableName): array | ||
{ | ||
$conditions = []; | ||
|
||
if ($tableName !== null) { | ||
if (strpos($tableName, '.') !== false) { | ||
[$schemaName, $tableName] = explode('.', $tableName); | ||
$conditions[] = 'n.nspname = ' . $this->_platform->quoteStringLiteral($schemaName); | ||
} else { | ||
$conditions[] = 'n.nspname = ANY(current_schemas(false))'; | ||
} | ||
|
||
$identifier = new Identifier($tableName); | ||
$conditions[] = 'c.relname = ' . $this->_platform->quoteStringLiteral($identifier->getName()); | ||
} | ||
|
||
$conditions[] = "n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')"; | ||
|
||
return $conditions; | ||
} | ||
} |
This file contains hidden or 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,67 @@ | ||
<?php | ||
|
||
namespace YlsIdeas\CockroachDb\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class MigrationRenamingTest extends DatabaseTestCase | ||
{ | ||
private const TEST_TABLE = 'test_table'; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('column_y'); | ||
$table->string('column_x'); | ||
}); | ||
} | ||
|
||
public function test_rename_column_to_previously_deleted_one(): void | ||
{ | ||
Schema::table(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->dropColumn('column_y'); | ||
}); | ||
|
||
Schema::table(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->renameColumn('column_x', 'column_y'); | ||
}); | ||
|
||
$tableColumns = Schema::getColumns(self::TEST_TABLE); | ||
$this->assertCount(2, $tableColumns); | ||
$this->assertEquals('id', $tableColumns[0]['name']); | ||
$this->assertEquals('column_y', $tableColumns[1]['name']); | ||
} | ||
|
||
public function test_rename_column_with_any_previously_deleted_one(): void | ||
{ | ||
Schema::table(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->dropColumn('column_y'); | ||
}); | ||
|
||
Schema::table(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->renameColumn('column_x', 'column_z'); | ||
}); | ||
|
||
$tableColumns = Schema::getColumns(self::TEST_TABLE); | ||
$this->assertCount(2, $tableColumns); | ||
$this->assertEquals('id', $tableColumns[0]['name']); | ||
$this->assertEquals('column_z', $tableColumns[1]['name']); | ||
} | ||
|
||
public function test_rename_column_without_deleted_ones(): void | ||
{ | ||
Schema::table(self::TEST_TABLE, function (Blueprint $table) { | ||
$table->renameColumn('column_x', 'column_z'); | ||
}); | ||
|
||
$tableColumns = Schema::getColumns(self::TEST_TABLE); | ||
$this->assertCount(3, $tableColumns); | ||
$this->assertEquals('id', $tableColumns[0]['name']); | ||
$this->assertEquals('column_y', $tableColumns[1]['name']); | ||
$this->assertEquals('column_z', $tableColumns[2]['name']); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.