Socket
Socket
Sign inDemoInstall

sql-migrations

Package Overview
Dependencies
48
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.3 to 1.0.4

commands/create-migration-command.js

14

adapters/pg.js
var pg = require('pg');
module.exports = function (config) {
module.exports = function (config, logger) {
var pool = new pg.Pool({

@@ -36,5 +36,5 @@ host: config.host,

return exec(sql).then(function (result) {
console.log('Applying ' + migration);
console.log(result)
console.log('===============================================');
logger.log('Applying ' + migration);
logger.log(result)
logger.log('===============================================');
var values = [migration.match(/^(\d)+/)[0]];

@@ -46,5 +46,5 @@ return exec('insert into __migrations__ (id) values ($1)', values);

return exec(sql).then(function (result) {
console.log('Reverting ' + migration);
console.log(result)
console.log('===============================================');
logger.log('Reverting ' + migration);
logger.log(result)
logger.log('===============================================');
var values = [migration.match(/^(\d)+/)[0]];

@@ -51,0 +51,0 @@ return exec('delete from __migrations__ where id = $1', values);

var MigrationProvider = require('./migration-provider');
var PgAdapter = require('./adapters/pg');
var migrateCommand = require('./cmds/migrate')
var rollbackCommand = require('./cmds/rollback');
var createMigrationCommand = require('./commands/create-migration-command');
var runMigrationsCommand = require('./commands/run-migrations-command')
var rollbackMigrationCommand = require('./commands/rollback-migration-command');
var LOGGER = console;
function migrate(config) {
var migrationProvider = MigrationProvider(config.migrationsDir);
var adapter = PgAdapter(config);
return migrateCommand(migrationProvider, adapter).then(function () {
var migrationProvider = MigrationProvider(config);
var adapter = PgAdapter(config, LOGGER);
return runMigrationsCommand(migrationProvider, adapter, LOGGER).then(function () {
return adapter.dispose();

@@ -20,5 +23,5 @@ }, function (error) {

function rollback(config) {
var migrationProvider = MigrationProvider(config.migrationsDir);
var adapter = PgAdapter(config);
return rollbackCommand(migrationProvider, adapter).then(function () {
var migrationProvider = MigrationProvider(config);
var adapter = PgAdapter(config, LOGGER);
return rollbackMigrationCommand(migrationProvider, adapter, LOGGER).then(function () {
return adapter.dispose();

@@ -34,2 +37,5 @@ }, function (error) {

module.exports = {
setLogger: function (logger) {
LOGGER = logger;
},
migrate: migrate,

@@ -42,3 +48,3 @@ rollback: rollback,

case 'create':
require('./cmds/create_migration.js')(args[1]);
createMigrationCommand(config, LOGGER, args[1]);
break;

@@ -52,7 +58,7 @@ case 'migrate':

default:
console.log('exit');
LOGGER.log('exit');
}
function onCliSuccess() {
console.log('done');
LOGGER.log('done');
process.exit();

@@ -62,3 +68,3 @@ }

function onCliError(error) {
console.error('ERROR:', error);
LOGGER.error('ERROR:', error);
process.exit(1);

@@ -65,0 +71,0 @@ }

var fs = require('fs');
var path = require('path');
module.exports = function (migrationsDir) {
module.exports = function (config) {
return {
getMigrationsList: function () {
return fs.readdirSync(migrationsDir);
return fs.readdirSync(config.migrationsDir);
},

@@ -16,5 +16,13 @@ /**

getSql: function (migration) {
return fs.readFileSync(path.join(migrationsDir, migration)).toString();
var sql = fs.readFileSync(path.join(config.migrationsDir, migration)).toString();
Object.keys(config.parameters || {}).forEach(function (key) {
sql = sql.replace(new RegExp(escapeRegExp(key), "g"), config.parameters[key]);
});
return sql;
}
};
};
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
{
"name": "sql-migrations",
"version": "1.0.3",
"version": "1.0.4",
"description": "raw SQL migrations library for Node.js",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -14,8 +14,3 @@ node-sql-migrations

require('sql-migrations').run({
migrationsDir: path.resolve(__dirname, 'migrations'),
user: 'dabramov',
host: 'localhost',
password: 'password',
db: 'sql_migrations',
port: 5432
// configuration here. See the Configuration section
});

@@ -51,8 +46,3 @@ ```

require('sql-migrations').migrate({
migrationsDir: path.resolve(__dirname, 'migrations'),
user: 'dabramov',
host: 'localhost',
password: 'password',
db: 'sql_migrations',
port: 5432
// configuration here. See the Configuration section
});

@@ -66,8 +56,3 @@ ```

require('sql-migrations').rollback({
migrationsDir: path.resolve(__dirname, 'migrations'),
user: 'dabramov',
host: 'localhost',
password: 'password',
db: 'sql_migrations',
port: 5432
// configuration here. See the Configuration section
});

@@ -77,4 +62,28 @@ ```

### Configuration
Configuration should be specified as below:
```js
var configuration = {
migrationsDir: path.resolve(__dirname, 'migrations'), // This is the directory that should contain your SQL migrations.
host: 'localhost', // Database host
port: 5432, // Database port
db: 'sql_migrations', // Database name
user: 'dabramov', // Database username
password: 'password', // Database password
// Parameters are optional. If you provide them then any occurrences of the parameter (i.e. FOO) in the SQL scripts will be replaced by the value (i.e. bar).
parameters: {
"FOO": "bar"
}
};
```
You can also swap out the default logger (the `console` object) for another one that supports the log and error methods. You should do this before running any other commands:
```js
require('sql-migrations').setLogger({
log: function() {},
error: function() {}
});
```
### Migration files
write raw sql in your migrations
Write raw sql in your migrations. You can also include placeholders which will be substituted.
example

@@ -81,0 +90,0 @@ ```sql

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc