From 358c1eaffc18cf10d9222e86175c69c48f3bcef6 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Tue, 24 Dec 2019 18:05:14 +0300 Subject: [PATCH] - fix column type detection of primary UUID columns for SQLite --- src/Driver/SQLite/Schema/SQLiteColumn.php | 10 +++++--- src/Schema/AbstractColumn.php | 24 +++++++++---------- src/Schema/AbstractTable.php | 1 + tests/Database/ConsistencyTest.php | 28 ++++++++++++++++++++++- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/Driver/SQLite/Schema/SQLiteColumn.php b/src/Driver/SQLite/Schema/SQLiteColumn.php index 25171b1b..5e76b453 100644 --- a/src/Driver/SQLite/Schema/SQLiteColumn.php +++ b/src/Driver/SQLite/Schema/SQLiteColumn.php @@ -108,6 +108,7 @@ class SQLiteColumn extends AbstractColumn 'time' => ['time'], 'timestamp' => ['timestamp'], 'binary' => ['blob'], + 'string' => ['varchar'] ]; /** @@ -125,7 +126,7 @@ class SQLiteColumn extends AbstractColumn */ public function getAbstractType(): string { - if ($this->primaryKey) { + if ($this->primaryKey && $this->type === 'integer') { return 'primary'; } @@ -138,7 +139,7 @@ public function getAbstractType(): string public function sqlStatement(DriverInterface $driver): string { $statement = parent::sqlStatement($driver); - if ($this->getAbstractType() != 'enum') { + if ($this->getAbstractType() !== 'enum') { return $statement; } @@ -167,7 +168,10 @@ public static function createInstance( $column->nullable = !$schema['notnull']; $column->type = $schema['type']; - $column->primaryKey = (bool)$schema['pk']; + + if ((bool)$schema['pk'] && $column->type === 'integer') { + $column->primaryKey = true; + } /* * Normalizing default value. diff --git a/src/Schema/AbstractColumn.php b/src/Schema/AbstractColumn.php index b34c4650..06359080 100644 --- a/src/Schema/AbstractColumn.php +++ b/src/Schema/AbstractColumn.php @@ -389,7 +389,7 @@ public function getDefaultValue() case 'float': return (float)$this->defaultValue; case 'bool': - if (is_string($this->defaultValue) && strtolower($this->defaultValue) == 'false') { + if (is_string($this->defaultValue) && strtolower($this->defaultValue) === 'false') { return false; } @@ -434,7 +434,7 @@ public function getType(): string { $schemaType = $this->getAbstractType(); foreach ($this->phpMapping as $phpType => $candidates) { - if (in_array($schemaType, $candidates)) { + if (in_array($schemaType, $candidates, true)) { return $phpType; } } @@ -465,23 +465,23 @@ public function getAbstractType(): string foreach ($this->reverseMapping as $type => $candidates) { foreach ($candidates as $candidate) { if (is_string($candidate)) { - if (strtolower($candidate) == strtolower($this->type)) { + if (strtolower($candidate) === strtolower($this->type)) { return $type; } continue; } - if (strtolower($candidate['type']) != strtolower($this->type)) { + if (strtolower($candidate['type']) !== strtolower($this->type)) { continue; } foreach ($candidate as $option => $required) { - if ($option == 'type') { + if ($option === 'type') { continue; } - if ($this->{$option} != $required) { + if ($this->{$option} !== $required) { continue 2; } } @@ -652,7 +652,7 @@ public function sqlStatement(DriverInterface $driver): string { $statement = [$driver->identifier($this->name), $this->type]; - if ($this->getAbstractType() == 'enum') { + if ($this->getAbstractType() === 'enum') { //Enum specific column options if (!empty($enumDefinition = $this->quoteEnum($driver))) { $statement[] = $enumDefinition; @@ -690,7 +690,7 @@ public function compare(AbstractColumn $initial): bool $difference = []; foreach ($columnVars as $name => $value) { - if (in_array($name, static::EXCLUDE_FROM_COMPARE)) { + if (in_array($name, static::EXCLUDE_FROM_COMPARE, true)) { continue; } @@ -703,7 +703,7 @@ public function compare(AbstractColumn $initial): bool continue; } - if ($value != $dbColumnVars[$name]) { + if ($value !== $dbColumnVars[$name]) { $difference[] = $name; } } @@ -748,15 +748,15 @@ protected function quoteDefault(DriverInterface $driver): string return $defaultValue->compile(new QueryBindings(), $driver->getCompiler()); } - if ($this->getType() == 'bool') { + if ($this->getType() === 'bool') { return $defaultValue ? 'TRUE' : 'FALSE'; } - if ($this->getType() == 'float') { + if ($this->getType() === 'float') { return sprintf('%F', $defaultValue); } - if ($this->getType() == 'int') { + if ($this->getType() === 'int') { return strval($defaultValue); } diff --git a/src/Schema/AbstractTable.php b/src/Schema/AbstractTable.php index e688439b..30d59a04 100644 --- a/src/Schema/AbstractTable.php +++ b/src/Schema/AbstractTable.php @@ -48,6 +48,7 @@ * @method AbstractColumn binary($column) * @method AbstractColumn tinyBinary($column) * @method AbstractColumn longBinary($column) + * @method AbstractColumn uuid($column) */ abstract class AbstractTable implements TableInterface, ElementInterface { diff --git a/tests/Database/ConsistencyTest.php b/tests/Database/ConsistencyTest.php index 1a30edea..0072c3ee 100644 --- a/tests/Database/ConsistencyTest.php +++ b/tests/Database/ConsistencyTest.php @@ -23,7 +23,6 @@ abstract class ConsistencyTest extends BaseTest public function setUp(): void { $this->database = $this->db(); - $this->database = $this->db(); } public function tearDown(): void @@ -356,9 +355,11 @@ public function testUuid(): void $column = $schema->uuid('target'); $schema->save(); + $schema = $this->schema('table'); $this->assertTrue($schema->exists()); $this->assertTrue($schema->column('target')->compare($column)); + $this->assertSame('string', $schema->column('target')->getType()); $this->database->table('table')->insertOne([ 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' @@ -368,4 +369,29 @@ public function testUuid(): void 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' ], $this->database->table('table')->select()->fetchAll()[0]); } + + public function testUuidPrimary(): void + { + $schema = $this->schema('table'); + $this->assertFalse($schema->exists()); + + $column = $schema->uuid('target')->nullable(false); + $schema->setPrimaryKeys(['target']); + $schema->save(); + + $schema = $this->schema('table'); + $this->assertTrue($schema->exists()); + + $this->assertTrue($schema->column('target')->compare($column)); + $this->assertSame('string', $schema->column('target')->getType()); + $this->assertSame(['target'], $schema->getPrimaryKeys()); + + $this->database->table('table')->insertOne([ + 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' + ]); + + $this->assertEquals([ + 'target' => 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11' + ], $this->database->table('table')->select()->fetchAll()[0]); + } }