-
Notifications
You must be signed in to change notification settings - Fork 180
/
dropIndex.ts
41 lines (33 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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
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>,
dropOptions?: DropIndexOptions
) => string;
export function dropIndex(mOptions: MigrationOptions): DropIndex {
const _drop: DropIndex = (tableName, rawColumns, options = {}) => {
const { concurrently = false, ifExists = false, cascade = false } = options;
const columns = toArray(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;
}