-
Notifications
You must be signed in to change notification settings - Fork 169
/
dropIndex.ts
42 lines (34 loc) · 1.19 KB
/
dropIndex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { MigrationOptions } from '../../types';
import type { DropOptions, Name } from '../generalTypes';
import type { IndexColumn } from './shared';
import { generateIndexName } from './shared';
export interface DropIndexOptions extends DropOptions {
unique?: boolean;
name?: string;
concurrently?: boolean;
}
export type DropIndex = (
tableName: Name,
columns: string | Array<string | IndexColumn>,
options?: DropIndexOptions
) => string | string[];
export function dropIndex(mOptions: MigrationOptions): DropIndex {
const _drop: DropIndex = (tableName, rawColumns, options = {}) => {
const { concurrently, ifExists, cascade } = options;
const columns = Array.isArray(rawColumns)
? rawColumns.slice()
: [rawColumns];
const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const indexName = generateIndexName(
tableName,
columns,
options,
mOptions.schemalize
);
const cascadeStr = cascade ? ' CASCADE' : '';
const indexNameStr = mOptions.literal(indexName);
return `DROP INDEX${concurrentlyStr}${ifExistsStr} ${indexNameStr}${cascadeStr};`;
};
return _drop;
}