-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Alter type functions * updated readme
- Loading branch information
Showing
5 changed files
with
132 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Jan Dolezel <[email protected]> | ||
|
||
Copyright (c) 2014 Theo Ephraim | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable strict */ | ||
/* eslint-disable strict,prefer-destructuring */ | ||
|
||
'use strict'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,65 @@ | ||
import _ from 'lodash'; | ||
import { template, applyType } from '../utils'; | ||
import { template, applyType, escapeValue } from '../utils'; | ||
|
||
export const drop = type_name => | ||
template`DROP TYPE "${type_name}";`; | ||
|
||
export const create = (type_shorthands) => { | ||
const _create = (type_name, options) => { | ||
if (_.isArray(options)) { | ||
return template`CREATE TYPE "${type_name}" AS ENUM ('${options.join('\', \'')}');`; | ||
return template`CREATE TYPE "${type_name}" AS ENUM (${options.map(escapeValue).join(', ')});`; | ||
} | ||
const columns = _.map(options, (column, column_name) => | ||
template`"${column_name}" ${applyType(column, type_shorthands).type}` | ||
const attributes = _.map(options, (attribute, attribute_name) => | ||
template`"${attribute_name}" ${applyType(attribute, type_shorthands).type}` | ||
).join(',\n'); | ||
return template`CREATE TYPE "${type_name}" AS (\n${columns}\n);`; | ||
return template`CREATE TYPE "${type_name}" AS (\n${attributes}\n);`; | ||
}; | ||
_create.reverse = drop; | ||
return _create; | ||
}; | ||
|
||
export const alter = () => null; | ||
export const dropTypeAttribute = (type_name, attribute_name, { ifExists } = {}) => | ||
template`ALTER TYPE "${type_name}" DROP ATTRIBUTE "${attribute_name}"${ifExists ? ' IF EXISTS' : ''};`; | ||
|
||
export const addTypeAttribute = (type_shorthands) => { | ||
const _alterAttributeAdd = (type_name, attribute_name, attribute_type) => | ||
template`ALTER TYPE "${type_name}" ADD ATTRIBUTE "${attribute_name}" ${applyType(attribute_type, type_shorthands).type};`; | ||
_alterAttributeAdd.reverse = dropTypeAttribute; | ||
return _alterAttributeAdd; | ||
}; | ||
|
||
export const setTypeAttribute = type_shorthands => | ||
(type_name, attribute_name, attribute_type) => | ||
template`ALTER TYPE "${type_name}" ALTER ATTRIBUTE "${attribute_name}" SET DATA TYPE ${applyType(attribute_type, type_shorthands).type};`; | ||
|
||
export const addTypeValue = (type_name, value, options = {}) => { | ||
const { | ||
ifNotExists, | ||
before, | ||
after, | ||
} = options; | ||
|
||
if (before && after) { | ||
throw new Error('"before" and "after" can\'t be specified together'); | ||
} | ||
const beforeClause = before ? ` BEFORE ${before}` : ''; | ||
const afterClause = after ? ` BEFORE ${after}` : ''; | ||
|
||
return template`ALTER TYPE "${type_name}" ADD VALUE${ifNotExists ? ' IF NOT EXISTS' : ''} ${escapeValue(value)}${beforeClause}${afterClause};`; | ||
}; | ||
|
||
// RENAME | ||
export const rename = (type_name, new_type_name) => | ||
template`ALTER TYPE "${type_name}" RENAME TO "${new_type_name}";`; | ||
|
||
export const undoRename = (type_name, new_type_name) => | ||
rename(new_type_name, type_name); | ||
|
||
export const renameTypeAttribute = (type_name, attribute_name, new_attribute_name) => | ||
template`ALTER TYPE "${type_name}" RENAME ATTRIBUTE "${attribute_name}" TO "${new_attribute_name}";`; | ||
|
||
export const undoRenameTypeAttribute = (type_name, attribute_name, new_attribute_name) => | ||
renameTypeAttribute(type_name, new_attribute_name, attribute_name); | ||
|
||
rename.reverse = undoRename; | ||
renameTypeAttribute.reverse = undoRenameTypeAttribute; |