Skip to content

Commit

Permalink
Merge pull request #13 from farzai/fix-deprecated-column-type
Browse files Browse the repository at this point in the history
Add getColumns
  • Loading branch information
parsilver authored Jan 10, 2024
2 parents 91bb240 + 03a4f49 commit c89c7d8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Contracts/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public function performQuery(string $query): array;
* @return array<string>
*/
public function getTables(): array;

/**
* Get all columns with types in the given table.
*
* @return array<string, string>
*/
public function getColumns(string $table): array;
}
25 changes: 25 additions & 0 deletions src/Database/DoctrineConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Farzai\Viola\Database;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;
use Farzai\Viola\Contracts\Database\ConnectionInterface;

final class DoctrineConnection implements ConnectionInterface
Expand Down Expand Up @@ -55,4 +56,28 @@ public function getTables(): array
{
return $this->connection->createSchemaManager()->listTableNames();
}

/**
* Get all columns with types in the given table.
*
* @return array<string, string>
*/
public function getColumns(string $table): array
{
$columns = $this->connection->createSchemaManager()->listTableColumns($table);

$columnsWithType = [];

foreach ($columns as $column) {
$type = basename(str_replace('\\', '/', get_class($column->getType())));

// Remove the "Type" suffix from the type name.
$type = str_replace('Type', '', $type);
$type = strtolower($type);

$columnsWithType[$column->getName()] = $type;
}

return $columnsWithType;
}
}
8 changes: 4 additions & 4 deletions src/Storage/CacheFilesystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct($prefix = '')
public function get(string $key, mixed $default = null): mixed
{
$item = $this->cache->getItem($key);

if ($item->isHit()) {
return $item->get();
}
Expand All @@ -40,10 +41,9 @@ public function set(string $key, mixed $value): void
{
$item = $this->cache->getItem($key);

$item->set($value);

// 5 years
$item->expiresAfter(60 * 60 * 24 * 365 * 5);
$item
->set($value)
->expiresAfter(new \DateInterval('P5Y'));

$this->cache->save($item);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,27 @@

$connection->performQuery('SELECT * FROM users');
})->throws(\Exception::class, "Error Processing Query: \nSELECT * FROM users\n\nInvalid Query");

it('should get the correct columns from table successfully', function () {
$schemaManager = $this->createMock(\Doctrine\DBAL\Schema\AbstractSchemaManager::class);
$schemaManager->expects($this->once())
->method('listTableColumns')
->willReturn([
new \Doctrine\DBAL\Schema\Column('id', new \Doctrine\DBAL\Types\IntegerType()),
new \Doctrine\DBAL\Schema\Column('name', new \Doctrine\DBAL\Types\StringType()),
new \Doctrine\DBAL\Schema\Column('email', new \Doctrine\DBAL\Types\StringType()),
]);

$mysqlConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
$mysqlConnection->expects($this->once())
->method('createSchemaManager')
->willReturn($schemaManager);

$connection = new DoctrineConnection($mysqlConnection);

expect($connection->getColumns('users'))->toBe([
'id' => 'integer',
'name' => 'string',
'email' => 'string',
]);
});

0 comments on commit c89c7d8

Please sign in to comment.