Skip to content

Commit

Permalink
Using code directly for node >= 8
Browse files Browse the repository at this point in the history
  • Loading branch information
dolezel committed Jun 29, 2018
1 parent 2256844 commit 4dda6fc
Show file tree
Hide file tree
Showing 26 changed files with 1,711 additions and 868 deletions.
19 changes: 18 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2

node-image: &node-image
image: circleci/node:6
image: circleci/node:8

set-npm-global: &set-npm-global
run:
Expand Down Expand Up @@ -88,6 +88,20 @@ jobs:
- run:
name: test
command: npm run migrate up -- -m test/cockroach -s '' --migrations-schema circle_test --no-lock && npm run migrate down 0 -- -m test/cockroach -s '' --migrations-schema circle_test --no-lock --timestamps
test-node-6:
docker:
- image: circleci/node:6
environment:
- DATABASE_URL=postgres://ubuntu@localhost:5432/circle_test
- image: postgres:10.4-alpine
environment:
- POSTGRES_USER=ubuntu
- POSTGRES_DB=circle_test
steps:
- <<: *restore
- run:
name: test
command: npm run migrate up -- -m test/migrations && npm run migrate down 0 -- -m test/migrations --timestamps

workflows:
version: 2
Expand All @@ -106,3 +120,6 @@ workflows:
- test-cocroach:
requires:
- install
- test-node-6:
requires:
- install
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.*
*.log
migrations/
lib/
test/
mocha*
11 changes: 6 additions & 5 deletions bin/node-pg-migrate
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env node
/* eslint-disable strict,prefer-destructuring */
/* eslint-disable strict */

"use strict";

const util = require("util");
const path = require("path");
const yargs = require("yargs");
const { default: Migration } = require("../dist/migration"); // eslint-disable-line import/no-unresolved,import/extensions
const { default: migrationRunner } = require("../dist/runner"); // eslint-disable-line import/no-unresolved,import/extensions
const migrationRunner = require("../index");

const { Migration } = migrationRunner;

process.on("uncaughtException", err => {
console.log(err.stack);
Expand Down Expand Up @@ -39,7 +40,7 @@ const lockArg = "lock";
const timestampArg = "timestamp";
const dryRunArg = "dry-run";

const argv = yargs
const { argv } = yargs
.usage("Usage: $0 [up|down|create|redo] [migrationName] [options]")

.option("d", {
Expand Down Expand Up @@ -157,7 +158,7 @@ const argv = yargs
type: "boolean"
})

.help().argv;
.help();

if (argv.version) {
console.log(module.exports.version);
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable global-require */
const [major] = process.versions.node.split(".");
if (Number(major) >= 8) {
module.exports = require("./lib/runner");
} else {
module.exports = require("./dist/runner"); // eslint-disable-line import/no-unresolved
}
6 changes: 3 additions & 3 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
This file just manages the database connection and provides a query method
*/

import pg from "pg";
const pg = require("pg");
// or native libpq bindings
// import pg from 'pg/native';
// const pg = require("pg/native");

export default (connectionString, log = console.error) => {
module.exports = (connectionString, log = console.error) => {
const client = new pg.Client(connectionString);

let clientActive = false;
Expand Down
38 changes: 19 additions & 19 deletions lib/migration-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
and it makes inference of down migrations possible.
*/

import { PgLiteral } from "./utils";

import * as extensions from "./operations/extensions";
import * as indexes from "./operations/indexes";
import * as tables from "./operations/tables";
import * as types from "./operations/types";
import * as roles from "./operations/roles";
import * as functions from "./operations/functions";
import * as triggers from "./operations/triggers";
import * as schemas from "./operations/schemas";
import * as domains from "./operations/domains";
import * as sequences from "./operations/sequences";
import * as operators from "./operations/operators";
import * as policies from "./operations/policies";
import * as views from "./operations/views";
import * as mViews from "./operations/viewsMaterialized";
import * as other from "./operations/other";
const { PgLiteral } = require("./utils");

const extensions = require("./operations/extensions");
const indexes = require("./operations/indexes");
const tables = require("./operations/tables");
const types = require("./operations/types");
const roles = require("./operations/roles");
const functions = require("./operations/functions");
const triggers = require("./operations/triggers");
const schemas = require("./operations/schemas");
const domains = require("./operations/domains");
const sequences = require("./operations/sequences");
const operators = require("./operations/operators");
const policies = require("./operations/policies");
const views = require("./operations/views");
const mViews = require("./operations/viewsMaterialized");
const other = require("./operations/other");

/* eslint-disable security/detect-non-literal-fs-filename */
export default class MigrationBuilder {
module.exports = class MigrationBuilder {
constructor(typeShorthands, db) {
this._steps = [];
this._REVERSE_MODE = false;
Expand Down Expand Up @@ -193,5 +193,5 @@ export default class MigrationBuilder {
// in reverse mode, we flip the order of the statements
return this._REVERSE_MODE ? this._steps.slice().reverse() : this._steps;
}
}
};
/* eslint-enable security/detect-non-literal-fs-filename */
16 changes: 7 additions & 9 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
*/

import fs from "fs";
import mkdirp from "mkdirp";
import path from "path";
const fs = require("fs");
const mkdirp = require("mkdirp");
const path = require("path");

import MigrationBuilder from "./migration-builder";
import { getMigrationTableSchema } from "./utils";
const MigrationBuilder = require("./migration-builder");
const { getMigrationTableSchema } = require("./utils");

const SEPARATOR = "_";

class Migration {
module.exports = class Migration {
// class method that creates a new migration file by cloning the migration template
static create(name, directory, language) {
// ensure the migrations directory exists
Expand Down Expand Up @@ -127,6 +127,4 @@ class Migration {

return this._apply(this.down, pgm);
}
}

export default Migration;
};
17 changes: 12 additions & 5 deletions lib/operations/domains.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { template, applyType, escapeValue } from "../utils";
const { template, applyType, escapeValue } = require("../utils");

export function dropDomain(domainName, { ifExists, cascade } = {}) {
function dropDomain(domainName, { ifExists, cascade } = {}) {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
return template`DROP DOMAIN${ifExistsStr} "${domainName}"${cascadeStr};`;
}

export function createDomain(typeShorthands) {
function createDomain(typeShorthands) {
const _create = (domainName, type, options = {}) => {
const {
default: defaultValue,
Expand Down Expand Up @@ -48,7 +48,7 @@ export function createDomain(typeShorthands) {
return _create;
}

export function alterDomain(domainName, options) {
function alterDomain(domainName, options) {
const {
default: defaultValue,
notNull,
Expand Down Expand Up @@ -78,11 +78,18 @@ export function alterDomain(domainName, options) {
.join(";\n")};`;
}

export function renameDomain(domainName, newDomainName) {
function renameDomain(domainName, newDomainName) {
return template`ALTER DOMAIN "${domainName}" RENAME TO "${newDomainName}";`;
}

const undoRename = (domainName, newDomainName) =>
renameDomain(newDomainName, domainName);

renameDomain.reverse = undoRename;

module.exports = {
dropDomain,
createDomain,
alterDomain,
renameDomain
};
13 changes: 9 additions & 4 deletions lib/operations/extensions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import { template } from "../utils";
const _ = require("lodash");
const { template } = require("../utils");

export function createExtension(extensions, { ifNotExists, schema } = {}) {
function createExtension(extensions, { ifNotExists, schema } = {}) {
if (!_.isArray(extensions)) extensions = [extensions]; // eslint-disable-line no-param-reassign
return _.map(
extensions,
Expand All @@ -12,7 +12,7 @@ export function createExtension(extensions, { ifNotExists, schema } = {}) {
);
}

export function dropExtension(extensions, { ifExists, cascade } = {}) {
function dropExtension(extensions, { ifExists, cascade } = {}) {
if (!_.isArray(extensions)) extensions = [extensions]; // eslint-disable-line no-param-reassign
return _.map(
extensions,
Expand All @@ -25,3 +25,8 @@ export function dropExtension(extensions, { ifExists, cascade } = {}) {

// setup reverse functions
createExtension.reverse = dropExtension;

module.exports = {
createExtension,
dropExtension
};
14 changes: 10 additions & 4 deletions lib/operations/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { template, escapeValue, formatParams } from "../utils";
const { template, escapeValue, formatParams } = require("../utils");

export function dropFunction(typeShorthands) {
function dropFunction(typeShorthands) {
return (functionName, functionParams = [], { ifExists, cascade } = {}) => {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
Expand All @@ -9,7 +9,7 @@ export function dropFunction(typeShorthands) {
};
}

export function createFunction(typeShorthands) {
function createFunction(typeShorthands) {
const _create = (
functionName,
functionParams = [],
Expand Down Expand Up @@ -60,7 +60,7 @@ export function createFunction(typeShorthands) {
return _create;
}

export function renameFunction(typeShorthands) {
function renameFunction(typeShorthands) {
const _rename = (oldFunctionName, functionParams = [], newFunctionName) => {
const paramsStr = formatParams(functionParams, typeShorthands);
return template`ALTER FUNCTION "${oldFunctionName}"${paramsStr} RENAME TO "${newFunctionName}";`;
Expand All @@ -71,3 +71,9 @@ export function renameFunction(typeShorthands) {

return _rename;
}

module.exports = {
createFunction,
dropFunction,
renameFunction
};
13 changes: 9 additions & 4 deletions lib/operations/indexes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import { template } from "../utils";
const _ = require("lodash");
const { template } = require("../utils");

function generateIndexName(table, columns, options) {
return options.name
Expand Down Expand Up @@ -27,7 +27,7 @@ function generateColumnsString(columns) {
: generateColumnString(columns);
}

export function createIndex(tableName, columns, options = {}) {
function createIndex(tableName, columns, options = {}) {
/*
columns - the column, columns, or expression to create the index on
Expand All @@ -50,7 +50,7 @@ export function createIndex(tableName, columns, options = {}) {
return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${tableName}"${method} (${columnsString}${opclass})${where};`;
}

export function dropIndex(tableName, columns, options = {}) {
function dropIndex(tableName, columns, options = {}) {
const { concurrently, ifExists, cascade } = options;
const concurrentlyStr = concurrently ? " CONCURRENTLY" : "";
const ifExistsStr = ifExists ? " IF EXISTS" : "";
Expand All @@ -62,3 +62,8 @@ export function dropIndex(tableName, columns, options = {}) {

// setup reverse functions
createIndex.reverse = dropIndex;

module.exports = {
createIndex,
dropIndex
};
Loading

0 comments on commit 4dda6fc

Please sign in to comment.