node-mini-migrations
Advanced tools
Comparing version 4.0.4 to 5.0.0
54
down.js
@@ -1,44 +0,36 @@ | ||
const righto = require('righto'); | ||
const rightoSeries = require('righto-series'); | ||
async function down (driver, migrations, logger, steps) { | ||
logger = logger || (() => null); | ||
steps = steps || 1; | ||
function down (driver, migrations, logger, steps, callback) { | ||
if (arguments.length === 4) { | ||
callback = steps; | ||
steps = logger; | ||
logger = () => null; | ||
if (driver.init) { | ||
await driver.init('down'); | ||
} | ||
rightoSeries(function * () { | ||
if (driver.init) { | ||
yield righto(driver.init, 'down'); | ||
} | ||
const reversedMigrations = [...migrations].reverse(); | ||
const reversedMigrations = [...migrations].reverse(); | ||
let currentSteps = 0; | ||
for (const migration of reversedMigrations) { | ||
currentSteps = currentSteps + 1; | ||
let currentSteps = 0; | ||
for (const migration of reversedMigrations) { | ||
currentSteps = currentSteps + 1; | ||
if (currentSteps > steps) { | ||
break; | ||
} | ||
if (currentSteps > steps) { | ||
break; | ||
} | ||
const active = await driver.getMigrationState(migration.id); | ||
const active = yield righto(driver.getMigrationState, migration.id); | ||
if (!active) { | ||
logger(`Migration ${migration.id} skipped (not active)`); | ||
} else { | ||
logger(`Bring down ${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); | ||
} | ||
await driver.handler(migration.down); | ||
await driver.setMigrationDown(migration.id); | ||
} | ||
} | ||
if (driver.finish) { | ||
yield righto(driver.finish, 'down'); | ||
} | ||
}, callback); | ||
if (driver.finish) { | ||
await driver.finish('down'); | ||
} | ||
} | ||
module.exports = down; |
{ | ||
"name": "node-mini-migrations", | ||
"version": "4.0.4", | ||
"version": "5.0.0", | ||
"description": "A very small, lightweight and flexible migrations library unconcerned with what database you use", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,33 +18,33 @@ # Mini Migrations for NodeJS | ||
```javascript | ||
const sqlite = require('sqlite-fp'); | ||
const righto = require('righto'); | ||
const sqlite = require('sqlite-fp/promises'); | ||
const up = require('node-mini-migrations/up'); | ||
const getMigrationsFromDirectory = require('node-mini-migrations/getMigrationsFromDirectory'); | ||
function migrator (db) { | ||
return { | ||
init: (direction, callback) => { | ||
sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);', callback); | ||
init: (direction) => { | ||
return sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);'); | ||
}, | ||
getMigrationState: (id, callback) => { | ||
sqlite.getOne(db, 'SELECT file FROM _migrations WHERE file = ?', [id], callback); | ||
getMigrationState: (id) => { | ||
return sqlite.getOne(db, 'SELECT file FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
setMigrationUp: (id, callback) => { | ||
sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id], callback); | ||
setMigrationUp: (id) => { | ||
return sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id]); | ||
}, | ||
setMigrationDown: (id, callback) => { | ||
sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id], callback); | ||
setMigrationDown: (id) => { | ||
return sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
handler: (fn, callback) => fn(db, callback) | ||
handler: (fn) => fn(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) | ||
const db = await sqlite.connect('./test.sqlite'); | ||
await up( | ||
migrator(db), | ||
getMigrationsFromDirectory('./test/migrations') | ||
); | ||
``` | ||
@@ -55,7 +55,7 @@ | ||
module.exports = { | ||
up: db => { | ||
up: (db) => { | ||
return db.exec('CREATE TABLE test_table (test TEXT)') | ||
}, | ||
down: db => { | ||
down: (db) => { | ||
return db.exec('DROP TABLE test_table') | ||
@@ -62,0 +62,0 @@ } |
const fs = require('fs'); | ||
const test = require('righto-tape'); | ||
const sqlite = require('sqlite-fp'); | ||
const righto = require('righto'); | ||
const sqlite = require('sqlite-fp/promises'); | ||
const migrator = require('./migrations'); | ||
@@ -15,3 +15,3 @@ | ||
test('migrate examples up', function * (t) { | ||
test('migrate examples up', async function (t) { | ||
t.plan(2); | ||
@@ -21,15 +21,15 @@ | ||
const db = yield righto(sqlite.connect, './test.sqlite'); | ||
const driver = yield righto.sync(migrator, db); | ||
const db = await sqlite.connect('./test.sqlite'); | ||
const driver = migrator(db); | ||
const migrations = getMigrationsFromDirectory('./test/migrations'); | ||
yield righto(up, driver, migrations); | ||
await up(driver, migrations, null); | ||
t.ok(fs.existsSync('./test.sqlite')); | ||
const result = yield righto(sqlite.getOne, db, 'SELECT * FROM test_table'); | ||
const result = await sqlite.getOne(db, 'SELECT * FROM test_table'); | ||
t.equal(result.test, 'hello'); | ||
yield righto(sqlite.close, db); | ||
await db.close(); | ||
}); | ||
test('migrate examples up, down', function * (t) { | ||
test('migrate examples up, down', async function (t) { | ||
t.plan(2); | ||
@@ -39,16 +39,16 @@ | ||
const db = yield righto(sqlite.connect, './test.sqlite'); | ||
const driver = yield righto.sync(migrator, db); | ||
const db = await sqlite.connect('./test.sqlite'); | ||
const driver = migrator(db); | ||
const migrations = getMigrationsFromDirectory('./test/migrations'); | ||
yield righto(up, driver, migrations); | ||
yield righto(down, driver, migrations, 1); | ||
await up(driver, migrations, null); | ||
await down(driver, migrations, null, 1); | ||
t.ok(fs.existsSync('./test.sqlite')); | ||
const result = yield righto(sqlite.getOne, db, 'SELECT * FROM test_table'); | ||
const result = await sqlite.getOne(db, 'SELECT * FROM test_table'); | ||
t.notOk(result); | ||
yield righto(sqlite.close, db); | ||
await db.close(); | ||
}); | ||
test('migrate examples up, down, up', function * (t) { | ||
test('migrate examples up, down, up', async function (t) { | ||
t.plan(2); | ||
@@ -58,17 +58,17 @@ | ||
const db = yield righto(sqlite.connect, './test.sqlite'); | ||
const driver = yield righto.sync(migrator, db); | ||
const db = await sqlite.connect('./test.sqlite'); | ||
const driver = migrator(db); | ||
const migrations = getMigrationsFromDirectory('./test/migrations'); | ||
yield righto(up, driver, migrations); | ||
yield righto(down, driver, migrations, 1); | ||
yield righto(up, driver, migrations); | ||
await up(driver, migrations, null); | ||
await down(driver, migrations, null, 1); | ||
await up(driver, migrations, null); | ||
t.ok(fs.existsSync('./test.sqlite')); | ||
const result = yield righto(sqlite.getOne, db, 'SELECT * FROM test_table'); | ||
const result = await sqlite.getOne(db, 'SELECT * FROM test_table'); | ||
t.equal(result.test, 'hello'); | ||
yield righto(sqlite.close, db); | ||
await db.close(); | ||
}); | ||
test('migrate examples up with error', function * (t) { | ||
test('migrate examples up with error', async function (t) { | ||
t.plan(1); | ||
@@ -78,15 +78,15 @@ | ||
const db = yield righto(sqlite.connect, './test.sqlite'); | ||
const driver = yield righto.sync(migrator, db); | ||
const migrations = getMigrationsFromDirectory('./test/migrationsError'); | ||
const upped = righto(up, driver, migrations); | ||
yield righto.handle(upped, function (error, callback) { | ||
const db = await sqlite.connect('./test.sqlite'); | ||
try { | ||
const driver = migrator(db); | ||
const migrations = getMigrationsFromDirectory('./test/migrationsError'); | ||
await up(driver, migrations, null); | ||
} catch (error) { | ||
t.ok(error.message.includes('syntax error')); | ||
sqlite.close(db, callback); | ||
}); | ||
await db.close(); | ||
} | ||
}); | ||
test('migrate examples up with no init or finish', function * (t) { | ||
test('migrate examples up with no init or finish', async function (t) { | ||
t.plan(1); | ||
@@ -105,5 +105,5 @@ | ||
const driver = migrator(); | ||
up(driver, [], function (error, result) { | ||
t.notOk(error); | ||
}); | ||
await up(driver, []); | ||
t.pass(); | ||
}); |
@@ -1,11 +0,9 @@ | ||
const sqlite = require('sqlite-fp'); | ||
module.exports = { | ||
up: (db, callback) => { | ||
return sqlite.run(db, 'CREATE TABLE test_table (test TEXT)', callback); | ||
up: (db) => { | ||
return db.run('CREATE TABLE test_table (test TEXT)'); | ||
}, | ||
down: (db, callback) => { | ||
return sqlite.run(db, 'DROP TABLE test_table', callback); | ||
down: (db) => { | ||
return db.run('DROP TABLE test_table'); | ||
} | ||
}; |
@@ -1,11 +0,9 @@ | ||
const sqlite = require('sqlite-fp'); | ||
module.exports = { | ||
up: (db, callback) => { | ||
return sqlite.run(db, 'INSERT INTO test_table (test) VALUES (?)', ['hello'], callback); | ||
up: (db) => { | ||
return db.run('INSERT INTO test_table (test) VALUES (?)', ['hello']); | ||
}, | ||
down: (db, callback) => { | ||
return sqlite.run(db, 'DELETE FROM test_table WHERE test = ?', ['hello'], callback); | ||
down: (db) => { | ||
return db.run('DELETE FROM test_table WHERE test = ?', ['hello']); | ||
} | ||
}; |
@@ -1,23 +0,29 @@ | ||
const sqlite = require('sqlite-fp'); | ||
const sqlite = require('sqlite-fp/promises'); | ||
module.exports = function (db) { | ||
const monad = { | ||
run: (...args) => sqlite.run(db, ...args), | ||
getAll: (...args) => sqlite.getAll(db, ...args), | ||
getOne: (...args) => sqlite.getOne(db, ...args) | ||
}; | ||
return { | ||
init: (direction, callback) => { | ||
sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);', callback); | ||
init: (direction) => { | ||
return sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);'); | ||
}, | ||
getMigrationState: (id, callback) => { | ||
sqlite.getOne(db, 'SELECT file FROM _migrations WHERE file = ?', [id], callback); | ||
getMigrationState: (id) => { | ||
return sqlite.getOne(db, 'SELECT file FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
setMigrationUp: (id, callback) => { | ||
sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id], callback); | ||
setMigrationUp: (id) => { | ||
return sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id]); | ||
}, | ||
setMigrationDown: (id, callback) => { | ||
sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id], callback); | ||
setMigrationDown: (id) => { | ||
return sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
handler: (fn, callback) => fn(db, callback) | ||
handler: (fn) => fn(monad) | ||
}; | ||
}; |
@@ -1,11 +0,9 @@ | ||
const sqlite = require('sqlite-fp'); | ||
module.exports = { | ||
up: (db, callback) => { | ||
return sqlite.run(db, 'CREATE_WHOOPS TABLE test_table (test TEXT)', callback); | ||
up: (db) => { | ||
return db.run('CREATE_WHOOPS TABLE test_table (test TEXT)'); | ||
}, | ||
down: (db, callback) => { | ||
return sqlite.run(db, 'DROP TABLE test_table', callback); | ||
down: (db) => { | ||
return db.run('DROP TABLE test_table'); | ||
} | ||
}; |
@@ -1,11 +0,9 @@ | ||
const sqlite = require('sqlite-fp'); | ||
module.exports = { | ||
up: (db, callback) => { | ||
return sqlite.run(db, 'INSERT INTO test_table (test) VALUES (?)', ['hello'], callback); | ||
up: (db) => { | ||
return db.run('INSERT INTO test_table (test) VALUES (?)', ['hello']); | ||
}, | ||
down: (db, callback) => { | ||
return sqlite.run(db, 'DELETE FROM test_table WHERE test = ?', ['hello'], callback); | ||
down: (db) => { | ||
return db.run('DELETE FROM test_table WHERE test = ?', ['hello']); | ||
} | ||
}; |
@@ -1,23 +0,29 @@ | ||
const sqlite = require('sqlite-fp'); | ||
const sqlite = require('sqlite-fp/promises'); | ||
module.exports = function (db) { | ||
const monad = { | ||
run: (...args) => sqlite.run(db, ...args), | ||
getAll: (...args) => sqlite.getAll(db, ...args), | ||
getOne: (...args) => sqlite.getOne(db, ...args) | ||
}; | ||
return { | ||
init: (direction, callback) => { | ||
sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);', callback); | ||
init: (direction) => { | ||
return sqlite.run(db, 'CREATE TABLE IF NOT EXISTS _migrations (file TEXT PRIMARY KEY);'); | ||
}, | ||
getMigrationState: (id, callback) => { | ||
sqlite.get(db, 'SELECT file FROM _migrations WHERE file = ?', [id], callback); | ||
getMigrationState: (id) => { | ||
return sqlite.getOne(db, 'SELECT file FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
setMigrationUp: (id, callback) => { | ||
sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id], callback); | ||
setMigrationUp: (id) => { | ||
return sqlite.run(db, 'INSERT INTO _migrations (file) VALUES (?)', [id]); | ||
}, | ||
setMigrationDown: (id, callback) => { | ||
sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id], callback); | ||
setMigrationDown: (id) => { | ||
return sqlite.run(db, 'DELETE FROM _migrations WHERE file = ?', [id]); | ||
}, | ||
handler: (fn, callback) => fn(db, callback) | ||
handler: (fn) => fn(monad) | ||
}; | ||
}; |
40
up.js
@@ -1,34 +0,26 @@ | ||
const righto = require('righto'); | ||
const rightoSeries = require('righto-series'); | ||
async function up (driver, migrations, logger) { | ||
logger = logger || (() => null); | ||
function up (driver, migrations, logger, callback) { | ||
if (arguments.length === 3) { | ||
callback = logger; | ||
logger = () => null; | ||
if (driver.init) { | ||
await driver.init('up'); | ||
} | ||
rightoSeries(function * () { | ||
if (driver.init) { | ||
yield righto(driver.init, 'up'); | ||
} | ||
for (const migration of migrations) { | ||
const active = await driver.getMigrationState(migration.id); | ||
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}`); | ||
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); | ||
} | ||
await driver.handler(migration.up); | ||
await driver.setMigrationUp(migration.id); | ||
} | ||
} | ||
if (driver.finish) { | ||
yield righto(driver.finish, 'up'); | ||
} | ||
}, callback); | ||
if (driver.finish) { | ||
await driver.finish('up'); | ||
} | ||
} | ||
module.exports = up; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11975
16
217