Skip to content

Commit 1dbb2f7

Browse files
authored
Add citext column type support for PostgreSQL (#1069)
The citext extension type is already supported end-to-end by the Cake core schema dialect (DDL generation and reflection), but the migrations adapter did not expose a constant and rejected the type in isValidColumnType(). Add TYPE_CITEXT to AdapterInterface, a Column::CITEXT alias, and allow it through PostgresAdapter.
1 parent 7e46d61 commit 1dbb2f7

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

docs/en/writing-migrations.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,11 @@ however with the MySQL adapter + MariaDB, the `nativeuuid` type maps to
264264
a native uuid column instead of `CHAR(36)` like `uuid` does.
265265

266266
In addition, the Postgres adapter supports `interval`, `json`, `jsonb`,
267-
`uuid`, `cidr`, `inet` and `macaddr` column types (PostgreSQL 9.3 and
268-
above).
267+
`uuid`, `cidr`, `inet`, `macaddr` and `citext` column types (PostgreSQL 9.3
268+
and above). The `citext` type requires the `citext` extension to be enabled
269+
on the database; the migration does not create the extension automatically,
270+
run `CREATE EXTENSION IF NOT EXISTS citext` first (for example from an
271+
earlier migration via `$this->execute(...)`).
269272

270273
### Valid Column Options
271274

src/Db/Adapter/AdapterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ interface AdapterInterface
9494

9595
public const TYPE_MACADDR = TableSchemaInterface::TYPE_MACADDR;
9696

97+
public const TYPE_CITEXT = TableSchemaInterface::TYPE_CITEXT;
98+
9799
public const TYPE_INTERVAL = TableSchemaInterface::TYPE_INTERVAL;
98100

99101
/**

src/Db/Adapter/PostgresAdapter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class PostgresAdapter extends AbstractAdapter
5353
self::TYPE_CIDR,
5454
self::TYPE_INET,
5555
self::TYPE_MACADDR,
56+
self::TYPE_CITEXT,
5657
self::TYPE_INTERVAL,
5758
self::TYPE_BINARY_UUID,
5859
self::TYPE_NATIVE_UUID,

src/Db/Table/Column.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class Column extends DatabaseColumn
9292
/** Postgres-only column type */
9393
public const MACADDR = TableSchemaInterface::TYPE_MACADDR;
9494

95+
/** Postgres-only column type, requires the `citext` extension */
96+
public const CITEXT = TableSchemaInterface::TYPE_CITEXT;
97+
9598
/** Postgres-only column type */
9699
public const INTERVAL = TableSchemaInterface::TYPE_INTERVAL;
97100

tests/TestCase/Db/Adapter/PostgresAdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,34 @@ public function testAddColumnJsonbCompat(): void
669669
$this->assertTrue($table->hasColumn('config'));
670670
}
671671

672+
/**
673+
* Test that adding a column with the citext type works.
674+
*
675+
* Requires the `citext` extension to be enabled on the database; the setUp
676+
* above creates the extension when missing.
677+
*/
678+
public function testAddColumnCitext(): void
679+
{
680+
$table = new Table('table1', [], $this->adapter);
681+
$table->save();
682+
$this->assertFalse($table->hasColumn('nickname'));
683+
$table->addColumn('nickname', 'citext', ['null' => true])
684+
->save();
685+
$this->assertTrue($table->hasColumn('nickname'));
686+
687+
$columns = $this->adapter->getColumns('table1');
688+
$nickname = null;
689+
foreach ($columns as $column) {
690+
if ($column->getName() === 'nickname') {
691+
$nickname = $column;
692+
break;
693+
}
694+
}
695+
$this->assertNotNull($nickname);
696+
$this->assertSame('citext', $nickname->getType());
697+
$this->assertTrue($nickname->isNull());
698+
}
699+
672700
public static function providerAddColumnIdentity(): array
673701
{
674702
return [

tests/TestCase/Db/Adapter/SqliteAdapterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,7 @@ public static function provideColumnTypesForValidation(): array
27452745
[SqliteAdapter::TYPE_UUID, true],
27462746
[SqliteAdapter::TYPE_TIMESTAMP, true],
27472747
[SqliteAdapter::TYPE_CIDR, false],
2748+
[SqliteAdapter::TYPE_CITEXT, false],
27482749
[SqliteAdapter::TYPE_DECIMAL, true],
27492750
[SqliteAdapter::TYPE_GEOMETRY, false],
27502751
[SqliteAdapter::TYPE_INET, false],

0 commit comments

Comments
 (0)