diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 2ea43dc0b76..9c528438734 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -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]; diff --git a/src/Schema/PostgreSQLSchemaManager.php b/src/Schema/PostgreSQLSchemaManager.php index 9f96f5cff04..bd708e3533d 100644 --- a/src/Schema/PostgreSQLSchemaManager.php +++ b/src/Schema/PostgreSQLSchemaManager.php @@ -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 diff --git a/tests/Functional/Driver/DBAL6044Test.php b/tests/Functional/Driver/DBAL6044Test.php new file mode 100644 index 00000000000..fa0c7773028 --- /dev/null +++ b/tests/Functional/Driver/DBAL6044Test.php @@ -0,0 +1,52 @@ +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); + } +} diff --git a/tests/Platforms/PostgreSQLPlatformTest.php b/tests/Platforms/PostgreSQLPlatformTest.php index 26cbf7c7d39..67b05904421 100644 --- a/tests/Platforms/PostgreSQLPlatformTest.php +++ b/tests/Platforms/PostgreSQLPlatformTest.php @@ -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 {