diff --git a/docs/en/writing-migrations.md b/docs/en/writing-migrations.md index b415820e..f28df090 100644 --- a/docs/en/writing-migrations.md +++ b/docs/en/writing-migrations.md @@ -264,8 +264,11 @@ however with the MySQL adapter + MariaDB, the `nativeuuid` type maps to a native uuid column instead of `CHAR(36)` like `uuid` does. In addition, the Postgres adapter supports `interval`, `json`, `jsonb`, -`uuid`, `cidr`, `inet` and `macaddr` column types (PostgreSQL 9.3 and -above). +`uuid`, `cidr`, `inet`, `macaddr` and `citext` column types (PostgreSQL 9.3 +and above). The `citext` type requires the `citext` extension to be enabled +on the database; the migration does not create the extension automatically, +run `CREATE EXTENSION IF NOT EXISTS citext` first (for example from an +earlier migration via `$this->execute(...)`). ### Valid Column Options diff --git a/src/Db/Adapter/AdapterInterface.php b/src/Db/Adapter/AdapterInterface.php index 26dfa543..48cad38c 100644 --- a/src/Db/Adapter/AdapterInterface.php +++ b/src/Db/Adapter/AdapterInterface.php @@ -94,6 +94,8 @@ interface AdapterInterface public const TYPE_MACADDR = TableSchemaInterface::TYPE_MACADDR; + public const TYPE_CITEXT = TableSchemaInterface::TYPE_CITEXT; + public const TYPE_INTERVAL = TableSchemaInterface::TYPE_INTERVAL; /** diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index 3ff57a1f..89fc010b 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -53,6 +53,7 @@ class PostgresAdapter extends AbstractAdapter self::TYPE_CIDR, self::TYPE_INET, self::TYPE_MACADDR, + self::TYPE_CITEXT, self::TYPE_INTERVAL, self::TYPE_BINARY_UUID, self::TYPE_NATIVE_UUID, diff --git a/src/Db/Table/Column.php b/src/Db/Table/Column.php index 0fbee5f8..f49ff559 100644 --- a/src/Db/Table/Column.php +++ b/src/Db/Table/Column.php @@ -92,6 +92,9 @@ class Column extends DatabaseColumn /** Postgres-only column type */ public const MACADDR = TableSchemaInterface::TYPE_MACADDR; + /** Postgres-only column type, requires the `citext` extension */ + public const CITEXT = TableSchemaInterface::TYPE_CITEXT; + /** Postgres-only column type */ public const INTERVAL = TableSchemaInterface::TYPE_INTERVAL; diff --git a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php index 8e9fb253..e6a4e3ea 100644 --- a/tests/TestCase/Db/Adapter/PostgresAdapterTest.php +++ b/tests/TestCase/Db/Adapter/PostgresAdapterTest.php @@ -669,6 +669,34 @@ public function testAddColumnJsonbCompat(): void $this->assertTrue($table->hasColumn('config')); } + /** + * Test that adding a column with the citext type works. + * + * Requires the `citext` extension to be enabled on the database; the setUp + * above creates the extension when missing. + */ + public function testAddColumnCitext(): void + { + $table = new Table('table1', [], $this->adapter); + $table->save(); + $this->assertFalse($table->hasColumn('nickname')); + $table->addColumn('nickname', 'citext', ['null' => true]) + ->save(); + $this->assertTrue($table->hasColumn('nickname')); + + $columns = $this->adapter->getColumns('table1'); + $nickname = null; + foreach ($columns as $column) { + if ($column->getName() === 'nickname') { + $nickname = $column; + break; + } + } + $this->assertNotNull($nickname); + $this->assertSame('citext', $nickname->getType()); + $this->assertTrue($nickname->isNull()); + } + public static function providerAddColumnIdentity(): array { return [ diff --git a/tests/TestCase/Db/Adapter/SqliteAdapterTest.php b/tests/TestCase/Db/Adapter/SqliteAdapterTest.php index a66031d5..240de94e 100644 --- a/tests/TestCase/Db/Adapter/SqliteAdapterTest.php +++ b/tests/TestCase/Db/Adapter/SqliteAdapterTest.php @@ -2745,6 +2745,7 @@ public static function provideColumnTypesForValidation(): array [SqliteAdapter::TYPE_UUID, true], [SqliteAdapter::TYPE_TIMESTAMP, true], [SqliteAdapter::TYPE_CIDR, false], + [SqliteAdapter::TYPE_CITEXT, false], [SqliteAdapter::TYPE_DECIMAL, true], [SqliteAdapter::TYPE_GEOMETRY, false], [SqliteAdapter::TYPE_INET, false],