Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniting drop statements #154

Merged
merged 1 commit into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ export interface RoleOptions {
}

export interface DropOptions {
ifExists?: boolean,
ifExists?: boolean
cascade?: boolean
}

export interface DropIndexOptions {
name?: string
concurrently?: boolean
ifExists?: boolean
cascade?: boolean
}

Expand Down Expand Up @@ -193,14 +200,14 @@ export type TriggerOptions = TriggerOptionsEn & FunctionOptions
export interface MigrationBuilder {
// Tables
createTable(tableName: Name, columns: ColumnDefinitions, options?: TableOptions): void
dropTable(tableName: Name): void
dropTable(tableName: Name, drop_options: DropOptions): void
renameTable(tablename: Name, new_tablename: Name): void

// Columns
addColumns(tablename: Name, new_columns: ColumnDefinitions): void
addColumn(tablename: Name, new_columns: ColumnDefinitions): void
dropColumns(tablename: Name, columns: string | string[] | { [name: string]: any }): void
dropColumn(tablename: Name, columns: string | string[] | { [name: string]: any }): void
dropColumns(tablename: Name, columns: string | string[] | { [name: string]: any }, drop_options: DropOptions): void
dropColumn(tablename: Name, columns: string | string[] | { [name: string]: any }, drop_options: DropOptions): void
renameColumn(tablename: Name, old_column_name: string, new_column_name: string): void
alterColumn(tableName: Name, columnName: string, options: ColumnOptions): void

Expand All @@ -213,17 +220,17 @@ export interface MigrationBuilder {
// Indexes
createIndex(tableName: Name, columns: string | string[], options?: CreateIndexOptions): void
addIndex(tableName: Name, columns: string | string[], options?: CreateIndexOptions): void
dropIndex(tableName: Name, columns: string | string[], options?: { name?: string }): void
dropIndex(tableName: Name, columns: string | string[], options?: DropIndexOptions): void

// Extensions
createExtension(extension: string | string[]): void
addExtension(extension: string | string[]): void
dropExtension(extension: string | string[]): void
dropExtension(extension: string | string[], drop_options: DropOptions): void

// Types
createType(type_name: Name, values: Value[] | { [name: string]: Type }): void
addType(type_name: Name, values: Value[] | { [name: string]: Type }): void
dropType(type_name: Name): void
dropType(type_name: Name, drop_options: DropOptions): void
renameType(type_name: Name, new_type_name: Name): void
addTypeAttribute(type_name: Name, attribute_name: string, attribute_type: Type): void
dropTypeAttribute(type_name: Name, attribute_name: string, options: { ifExists?: boolean }): void
Expand All @@ -233,7 +240,7 @@ export interface MigrationBuilder {

// Roles
createRole(role_name: Name, role_options: RoleOptions): void
dropRole(role_name: Name): void
dropRole(role_name: Name, options: { ifExists?: boolean }): void
alterRole(role_name: Name, role_options: RoleOptions): void
renameRole(old_role_name: Name, new_role_name: Name): void

Expand Down
4 changes: 2 additions & 2 deletions lib/operations/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export const create = (extensions) => {
return _.map(extensions, extension => template`CREATE EXTENSION "${extension}";`);
};

export const drop = (extensions) => {
export const drop = (extensions, { ifExists, cascade } = {}) => {
if (!_.isArray(extensions)) extensions = [extensions]; // eslint-disable-line no-param-reassign
return _.map(extensions, extension => template`DROP EXTENSION "${extension}";`);
return _.map(extensions, extension => template`DROP EXTENSION${ifExists ? ' IF EXISTS' : ''} "${extension}"${cascade ? ' CASCADE' : ''};`);
};

// setup reverse functions
Expand Down
9 changes: 8 additions & 1 deletion lib/operations/indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ export const create = (table_name, columns, options = {}) => {
return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${table_name}"${method} (${columnsString})${where};`;
};

export const drop = (table_name, columns, options = {}) => `DROP INDEX "${generateIndexName(table_name, columns, options)}";`;
export const drop = (table_name, columns, options = {}) => {
const {
concurrently,
ifExists,
cascade,
} = options;
return `DROP INDEX${concurrently ? ' CONCURRENTLY' : ''}${ifExists ? ' IF EXISTS' : ''} "${generateIndexName(table_name, columns, options)}"${cascade ? ' CASCADE' : ''};`;
};

// setup reverse functions
create.reverse = drop;
3 changes: 2 additions & 1 deletion lib/operations/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export const create = (role_name, role_options = {}) => {
return template`CREATE ROLE "${role_name}"${options ? ` WITH ${options}` : ''};`;
};

export const drop = role_name => template`DROP ROLE "${role_name}";`;
export const drop = (role_name, { ifExists } = {}) =>
template`DROP ROLE${ifExists ? ' IF EXISTS' : ''} "${role_name}";`;

export const alter = (role_name, role_options = {}) => {
const options = formatRoleOptions(role_options);
Expand Down
4 changes: 2 additions & 2 deletions lib/operations/types.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _ from 'lodash';
import { template, applyType, escapeValue } from '../utils';

export const drop = type_name =>
template`DROP TYPE "${type_name}";`;
export const drop = (type_name, { ifExists, cascade } = {}) =>
template`DROP TYPE${ifExists ? ' IF EXISTS' : ''} "${type_name}"${cascade ? ' CASCADE' : ''};`;

export const create = (type_shorthands) => {
const _create = (type_name, options) => {
Expand Down