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 creating temporary tables in schema #6409

Open
wants to merge 15 commits into
base: 4.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/en/reference/schema-representation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ and absolutely not portable.
- **engine** (string): The DB engine used for the table. Currently only supported on MySQL.

- **unlogged** (boolean): Set a PostgreSQL table type as
`unlogged <https://www.postgresql.org/docs/current/sql-createtable.htmll>`_
`unlogged <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-UNLOGGED>`_

- **temporary** (boolean): Set a PostgreSQL table type as
`temporary <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-TEMPORARY>`_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a PostgreSQL-specific concept to me 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it isn't per se but since I have access to PostgreSQL mainly, I implemented it just on the PostgreSQL platform.


- **on_commit** (string): Set a PostgreSQL table
`commit options <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-ON-COMMIT>`_,
only used if **temporary** is true

- ``preserve``: preserve rows on commit
- ``delete``: delete rows on commit
- ``drop``: drop rows on commit

Column
~~~~~~
Expand Down
11 changes: 10 additions & 1 deletion src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,18 @@
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}

$temporary = isset($options['temporary']) && $options['temporary'] === true ? ' TEMPORARY' : '';

$onCommit = $options['temporary'] ?? false ? match ($options['on_commit'] ?? '') {

Check failure on line 396 in src/Platforms/PostgreSQLPlatform.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

RiskyTruthyFalsyComparison

src/Platforms/PostgreSQLPlatform.php:396:21: RiskyTruthyFalsyComparison: Operand of type false|mixed contains type mixed, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
'preserve' => ' ON COMMIT PRESERVE ROWS',
'delete' => ' ON COMMIT DELETE ROWS',
'drop' => ' ON COMMIT DROP',
default => ''
} : '';

$unlogged = isset($options['unlogged']) && $options['unlogged'] === true ? ' UNLOGGED' : '';

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

$sql = [$query];

Expand Down
52 changes: 52 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use UnexpectedValueException;

Expand Down Expand Up @@ -191,6 +192,57 @@ public function testGenerateTableWithAutoincrement(): void
);
}

#[DataProvider('pgTemporaryProvider')]
public function testGenerateTemporaryTable(
bool $temporary,
string|null $onCommit,
string $expectedSQL,
): void {
$table = new Table('mytable');
$table->addOption('temporary', $temporary);
if ($onCommit !== null) {
$table->addOption('on_commit', $onCommit);
}

$table->addColumn('foo', 'string');

self::assertEquals(
[$expectedSQL],
$this->platform->getCreateTableSQL($table),
);
}

public static function pgTemporaryProvider(): Generator
{
yield 'temporary, no on commit option' => [true, null, 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'temporary, empty on commit option' =>
[true, '', 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'temporary, invalid on commit option' =>
[true, 'invalid', 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'non temporary' => [false, null, 'CREATE TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'temporary, preserve rows on commit' =>
[true, 'preserve', 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL) ON COMMIT PRESERVE ROWS'];

yield 'non temporary, preserve rows on commit omitted' =>
[false, 'preserve', 'CREATE TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'temporary, delete rows on commit' =>
[true, 'delete', 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL) ON COMMIT DELETE ROWS'];

yield 'non temporary, delete rows on commit omitted' =>
[false, 'delete', 'CREATE TABLE mytable (foo VARCHAR NOT NULL)'];

yield 'temporary, drop on commit' =>
[true, 'drop', 'CREATE TEMPORARY TABLE mytable (foo VARCHAR NOT NULL) ON COMMIT DROP'];

yield 'non temporary, drop on commit omitted' =>
[false, 'drop', 'CREATE TABLE mytable (foo VARCHAR NOT NULL)'];
}

public function testGenerateUnloggedTable(): void
{
$table = new Table('mytable');
Expand Down
Loading