-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathschemas.ts
50 lines (36 loc) · 1.6 KB
/
schemas.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
43
44
45
46
47
48
49
50
import type { MigrationOptions } from '../types';
import type { CreateSchema, DropSchema, RenameSchema } from './schemasTypes';
export type { CreateSchema, DropSchema, RenameSchema };
export function dropSchema(mOptions: MigrationOptions): DropSchema {
const _drop: DropSchema = (schemaName, options = {}) => {
const { ifExists, cascade } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const schemaNameStr = mOptions.literal(schemaName);
return `DROP SCHEMA${ifExistsStr} ${schemaNameStr}${cascadeStr};`;
};
return _drop;
}
export function createSchema(mOptions: MigrationOptions): CreateSchema {
const _create: CreateSchema = (schemaName: string, options = {}) => {
const { ifNotExists, authorization } = options;
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const schemaNameStr = mOptions.literal(schemaName);
const authorizationStr = authorization
? ` AUTHORIZATION ${authorization}`
: '';
return `CREATE SCHEMA${ifNotExistsStr} ${schemaNameStr}${authorizationStr};`;
};
_create.reverse = dropSchema(mOptions);
return _create;
}
export function renameSchema(mOptions: MigrationOptions): RenameSchema {
const _rename: RenameSchema = (schemaName, newSchemaName) => {
const schemaNameStr = mOptions.literal(schemaName);
const newSchemaNameStr = mOptions.literal(newSchemaName);
return `ALTER SCHEMA ${schemaNameStr} RENAME TO ${newSchemaNameStr};`;
};
_rename.reverse = (schemaName, newSchemaName) =>
_rename(newSchemaName, schemaName);
return _rename;
}