Skip to content
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

Add support for unlogged tables in PostgreSQL #6046

Merged
merged 8 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Platforms/PostgreSQLPlatform.php
Expand Up @@ -821,7 +821,9 @@ protected function _getCreateTableSQL($name, array $columns, array $options = []
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}

$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
$unlogged = isset($options['unlogged']) && $options['unlogged'] === true ? ' UNLOGGED' : '';

$query = 'CREATE' . $unlogged . ' TABLE ' . $name . ' (' . $queryFields . ')';

$sql = [$query];

Expand Down
1 change: 1 addition & 0 deletions src/Schema/PostgreSQLSchemaManager.php
Expand Up @@ -733,6 +733,7 @@ protected function fetchTableOptionsByTable(string $databaseName, ?string $table
{
$sql = <<<'SQL'
SELECT c.relname,
CASE c.relpersistence WHEN 'u' THEN true ELSE false END as unlogged,
obj_description(c.oid, 'pg_class') AS comment
FROM pg_class c
INNER JOIN pg_namespace n
Expand Down
52 changes: 52 additions & 0 deletions tests/Functional/Driver/DBAL6044Test.php
@@ -0,0 +1,52 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Driver;

use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;

class DBAL6044Test extends FunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

if (TestUtil::isDriverOneOf('pdo_pgsql', 'pgsql')) {
return;
}

self::markTestSkipped('This test requires the pdo_pgsql or the pgsql driver.');
}

public function testUnloggedTables(): void
{
$unloggedTable = new Table('my_unlogged');
$unloggedTable->addOption('unlogged', true);
$unloggedTable->addColumn('foo', 'string');
$this->dropAndCreateTable($unloggedTable);

$loggedTable = new Table('my_logged');
$loggedTable->addColumn('foo', 'string');
$this->dropAndCreateTable($loggedTable);

$schemaManager = $this->connection->createSchemaManager();

$validationSchema = $schemaManager->introspectSchema();
$validationUnloggedTable = $validationSchema->getTable($unloggedTable->getName());
$this->assertTrue($validationUnloggedTable->getOption('unlogged'));
$validationLoggedTable = $validationSchema->getTable($loggedTable->getName());
$this->assertFalse($validationLoggedTable->getOption('unlogged'));

$sql = 'SELECT relpersistence FROM pg_class WHERE relname = ?';
$stmt = $this->connection->prepare($sql);

$stmt->bindValue(1, $unloggedTable->getName());
$unloggedTablePersistenceType = $stmt->executeQuery()->fetchOne();
$this->assertEquals('u', $unloggedTablePersistenceType);

$stmt->bindValue(1, $loggedTable->getName());
$loggedTablePersistenceType = $stmt->executeQuery()->fetchOne();
$this->assertEquals('p', $loggedTablePersistenceType);
}
}
12 changes: 12 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Expand Up @@ -187,6 +187,18 @@ public function testGenerateTableWithAutoincrement(): void
);
}

public function testGenerateUnloggedTable(): void
{
$table = new Table('mytable');
$table->addOption('unlogged', true);
$table->addColumn('foo', 'string');

self::assertEquals(
['CREATE UNLOGGED TABLE mytable (foo VARCHAR(255) NOT NULL)'],
$this->platform->getCreateTableSQL($table),
);
}

/** @return mixed[][] */
public static function serialTypes(): iterable
{
Expand Down