Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from spiral/feature/sqlite-uuid
Browse files Browse the repository at this point in the history
- fix column type detection of primary UUID columns for SQLite
  • Loading branch information
wolfy-j authored Dec 24, 2019
2 parents b0fd54a + 358c1ea commit 1670ab6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/Driver/SQLite/Schema/SQLiteColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class SQLiteColumn extends AbstractColumn
'time' => ['time'],
'timestamp' => ['timestamp'],
'binary' => ['blob'],
'string' => ['varchar']
];

/**
Expand All @@ -125,7 +126,7 @@ class SQLiteColumn extends AbstractColumn
*/
public function getAbstractType(): string
{
if ($this->primaryKey) {
if ($this->primaryKey && $this->type === 'integer') {
return 'primary';
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions src/Schema/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -703,7 +703,7 @@ public function compare(AbstractColumn $initial): bool
continue;
}

if ($value != $dbColumnVars[$name]) {
if ($value !== $dbColumnVars[$name]) {
$difference[] = $name;
}
}
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions src/Schema/AbstractTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
28 changes: 27 additions & 1 deletion tests/Database/ConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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]);
}
}

0 comments on commit 1670ab6

Please sign in to comment.