Socket
Socket
Sign inDemoInstall

node-mini-migrations

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.1 to 4.0.0

.vscode/launch.json

5

CHANGELOG.md

@@ -7,2 +7,7 @@ # Changelog

## [4.0.0] - 2020-04-26
### Removed
- No more bin
- No more promises
## [2.1.1] - 2019-11-08

@@ -9,0 +14,0 @@ ### Changed

18

package.json
{
"name": "node-mini-migrations",
"version": "2.1.1",
"version": "4.0.0",
"description": "A very small, lightweight and flexible migrations library unconcerned with what database you use",
"main": "src/index.js",
"bin": {
"migrator": "./bin/migrator.js"
},
"scripts": {
"test": "tape test/**/*.js test/*.js",
"example": "./bin/migrator.js -d example up"
"test": "node test"
},
"author": "Mark Wylde",
"license": "MIT",
"dependencies": {
"commander": "^4.0.0"
},
"dependencies": {},
"devDependencies": {
"sqlite": "^3.0.3",
"tape": "^4.11.0"
"righto-series": "^1.0.1",
"righto-tape": "^1.0.4",
"sqlite-fp": "^2.0.0",
"tape": "^4.13.2"
}
}

@@ -16,46 +16,39 @@ # Mini Migrations for NodeJS

## Example Usage
There are two examples, one using sqlite and another using a pretend custom file system database.
1. [SQLite Driver](example/driver.js)
2. [Filesystem Driver](exampleFilesystem/driver.js)
### Preview SQLite driver
### driver
```javascript
const sqlite = require('sqlite')
const sqlite = require('sqlite-fp');
const righto = require('righto');
module.exports = function () {
let db
function migrator (db) {
return {
init: async () => {
db = await sqlite.open('./test.sqlite')
await db.run('CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);')
init: (direction, callback) => {
sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);', callback);
},
finish: async () => {
await db.close()
getMigrationState: (id, callback) => {
sqlite.get(db, 'SELECT file FROM _migrations WHERE file = ?', [id], (error, result) => {
callback(error, (result && result.length > 0));
});
},
getMigrationState: async (id) => {
return db.get('SELECT file FROM _migrations WHERE file = ?', [id])
setMigrationUp: (id, callback) => {
sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id], callback);
},
setMigrationUp: async (id) => {
return db.run('INSERT INTO _migrations (file) VALUES (?)', [id])
setMigrationDown: (id, callback) => {
sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id], callback);
},
setMigrationDown: async (id) => {
return db.run('DELETE FROM _migrations WHERE file = ?', [id])
},
handler: (fn, callback) => fn(db, callback)
};
};
getPassedFunctions: async () => {
return db
}
}
}
const db = righto(sqlite.connect, './test.sqlite');
const driver = righto.sync(migrator, db);
const migrations = getMigrationsFromDirectory('./test/migrations');
const migrated = righto(up, driver, migrations);
migrated(callback)
```
### Preview SQLite migration
### migration
```javascript

@@ -73,12 +66,3 @@ module.exports = {

### Usage
You run `migrator up` to bring up any migrations or `migrator down` to bring them down.
#### Or inside node app
```javascript
const {up, prepareRun} = require('node-mini-migrations')
up(prepareRun('./migrations'))
```
## License
This project is licensed under the terms of the MIT license.

@@ -1,30 +0,44 @@

module.exports = async ({ driver, migrations, logger }) => {
logger = logger || (() => null)
const righto = require('righto');
const rightoSeries = require('righto-series');
const driverInstance = await driver()
if (driverInstance.init) {
await driverInstance.init('down')
function down (driver, migrations, logger, steps, callback) {
if (arguments.length === 4) {
callback = steps;
steps = logger;
logger = () => null;
}
migrations.reverse()
rightoSeries(function * () {
if (driver.init) {
yield righto(driver.init, 'down');
}
for (const migration of migrations) {
const active = await driverInstance.getMigrationState(migration.id)
if (active) {
logger(`Tearing down ${migration.id}`)
try {
const passedFunctions = await driverInstance.getPassedFunctions()
await migration.down(passedFunctions)
await driverInstance.setMigrationDown(migration.id)
} catch (error) {
logger(`Error tearing down ${migration.id}`, error)
const reversedMigrations = [...migrations].reverse();
let currentSteps = 0;
for (const migration of reversedMigrations) {
currentSteps = currentSteps + 1;
if (currentSteps > steps) {
break;
}
} else {
logger(`Migration ${migration.id} is not active`)
const active = yield righto(driver.getMigrationState, migration.id);
if (!active) {
logger(`Migration ${migration.id} skipped (not active)`);
} else {
logger(`Bring down ${migration.id}`);
yield righto(driver.handler, migration.down);
yield righto(driver.setMigrationDown, migration.id);
}
}
}
if (driverInstance.finish) {
await driverInstance.finish('down')
}
if (driver.finish) {
yield righto(driver.finish, 'down');
}
}, callback);
}
module.exports = down;
module.exports = {
up: require('./up'),
down: require('./down'),
prepareRun: require('./prepareRun')
}
getMigrationsFromDirectory: require('./getMigrationsFromDirectory')
};

@@ -1,29 +0,34 @@

module.exports = async ({ driver, migrations, logger }) => {
logger = logger || (() => null)
const righto = require('righto');
const rightoSeries = require('righto-series');
const driverInstance = driver()
if (driverInstance.init) {
await driverInstance.init('up')
function up (driver, migrations, logger, callback) {
if (arguments.length === 3) {
callback = logger;
logger = () => null;
}
for (const migration of migrations) {
const active = await driverInstance.getMigrationState(migration.id)
if (active) {
logger(`Migration ${migration.id} already active`)
} else {
logger(`Bring up ${migration.id}`)
try {
const passedFunctions = await driverInstance.getPassedFunctions()
await migration.up(passedFunctions)
await driverInstance.setMigrationUp(migration.id)
} catch (error) {
logger(`Error bringing up ${migration.id}`, error)
throw error
rightoSeries(function * () {
if (driver.init) {
yield righto(driver.init, 'up');
}
for (const migration of migrations) {
const active = yield righto(driver.getMigrationState, migration.id);
if (active) {
logger(`Migration ${migration.id} skipped (already active)`);
} else {
logger(`Bring up ${migration.id}`);
yield righto(driver.handler, migration.up);
yield righto(driver.setMigrationUp, migration.id);
}
}
}
if (driverInstance.finish) {
await driverInstance.finish('up')
}
if (driver.finish) {
yield righto(driver.finish, 'up');
}
}, callback);
}
module.exports = up;

@@ -0,9 +1,11 @@

const sqlite = require('sqlite-fp');
module.exports = {
up: db => {
return db.exec('CREATE TABLE test_table (test TEXT)')
up: (db, callback) => {
return sqlite.run(db, 'CREATE TABLE test_table (test TEXT)', callback);
},
down: db => {
return db.exec('DROP TABLE test_table')
down: (db, callback) => {
return sqlite.run(db, 'DROP TABLE test_table', callback);
}
}
};

@@ -0,9 +1,11 @@

const sqlite = require('sqlite-fp');
module.exports = {
up: db => {
return db.run('INSERT INTO test_table (test) VALUES (?)', ['hello'])
up: (db, callback) => {
return sqlite.run(db, 'INSERT INTO test_table (test) VALUES (?)', ['hello'], callback);
},
down: db => {
return db.run('DELETE FROM test_table WHERE test = ?', ['hello'])
down: (db, callback) => {
return sqlite.run(db, 'DELETE FROM test_table WHERE test = ?', ['hello'], callback);
}
}
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc