Skip to content

Commit 1c81b16

Browse files
feat: add nulls option to createIndex (#1489)
Co-authored-by: Breno A. <git@breno.tech>
1 parent f97819a commit 1c81b16

15 files changed

+136
-7
lines changed

docs/src/migrations/indexes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
| `ifNotExists` | `boolean` | default false |
2828
| `method` | `string` | btree \| hash \| gist \| spgist \| gin |
2929
| `include` | `string` or `array[string]` | columns to add to the include clause |
30+
| `nulls` | `string` | distinct \| not distinct (for unique indexes only) |
3031

3132
### Examples
3233

src/operations/indexes/createIndex.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface CreateIndexOptions extends IfNotExistsOption {
1818
method?: 'btree' | 'hash' | 'gist' | 'spgist' | 'gin';
1919

2020
include?: string | string[];
21+
22+
nulls?: 'distinct' | 'not distinct';
2123
}
2224

2325
export type CreateIndexFn = (
@@ -37,8 +39,15 @@ export function createIndex(mOptions: MigrationOptions): CreateIndex {
3739
method,
3840
where,
3941
include,
42+
nulls,
4043
} = options;
4144

45+
if (nulls && !unique) {
46+
throw new Error(
47+
'The "nulls" option can only be used with unique indexes.'
48+
);
49+
}
50+
4251
/*
4352
columns - the column, columns, or expression to create the index on
4453
@@ -49,6 +58,7 @@ export function createIndex(mOptions: MigrationOptions): CreateIndex {
4958
concurrently -
5059
ifNotExists - optionally create index
5160
options.method - [ btree | hash | gist | spgist | gin ]
61+
nulls - nulls distinct or not distinct (for unique indexes only)
5262
*/
5363
const columns = toArray(rawColumns);
5464

@@ -67,10 +77,11 @@ export function createIndex(mOptions: MigrationOptions): CreateIndex {
6777
const includeStr = include
6878
? ` INCLUDE (${toArray(include).map(mOptions.literal).join(', ')})`
6979
: '';
80+
const nullsStr = nulls ? ` NULLS ${nulls.toUpperCase()}` : '';
7081
const indexNameStr = mOptions.literal(indexName);
7182
const tableNameStr = mOptions.literal(tableName);
7283

73-
return `CREATE${uniqueStr} INDEX${concurrentlyStr}${ifNotExistsStr} ${indexNameStr} ON ${tableNameStr}${methodStr} (${columnsString})${includeStr}${whereStr};`;
84+
return `CREATE${uniqueStr} INDEX${concurrentlyStr}${ifNotExistsStr} ${indexNameStr} ON ${tableNameStr}${methodStr} (${columnsString})${includeStr}${nullsStr}${whereStr};`;
7485
};
7586

7687
_create.reverse = dropIndex(mOptions);

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-down.pg-13.stdout.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
> Migrating files:
2+
> - 095_index_nulls
23
> - 094_unlogged_table
34
> - 093_alter_column_expression
45
> - 092_partition
@@ -93,6 +94,10 @@
9394
> - 003_promise
9495
> - 002_callback
9596
> - 001_noop
97+
### MIGRATION 095_index_nulls (DOWN) ###
98+
DELETE FROM "public"."pgmigrations" WHERE name='095_index_nulls';
99+
100+
96101
### MIGRATION 094_unlogged_table (DOWN) ###
97102
DROP TABLE "t_unlogged";
98103
DROP TABLE "t_regular";

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-down.pg-14.stdout.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
> Migrating files:
2+
> - 095_index_nulls
23
> - 094_unlogged_table
34
> - 093_alter_column_expression
45
> - 092_partition
@@ -93,6 +94,10 @@
9394
> - 003_promise
9495
> - 002_callback
9596
> - 001_noop
97+
### MIGRATION 095_index_nulls (DOWN) ###
98+
DELETE FROM "public"."pgmigrations" WHERE name='095_index_nulls';
99+
100+
96101
### MIGRATION 094_unlogged_table (DOWN) ###
97102
DROP TABLE "t_unlogged";
98103
DROP TABLE "t_regular";

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-down.pg-15.stdout.log

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
> Migrating files:
2+
> - 095_index_nulls
23
> - 094_unlogged_table
34
> - 093_alter_column_expression
45
> - 092_partition
@@ -93,6 +94,11 @@
9394
> - 003_promise
9495
> - 002_callback
9596
> - 001_noop
97+
### MIGRATION 095_index_nulls (DOWN) ###
98+
DROP TABLE "t095";
99+
DELETE FROM "public"."pgmigrations" WHERE name='095_index_nulls';
100+
101+
96102
### MIGRATION 094_unlogged_table (DOWN) ###
97103
DROP TABLE "t_unlogged";
98104
DROP TABLE "t_regular";

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-down.pg-16.stdout.log

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
> Migrating files:
2+
> - 095_index_nulls
23
> - 094_unlogged_table
34
> - 093_alter_column_expression
45
> - 092_partition
@@ -93,6 +94,11 @@
9394
> - 003_promise
9495
> - 002_callback
9596
> - 001_noop
97+
### MIGRATION 095_index_nulls (DOWN) ###
98+
DROP TABLE "t095";
99+
DELETE FROM "public"."pgmigrations" WHERE name='095_index_nulls';
100+
101+
96102
### MIGRATION 094_unlogged_table (DOWN) ###
97103
DROP TABLE "t_unlogged";
98104
DROP TABLE "t_regular";

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-down.pg-17.stdout.log

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
> Migrating files:
2+
> - 095_index_nulls
23
> - 094_unlogged_table
34
> - 093_alter_column_expression
45
> - 092_partition
@@ -93,6 +94,11 @@
9394
> - 003_promise
9495
> - 002_callback
9596
> - 001_noop
97+
### MIGRATION 095_index_nulls (DOWN) ###
98+
DROP TABLE "t095";
99+
DELETE FROM "public"."pgmigrations" WHERE name='095_index_nulls';
100+
101+
96102
### MIGRATION 094_unlogged_table (DOWN) ###
97103
DROP TABLE "t_unlogged";
98104
DROP TABLE "t_regular";

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-up.pg-13.stdout.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
> - 092_partition
9494
> - 093_alter_column_expression
9595
> - 094_unlogged_table
96+
> - 095_index_nulls
9697
### MIGRATION 001_noop (UP) ###
9798
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('001_noop', NOW());
9899

@@ -812,4 +813,8 @@ ALTER TABLE "t_regular"
812813
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('094_unlogged_table', NOW());
813814

814815

816+
### MIGRATION 095_index_nulls (UP) ###
817+
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('095_index_nulls', NOW());
818+
819+
815820
Migrations complete!

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-up.pg-14.stdout.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
> - 092_partition
9494
> - 093_alter_column_expression
9595
> - 094_unlogged_table
96+
> - 095_index_nulls
9697
### MIGRATION 001_noop (UP) ###
9798
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('001_noop', NOW());
9899

@@ -812,4 +813,8 @@ ALTER TABLE "t_regular"
812813
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('094_unlogged_table', NOW());
813814

814815

816+
### MIGRATION 095_index_nulls (UP) ###
817+
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('095_index_nulls', NOW());
818+
819+
815820
Migrations complete!

test/integration/__snapshots__/db-config-it.spec.ts.snap.d/migrations-up.pg-15.stdout.log

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
> - 092_partition
9494
> - 093_alter_column_expression
9595
> - 094_unlogged_table
96+
> - 095_index_nulls
9697
### MIGRATION 001_noop (UP) ###
9798
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('001_noop', NOW());
9899

@@ -812,4 +813,12 @@ ALTER TABLE "t_regular"
812813
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('094_unlogged_table', NOW());
813814

814815

816+
### MIGRATION 095_index_nulls (UP) ###
817+
CREATE TABLE "t095" (
818+
"id" integer
819+
);
820+
CREATE UNIQUE INDEX "t095_id_unique_index" ON "t095" ("id") NULLS NOT DISTINCT;
821+
INSERT INTO "public"."pgmigrations" (name, run_on) VALUES ('095_index_nulls', NOW());
822+
823+
815824
Migrations complete!

0 commit comments

Comments
 (0)