pg-migration
Advanced tools
+13
| module.exports = { | ||
| root: true, | ||
| extends: "standard", | ||
| env: { | ||
| jest: true, | ||
| node: true, | ||
| es6: true, | ||
| }, | ||
| rules: { | ||
| // Custom overrides | ||
| 'space-before-function-paren': 0 | ||
| } | ||
| }; |
+21
| MIT License | ||
| Copyright (c) 2017 Rogier Slag | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
+57
-50
@@ -16,3 +16,3 @@ 'use strict'; | ||
| function log(level, message) { | ||
| function defaultLog(level, message) { | ||
| var obj = { | ||
@@ -23,23 +23,24 @@ datetime: Date.now(), | ||
| }; | ||
| console.log(JSON.stringify(obj)); //eslint-disable-line no-console | ||
| console.log(JSON.stringify(obj)); | ||
| } | ||
| var changesetExists = "SELECT id FROM dbchangelog WHERE id=$1"; | ||
| var changesetExists = 'SELECT id FROM dbchangelog WHERE id=$1'; | ||
| function migrateAndStart(databaseClient, migrationsDir, cb) { | ||
| log('info', 'Checking for migrations to perform'); | ||
| opts.log('info', 'Checking for migrations to perform'); | ||
| // Check whether we need to create the migration table itself | ||
| databaseClient.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'dbchangelog'", [], function (err, res) { | ||
| databaseClient.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'dbchangelog'").then(function (res) { | ||
| if (res.rowCount === 1) { | ||
| migrate(databaseClient, migrationsDir, cb); | ||
| } else { | ||
| log('info', 'Creating the migration table'); | ||
| databaseClient.query("CREATE TABLE dbchangelog(id text, datetime timestamp with time zone);CREATE UNIQUE index changeset on dbchangelog(id);", function (err, res) { | ||
| if (err) { | ||
| return log('error', 'Could not create the migration table'); | ||
| } | ||
| log('info', 'Created the migration table'); | ||
| opts.log('info', 'Creating the migration table'); | ||
| return databaseClient.query('CREATE TABLE dbchangelog(id text, datetime timestamp with time zone);CREATE UNIQUE index changeset on dbchangelog(id);').then(function (res) { | ||
| opts.log('info', 'Created the migration table'); | ||
| migrate(databaseClient, migrationsDir, cb); | ||
| }).catch(function (err) { | ||
| return opts.log('error', 'Could not create the migration table: ' + err.message); | ||
| }); | ||
| } | ||
| }).catch(function (err) { | ||
| return opts.log('error', err.message); | ||
| }); | ||
@@ -52,4 +53,4 @@ } | ||
| // In case of an error while fetching the migrations, error and abort | ||
| log('error', 'Could not find changesets: ' + err); | ||
| return; | ||
| opts.log('warning', 'Could not find changesets: ' + err.message); | ||
| return cb(); | ||
| } | ||
@@ -64,40 +65,40 @@ | ||
| var changesetCode = changeset.replace('.sql', ''); | ||
| log('info', 'Checking whether to apply changeset ' + changesetCode); | ||
| databaseClient.query(changesetExists, [changesetCode], function (err, res) { | ||
| if (err) { | ||
| // An error occurred while checking if this changeset exists | ||
| log('error', err); | ||
| return reject(); | ||
| } | ||
| opts.log('debug', 'Checking whether to apply changeset ' + changesetCode); | ||
| return databaseClient.query(changesetExists, [changesetCode]).then(function (res) { | ||
| if (res.rowCount === 1) { | ||
| // This changeset is already applied | ||
| log('info', 'No need to apply changeset ' + changesetCode); | ||
| opts.log('debug', 'No need to apply changeset ' + changesetCode); | ||
| return resolve(); | ||
| } | ||
| log('info', 'Applying changeset ' + changesetCode); | ||
| opts.log('info', 'Applying changeset ' + changesetCode); | ||
| // Read the file content | ||
| var changesetContent = _fsExtra2.default.readFileSync(migrationsDir + '/' + changeset, 'UTF-8'); | ||
| databaseClient.query("BEGIN", function (err) { | ||
| databaseClient.query(changesetContent, function (err) { | ||
| // Apply the changeset | ||
| if (err) { | ||
| // Couldn't apply the changeset, probably an error in the migration. Reject the promise | ||
| log('error', 'Error applying the changeset ' + changesetCode + ': ' + err); | ||
| return reject(); | ||
| } | ||
| databaseClient.query("INSERT INTO dbchangelog (id, datetime) VALUES ($1, NOW())", [changesetCode], function (err) { | ||
| // Save the value of the changeset to the database | ||
| if (err) { | ||
| return reject(); | ||
| } | ||
| databaseClient.query("COMMIT", function (err) { | ||
| if (err) { | ||
| return reject(); | ||
| } | ||
| log('info', 'Finished applying changeset ' + changesetCode); | ||
| return resolve(); | ||
| }); | ||
| }); | ||
| }); | ||
| return databaseClient.query('BEGIN') | ||
| // Apply the changeset | ||
| .then(function (res) { | ||
| return databaseClient.query(changesetContent); | ||
| }) | ||
| // Save the value of the changeset to the database | ||
| .then(function (res) { | ||
| return databaseClient.query('INSERT INTO dbchangelog (id, datetime) VALUES ($1, NOW())', [changesetCode]); | ||
| }) | ||
| // Commit the changeset | ||
| .then(function (res) { | ||
| return databaseClient.query('COMMIT'); | ||
| }) | ||
| // Resolve | ||
| .then(function (res) { | ||
| opts.log('debug', 'Finished applying changeset ' + changesetCode); | ||
| resolve(); | ||
| }) | ||
| // Something bad happened | ||
| .catch(function (err) { | ||
| // Couldn't apply the changeset, probably an error in the migration. Reject the promise | ||
| opts.log('error', 'Error applying the changeset ' + changesetCode + ': ' + err); | ||
| reject(err); | ||
| }); | ||
| }).catch(function (err) { | ||
| opts.log('error', err); | ||
| reject(err); | ||
| }); | ||
@@ -108,10 +109,16 @@ }); | ||
| promise.catch(function (err) { | ||
| console.log(err); | ||
| log('error', 'One or more migrations failed to apply'); | ||
| process.exit(); | ||
| }).then(cb); | ||
| promise.then(cb).catch(function (err) { | ||
| opts.log('error', 'One or more migrations failed to apply'); | ||
| cb(err); | ||
| }); | ||
| }); | ||
| } | ||
| exports.default = migrateAndStart; | ||
| var opts = { | ||
| log: defaultLog | ||
| }; | ||
| exports.default = function (options) { | ||
| Object.assign(opts, options); | ||
| return migrateAndStart; | ||
| }; |
+8
-2
| { | ||
| "name": "pg-migration", | ||
| "version": "2.0.2", | ||
| "version": "3.0.0", | ||
| "description": "Apply changesets of your data structure for Postgresql easily from your node application", | ||
@@ -31,3 +31,9 @@ "main": "lib/index.js", | ||
| "babel-cli": "^6.3.17", | ||
| "babel-preset-es2015": "^6.3.13" | ||
| "babel-preset-es2015": "^6.3.13", | ||
| "eslint": "^4.19.1", | ||
| "eslint-config-standard": "^11.0.0", | ||
| "eslint-plugin-import": "^2.10.0", | ||
| "eslint-plugin-node": "^6.0.1", | ||
| "eslint-plugin-promise": "^3.7.0", | ||
| "eslint-plugin-standard": "^3.0.1" | ||
| }, | ||
@@ -34,0 +40,0 @@ "dependencies": { |
+2
-1
@@ -15,3 +15,4 @@ # pg-migration | ||
| 1. Import the library `import migrateAndStart from 'pg-migration';` | ||
| 1. Import the library `import migration from 'pg-migration';` | ||
| 1. Configure the library: `const migrateAndStart = migration({ log: (level, message) => { your logging code here } })` | ||
| 1. Create a valid database client connection | ||
@@ -18,0 +19,0 @@ 1. Create the migration, with that client, and a callback to the server start (e.g. `migrateAndStart(db, './migrations', startServer);`) |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
27067
6.68%14
16.67%116
17.17%29
3.57%8
300%