Skip to content

Commit

Permalink
Merge pull request #4 from Come2Daddy/fix/ssl-errors
Browse files Browse the repository at this point in the history
fix: avoid ssl errors to drop connection
  • Loading branch information
Come2Daddy authored Nov 20, 2020
2 parents 32d1536 + 8029b1b commit 0c786af
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Either use environment variables
```bash
UPMIG_PATH=./migrations
UPMIG_TABLE=pg_upmig
UPMIG_REJECT=false
```
or use `.upmigrc.js` to set global options:
```javascript
Expand All @@ -47,6 +48,7 @@ try {
module.exports = {
migrations: "./migrations", // Where to store migrations files
table: "pg_upmig", // Table name where migrations history is stored
reject: false
};
```
## Migrations folder tree view
Expand Down Expand Up @@ -86,6 +88,7 @@ Options:
-h, --help display help for command
-m, --migrations <path> specify migrations path (default: "./migrations")
-p, --pgtable <table> specify migration table name (default: "pg_upmig")
-r, --reject dont ignore unauthorized ssl rejection

Commands:
up [options] perform all pending migrations
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-upmig",
"version": "0.0.26",
"version": "0.0.27",
"description": "Postgresql migration tool",
"keywords": [
"database",
Expand Down
33 changes: 29 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const path = require("path");

let migrationsPath = process.env.UPMIG_PATH||"./migrations";
let pgTable = process.env.UPMIG_TABLE||"pg_upmig";
let reject = Boolean(process.env.UPMIG_REJECT)||false;

try {
const dotConfig = require(path.join(process.cwd(), ".upmigrc.js"));
migrationsPath = dotConfig.migrations?dotConfig.migrations:migrationsPath;
pgTable = dotConfig.table?dotConfig.table:pgTable;
reject = dotConfig.reject?dotConfig.reject:reject;
} catch (error) {}

function steps (s) {
Expand Down Expand Up @@ -43,6 +45,9 @@ function setOpt (namespace) {
break;
case "pgtable":
pgTable = option ? migTable(option):pgTable;
break;
case "reject":
reject = option;
}
}
}
Expand All @@ -58,15 +63,23 @@ exports._env = () => {

program.on("option:migrations", setOpt("migrations"));
program.on("option:pgtable", setOpt("pgtable"));
program.on("option:reject", setOpt("reject"));

program.version(pkg.version)
.arguments("<cmd> [opt]")
.usage("<command> [options]")
.option("-m, --migrations <path>", "specify migrations path", "./migrations")
.option("-p, --pgtable <table>", "specify migration table name", "pg-upmig", migTable);
.option("-p, --pgtable <table>", "specify migration table name", "pg-upmig", migTable)
.option("-r, --reject", "dont ignore unauthorized ssl rejection");

async function up (cmd) {
const mig = new migration();
const mig = new migration({
connection: {
ssl: {
rejectUnauthorized: reject
}
}
});
await mig.init({
table: pgTable,
migrations: migrationsPath,
Expand Down Expand Up @@ -97,7 +110,13 @@ program
});

async function create (cmd, name){
const mig = new migration();
const mig = new migration({
connection: {
ssl: {
rejectUnauthorized: reject
}
}
});
await mig.init({migrations: migrationsPath, table: pgTable});
const file = await mig.create(name?name.join("-"):"", cmd.nosql);
mig.release();
Expand All @@ -118,7 +137,13 @@ program
});

async function pending (cmd) {
const mig = new migration();
const mig = new migration({
connection: {
ssl: {
rejectUnauthorized: reject
}
}
});
await mig.init({migrations: migrationsPath, table: pgTable, history: cmd.history});
const list = await mig.pending();
mig.release();
Expand Down
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,21 @@ class migration {
// Sets default database client
if (!this.client) {
if ((params||{}).connection) {
if (!params.connection.hasOwnProperty("ssl")) {
params.connection.ssl= { rejectUnauthorized: process.env.UPMIG_REJECT||false };
} else if (!params.connection.ssl.hasOwnProperty("rejectUnauthorized")) {
params.connection.ssl["rejectUnauthorized"] = process.env.UPMIG_REJECT||false;
}
// Uses connection params
this.client = new Client(params.connection);
} else {
// Try to connect with environment variables
// At this point, if nothing is provided we let PG client rise errors
this.client = new Client();
this.client = new Client({
ssl{
rejectUnauthorized: process.env.UPMIG_REJECT||false
}
});
}
}
}
Expand Down
17 changes: 3 additions & 14 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ afterAll(async () => {
await teardown();
});

describe("Covering privates and specific code - fallback to default .env file", () => {
describe("Covering privates and specific code with default env vars", () => {
beforeAll(async () => {
mig = new lib({
options: {
migrations: fixtures.migrations,
table: fixtures.pgTable
},
envFile: "x/.env"
});
mig = new lib();
await mig.init();
});

Expand Down Expand Up @@ -72,12 +66,7 @@ describe("Covering privates and specific code - fallback to default .env file",

describe("Migration using environment variables", () => {
beforeAll(async () => {
mig = new lib({
options: {
migrations: fixtures.migrations,
table: fixtures.pgTable,
}
});
mig = new lib();
await mig.init();
});

Expand Down

0 comments on commit 0c786af

Please sign in to comment.