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

Adding LIKE options when creating table #394

Merged
merged 1 commit into from
Feb 8, 2019
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
8 changes: 7 additions & 1 deletion docs/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
- `ifNotExists` _[bool]_ - default false
- `inherits` _[[Name](migrations.md#type)]_ - table(s) to inherit from
- `constraints` _[object]_ - table constraints see [add constraint](constraints.md#pgmaddconstraint-tablename-constraint_name-expression-)
- `like` _[[Name](migrations.md#type)]_ - table(s) to inherit from
- `like` either:
- _[[Name](migrations.md#type)]_ - table(s) to inherit from
- or _[object]_
- `table` _[[Name](migrations.md#type)]_ - table(s) to inherit from
- `options` _[object]_ - like options (optional)
- `including` _[string or array of strings]_ - 'COMMENTS', 'CONSTRAINTS', 'DEFAULTS', 'IDENTITY', 'INDEXES', 'STATISTICS', 'STORAGE', 'ALL'
- `excluding` _[string or array of strings]_ - 'COMMENTS', 'CONSTRAINTS', 'DEFAULTS', 'IDENTITY', 'INDEXES', 'STATISTICS', 'STORAGE', 'ALL'
- `comment` _[string]_ - adds comment on table

**Reverse Operation:** `dropTable`
Expand Down
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ export interface ColumnDefinition extends ReferencesOptions {
comment?: string | null
}

export type Like = 'COMMENTS' | 'CONSTRAINTS' | 'DEFAULTS' | 'IDENTITY' | 'INDEXES' | 'STATISTICS' | 'STORAGE' | 'ALL'

export interface LikeOptions {
including?: Like | Like[]
excluding?: Like | Like[]
}

export interface TableOptions {
temporary?: boolean
ifNotExists?: boolean
inherits?: Name
like?: Name
like?: Name | { table: Name, options?: LikeOptions }
constraints?: ConstraintOptions
comment?: string | null
}
Expand Down
18 changes: 17 additions & 1 deletion lib/operations/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ const parseConstraints = (table, options, genName) => {
: constraints;
};

const parseLike = like => {
const formatOptions = (name, options) =>
(_.isArray(options) ? options : [options])
.map(option => ` ${name} ${option}`)
.join("");

const table = typeof like === "string" || !like.table ? like : like.table;
const options = like.options
? [
formatOptions("INCLUDING", like.options.including),
formatOptions("EXCLUDING", like.options.excluding)
].join("")
: "";
return template`LIKE "${table}"${options}`;
};

// TABLE
function dropTable(tableName, { ifExists, cascade } = {}) {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
Expand Down Expand Up @@ -221,7 +237,7 @@ function createTable(typeShorthands) {
const constraints = { ...optionsConstraints, ...columnsConstraints };
const constraintLines = parseConstraints(tableName, constraints, true);
const tableDefinition = [...columnLines, ...constraintLines].concat(
like ? [template`LIKE "${like}"`] : []
like ? [parseLike(like)] : []
);

const temporaryStr = temporary ? " TEMPORARY" : "";
Expand Down
17 changes: 17 additions & 0 deletions test/migrations/076_create_table_like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
exports.up = pgm => {
pgm.createTable(
"t_like",
{},
{
like: {
table: "t1",
options: {
including: ["COMMENTS", "CONSTRAINTS"],
excluding: ["STATISTICS", "STORAGE"]
}
}
}
);
};

exports.down = () => null;