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 5 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
51 changes: 51 additions & 0 deletions tests/Functional/Driver/PDO/PgSQL/DBAL6044Test.php
@@ -0,0 +1,51 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Driver\PDO\PgSQL;

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

/** @requires extension pdo_pgsql */
f-lombardo marked this conversation as resolved.
Show resolved Hide resolved
class DBAL6044Test extends FunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

if (TestUtil::isDriverOneOf('pdo_pgsql')) {
f-lombardo marked this conversation as resolved.
Show resolved Hide resolved
return;
}

self::markTestSkipped('This test requires the pdo_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();
$validationTable = $validationSchema->getTable($unloggedTable->getName());
$this->assertEquals($unloggedTable->getName(), $validationTable->getName());
derrabus marked this conversation as resolved.
Show resolved Hide resolved

$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