Skip to content

Commit

Permalink
refactor(schemas): reorganize operations schemas (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 10, 2024
1 parent 550b31f commit 58b6010
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type {
CreateSchemaOptions,
DropSchema,
RenameSchema,
} from './operations/schemasTypes';
} from './operations/schemas';
export type {
AlterSequence,
CreateSequence,
Expand Down
50 changes: 0 additions & 50 deletions src/operations/schemas.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/operations/schemas/createSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions, IfNotExistsOption } from '../generalTypes';
import { dropSchema } from './dropSchema';

export interface CreateSchemaOptions extends IfNotExistsOption {
authorization?: string;
}

export type CreateSchemaFn = (
schemaName: string,
schemaOptions?: CreateSchemaOptions & DropOptions
) => string | string[];

export type CreateSchema = CreateSchemaFn & { reverse: CreateSchemaFn };

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;
}
21 changes: 21 additions & 0 deletions src/operations/schemas/dropSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions } from '../generalTypes';

export type DropSchema = (
schemaName: string,
dropOptions?: DropOptions
) => string | string[];

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;
}
3 changes: 3 additions & 0 deletions src/operations/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './createSchema';
export * from './dropSchema';
export * from './renameSchema';
22 changes: 22 additions & 0 deletions src/operations/schemas/renameSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { MigrationOptions } from '../../types';

export type RenameSchemaFn = (
oldSchemaName: string,
newSchemaName: string
) => string | string[];

export type RenameSchema = RenameSchemaFn & { reverse: RenameSchemaFn };

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;
}
24 changes: 0 additions & 24 deletions src/operations/schemasTypes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type * as operators from './operations/operators';
import type PgLiteral from './operations/PgLiteral';
import type * as policies from './operations/policies';
import type * as roles from './operations/roles';
import type * as schemas from './operations/schemasTypes';
import type * as schemas from './operations/schemas';
import type * as sequences from './operations/sequencesTypes';
import type * as sql from './operations/sql';
import type * as tables from './operations/tablesTypes';
Expand Down

0 comments on commit 58b6010

Please sign in to comment.