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

Updated to be compatible with node-postgres #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

root = true


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2
Expand Down
21 changes: 0 additions & 21 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.1.0 / 2017-07-26
==================

* Rewrote most of the application to use promises
* Use default postgres database
* Use username from configuration instead of falling back to process.env.USER

1.0.1 / 2014-11-26
==================

Expand Down
26 changes: 26 additions & 0 deletions create_database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node
"use strict";

const pg = require("pg-promise")();
const create = require("./lib/create");
const config = require("./lib/load-configuration")(process.cwd());
const DEFAULT_DATABASE = process.env.POSTGRES_DB || "postgres";
const db = pg(Object.assign({}, config, { database: DEFAULT_DATABASE }));
const DEBUG = process.env.DEBUG || false;

create(config.database, db)
.then(() => {
console.log(`Created ${config.database}`);
process.exit(0);
})
.catch(err => {
if (DEBUG) {
console.log(`Failed to create ${config.database}`);
console.error(err);
} else {
console.error(
`Failed to create ${config.database} (Run with DEBUG=true to see stack trace)`
);
}
process.exit(1);
});
38 changes: 38 additions & 0 deletions drop_database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node
"use strict";

const pg = require("pg-promise")();
const drop = require("./lib/drop");
const config = require("./lib/load-configuration")(process.cwd());
const DEFAULT_DATABASE = process.env.POSTGRES_DB || "postgres";
const db = pg(Object.assign({}, config, { database: DEFAULT_DATABASE }));
const DEBUG = process.env.DEBUG || false;

if (
(process.env.NODE_ENV || "").indexOf("production") !== -1 &&
process.env.FORCE !== "true"
) {
console.warn("You were about to drop a production database");
console.warn("To drop a production database you must pass FORCE=true");
console.warn(
"For example: NODE_ENV=production FORCE=true ./node_modules/.bin/drop_database"
);
return process.exit(1);
}

drop(config.database, db)
.then(() => {
console.log(`Dropped ${config.database}`);
process.exit(0);
})
.catch(err => {
if (DEBUG) {
console.log(`Failed to drop ${config.database}`);
console.error(err);
} else {
console.error(
`Failed to drop ${config.database} (Run with DEBUG=true to see stack trace)`
);
}
process.exit(1);
});
3 changes: 3 additions & 0 deletions lib/configuration-mapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = sequelizeConfig => {
return Object.assign({}, sequelizeConfig, { user: sequelizeConfig.username });
};
14 changes: 14 additions & 0 deletions lib/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const checkForExistingDatabase = (databaseName, client) =>
client.none("SELECT 1 from pg_database WHERE datname = $1", databaseName);

// Postgres doesn't support parameterized queries for `CREATE DATABASE ...`
const createDatabase = (databaseName, client) =>
client.none(`CREATE DATABASE ${databaseName}`);

module.exports = (databaseName, client) => {
return checkForExistingDatabase(databaseName, client).then(() =>
createDatabase(databaseName, client)
);
};
50 changes: 0 additions & 50 deletions lib/create_database.js

This file was deleted.

14 changes: 14 additions & 0 deletions lib/drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const checkForExistingDatabase = (databaseName, client) =>
client.one("SELECT 1 from pg_database WHERE datname = $1", databaseName);

// Postgres doesn't support parameterized queries for `DROP DATABASE ...`
const dropDatabase = (databaseName, client) =>
client.none(`DROP DATABASE ${databaseName}`);

module.exports = (databaseName, client) => {
return checkForExistingDatabase(databaseName, client).then(() =>
dropDatabase(databaseName, client)
);
};
58 changes: 0 additions & 58 deletions lib/drop_database.js

This file was deleted.

33 changes: 33 additions & 0 deletions lib/load-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const path = require("path");
const toPgConfiguration = require("./configuration-mapper");

const ENVIRONMENT = process.env.NODE_ENV || "development";

module.exports = rootPath => {
let configPath;
try {
configPath = require(path.join(rootPath, ".sequelizerc")).config;
if (!configPath) {
throw new Error();
}
} catch (err) {
configPath = path.join(rootPath, "config/config.json");
}

let config;
try {
config = require(configPath);
console.log("Using configuration in", configPath);
} catch (err) {
console.error("Configuration not found in", configPath);
throw err;
}

config = config[ENVIRONMENT];
if (!config) {
console.error("Environment", ENVIRONMENT, "not found in file", configPath);
throw new Error("environment not found");
} else {
return toPgConfiguration(config);
}
};
37 changes: 0 additions & 37 deletions lib/load_configuration.js

This file was deleted.

26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "sequelize-migration-pg-extras",
"description": "Add db:create and db:drop scripts to your project",
"license": "MIT",
"keywords": ["sequelize", "postgres"],
"keywords": [
"sequelize",
"postgres"
],
"version": "1.0.1",
"repository": {
"type": "git",
Expand All @@ -17,13 +20,26 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "echo \"Error: no automated tests\" && exit 1"
"test": "echo \"Error: no automated tests\" && exit 1",
"precommit": "lint-staged",
"format": "prettier --write lib/**/*.js *.js"
},
"dependencies": {
"pg": "^4.0.0"
"pg-promise": "^6.3.6"
},
"bin": {
"create_database": "lib/create_database.js",
"drop_database": "lib/drop_database.js"
"create_database": "create_database.js",
"drop_database": "drop_database.js"
},
"devDependencies": {
"husky": "^0.14.3",
"lint-staged": "^4.0.2",
"prettier": "^1.5.3"
},
"lint-staged": {
"*.js": [
"prettier --write",
"git add"
]
}
}
23 changes: 0 additions & 23 deletions test/default/config/config.json

This file was deleted.

Loading