Skip to content

Commit

Permalink
Add support for unlogged tables in PostgreSQL (#6046)
Browse files Browse the repository at this point in the history
|      Q       |   A
|------------- | -----------
| Type         | bug/feature/improvement
| Feature | see #6044

#### Summary

Add support for creating unlogged PostgreSQL tables:
https://www.postgresql.org/docs/current/sql-createtable.html

I added a boolean `unlogged` option to `default_table_options` doctrine
configuration

https://www.doctrine-project.org/projects/doctrine-bundle/en/latest/configuration.html
For example:

```yaml
doctrine:
    dbal:
        default_table_options:
            unlogged: true
```

---------

Co-authored-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
f-lombardo and derrabus committed Jun 7, 2023
1 parent 01c4ce1 commit f97f825
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Platforms/PostgreSQLPlatform.php
Expand Up @@ -822,7 +822,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 @@ -732,6 +732,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

0 comments on commit f97f825

Please sign in to comment.