db-migrate-base
Advanced tools
Comparing version 1.6.3 to 2.0.0
@@ -0,1 +1,11 @@ | ||
<a name="2.0.0"></a> | ||
# [2.0.0](https://github.com/db-migrate/db-migrate-base/compare/v1.6.3...v2.0.0) (2019-05-16) | ||
### Features | ||
* **methods:** add new migrator methods ([1783a66](https://github.com/db-migrate/db-migrate-base/commit/1783a66)) | ||
<a name="1.6.3"></a> | ||
@@ -2,0 +12,0 @@ ## [1.6.3](https://github.com/db-migrate/db-migrate-base/compare/v1.6.2...v1.6.3) (2019-02-11) |
250
index.js
@@ -11,3 +11,3 @@ var util = require('util'); | ||
var Base = Class.extend({ | ||
init: function(intern) { | ||
init: function (intern) { | ||
this._escapeDDL = this._escapeDDL || '"'; | ||
@@ -28,7 +28,7 @@ this._escapeString = this._escapeString || "'"; | ||
close: function() { | ||
close: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
_translateSpecialDefaultValues: function( | ||
_translateSpecialDefaultValues: function ( | ||
spec, | ||
@@ -47,3 +47,3 @@ options, | ||
_prepareSpec: function(columnName, spec, options, tableName) { | ||
_prepareSpec: function (columnName, spec, options, tableName) { | ||
if (spec.defaultValue) { | ||
@@ -63,3 +63,3 @@ if (spec.defaultValue.raw) { | ||
mapDataType: function(str) { | ||
mapDataType: function (str) { | ||
switch (str) { | ||
@@ -101,3 +101,3 @@ case type.STRING: | ||
truncate: function(tableName, callback) { | ||
truncate: function (tableName, callback) { | ||
return this.runSql( | ||
@@ -108,3 +108,3 @@ 'TRUNCATE ' + this._escapeDDL + tableName + this._escapeDDL | ||
checkDBMS: function(dbms, callback) { | ||
checkDBMS: function (dbms, callback) { | ||
if (this.dbms === dbms) return Promise.resolve(dbms).nodeify(callback); | ||
@@ -114,19 +114,19 @@ else return Promise.reject('dbms does not match'); | ||
createDatabase: function() { | ||
createDatabase: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
showDatabase: function() { | ||
showDatabase: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
switchDatabase: function() { | ||
switchDatabase: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
dropDatabase: function() { | ||
dropDatabase: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
recurseCallbackArray: function(foreignKeys, callback) { | ||
recurseCallbackArray: function (foreignKeys, callback) { | ||
var self = this, | ||
@@ -136,4 +136,5 @@ fkFunc, | ||
while ((fkFunc = foreignKeys.pop())) | ||
while ((fkFunc = foreignKeys.pop())) { | ||
promises.push(Promise.resolve(fkFunc())); | ||
} | ||
@@ -143,12 +144,12 @@ return Promise.all(promises).nodeify(callback); | ||
bindForeignKey: function(tableName, columnName, fkOptions) { | ||
bindForeignKey: function (tableName, columnName, fkOptions) { | ||
var self = this, | ||
mapping = {}; | ||
if (typeof fkOptions.mapping === 'string') | ||
if (typeof fkOptions.mapping === 'string') { | ||
mapping[columnName] = fkOptions.mapping; | ||
else mapping = fkOptions.mapping; | ||
} else mapping = fkOptions.mapping; | ||
return function(callback) { | ||
if (typeof callback === 'function') | ||
return function (callback) { | ||
if (typeof callback === 'function') { | ||
self.addForeignKey( | ||
@@ -162,3 +163,3 @@ tableName, | ||
); | ||
else | ||
} else { | ||
return self.addForeignKey( | ||
@@ -171,6 +172,7 @@ tableName, | ||
); | ||
} | ||
}; | ||
}, | ||
createColumnDef: function(name, spec, options) { | ||
createColumnDef: function (name, spec, options) { | ||
name = this._escapeDDL + name + this._escapeDDL; | ||
@@ -187,3 +189,3 @@ var type = this.mapDataType(spec.type); | ||
createMigrationsTable: function(callback) { | ||
_createList: function (table, opt = {}) { | ||
var options = { | ||
@@ -202,6 +204,40 @@ columns: { | ||
}; | ||
return this.createTable(table, options); | ||
}, | ||
_createKV: function (table, opt = { valueJson: false }) { | ||
var options = { | ||
columns: { | ||
key: { | ||
type: type.STRING, | ||
notNull: true, | ||
primaryKey: true, | ||
unique: true | ||
}, | ||
value: { type: type.TEXT, notNull: true }, | ||
run_on: { type: type.DATE_TIME, notNull: true } | ||
}, | ||
ifNotExists: true | ||
}; | ||
return this.createTable(table, options); | ||
}, | ||
createMigrationsTable: function (callback) { | ||
var options = { | ||
columns: { | ||
id: { | ||
type: type.INTEGER, | ||
notNull: true, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
name: { type: type.STRING, length: 255, notNull: true }, | ||
run_on: { type: type.DATE_TIME, notNull: true } | ||
}, | ||
ifNotExists: true | ||
}; | ||
this.createTable(this.internals.migrationTable, options, callback); | ||
}, | ||
createSeedsTable: function(callback) { | ||
createSeedsTable: function (callback) { | ||
var options = { | ||
@@ -223,7 +259,7 @@ columns: { | ||
_handleMultiPrimaryKeys: function(primaryKeyColumns) { | ||
_handleMultiPrimaryKeys: function (primaryKeyColumns) { | ||
return util.format( | ||
', PRIMARY KEY (%s)', | ||
this.quoteDDLArr( | ||
primaryKeyColumns.map(function(value) { | ||
primaryKeyColumns.map(function (value) { | ||
return value.name; | ||
@@ -235,3 +271,3 @@ }) | ||
createTable: function(tableName, options, callback) { | ||
createTable: function (tableName, options, callback) { | ||
log.verbose('creating table:', tableName); | ||
@@ -318,3 +354,3 @@ var columnSpecs = options; | ||
.then( | ||
function() { | ||
function () { | ||
return this.recurseCallbackArray(callbacks); | ||
@@ -326,3 +362,3 @@ }.bind(this) | ||
dropTable: function(tableName, options, callback) { | ||
dropTable: function (tableName, options, callback) { | ||
if (arguments.length < 3 && typeof options === 'function') { | ||
@@ -348,7 +384,7 @@ callback = options; | ||
renameTable: function(tableName, newTableName, callback) { | ||
renameTable: function (tableName, newTableName, callback) { | ||
throw new Error('not implemented'); | ||
}, | ||
addColumn: function(tableName, columnName, columnSpec, callback) { | ||
addColumn: function (tableName, columnName, columnSpec, callback) { | ||
var columnSpec = this.normalizeColumnSpec(columnSpec); | ||
@@ -372,3 +408,3 @@ this._prepareSpec(columnName, columnSpec, {}, tableName); | ||
return this.runSql(sql) | ||
.then(function() { | ||
.then(function () { | ||
var callbacks = def.callbacks || []; | ||
@@ -381,17 +417,18 @@ if (def.foreignKey) callbacks.push(def.foreignKey); | ||
removeColumn: function(tableName, columnName, callback) { | ||
removeColumn: function (tableName, columnName, callback) { | ||
throw new Error('not implemented'); | ||
}, | ||
renameColumn: function(tableName, oldColumnName, newColumnName, callback) { | ||
renameColumn: function (tableName, oldColumnName, newColumnName, callback) { | ||
throw new Error('not implemented'); | ||
}, | ||
changeColumn: function(tableName, columnName, columnSpec, callback) { | ||
changeColumn: function (tableName, columnName, columnSpec, callback) { | ||
throw new Error('not implemented'); | ||
}, | ||
quoteDDLArr: function(arr) { | ||
for (var i = 0; i < arr.length; ++i) | ||
quoteDDLArr: function (arr) { | ||
for (var i = 0; i < arr.length; ++i) { | ||
arr[i] = this._escapeDDL + arr[i] + this._escapeDDL; | ||
} | ||
@@ -401,5 +438,6 @@ return arr; | ||
quoteArr: function(arr) { | ||
for (var i = 0; i < arr.length; ++i) | ||
quoteArr: function (arr) { | ||
for (var i = 0; i < arr.length; ++i) { | ||
arr[i] = this._escapeString + arr[i] + this._escapeString; | ||
} | ||
@@ -409,3 +447,3 @@ return arr; | ||
addIndex: function(tableName, indexName, columns, unique, callback) { | ||
addIndex: function (tableName, indexName, columns, unique, callback) { | ||
if (typeof unique === 'function') { | ||
@@ -430,3 +468,3 @@ callback = unique; | ||
insert: function(tableName, valueArray, callback) { | ||
insert: function (tableName, valueArray, callback) { | ||
var columnNameArray = {}; | ||
@@ -439,3 +477,3 @@ | ||
var names; | ||
if (util.isArray(valueArray)) { | ||
if (Array.isArray(valueArray)) { | ||
names = Object.keys(valueArray[0]); | ||
@@ -465,3 +503,3 @@ } else { | ||
if (util.isArray(valueArray) && typeof valueArray[0] === 'object') { | ||
if (Array.isArray(valueArray) && typeof valueArray[0] === 'object') { | ||
for (var i = 0; i < valueArray.length; ++i) { | ||
@@ -489,3 +527,3 @@ values_part[i] = values_part[i] || ''; | ||
if (util.isArray(valueArray) && typeof valueArray[0] === 'object') { | ||
if (Array.isArray(valueArray) && typeof valueArray[0] === 'object') { | ||
for (var i = 0; i < values_part.length; ++i) { | ||
@@ -505,3 +543,3 @@ values += '(' + values_part[i].slice(0, -1) + '),'; | ||
update: function(tableName, valueArray, ids, callback) { | ||
update: function (tableName, valueArray, ids, callback) { | ||
var columnNameArray = {}; | ||
@@ -520,3 +558,3 @@ | ||
var names; | ||
if (util.isArray(valueArray)) { | ||
if (Array.isArray(valueArray)) { | ||
names = Object.keys(valueArray[0]); | ||
@@ -558,3 +596,3 @@ } else { | ||
lookup: function(tableName, column, id, callback) { | ||
lookup: function (tableName, column, id, callback) { | ||
var sql = | ||
@@ -568,3 +606,3 @@ 'SELECT ' + | ||
return this.runSql(sql).then(function(row) { | ||
return this.runSql(sql).then(function (row) { | ||
return row[0]; | ||
@@ -574,3 +612,3 @@ }); | ||
removeIndex: function(tableName, indexName, callback) { | ||
removeIndex: function (tableName, indexName, callback) { | ||
if (arguments.length === 2 && typeof indexName === 'function') { | ||
@@ -587,11 +625,11 @@ callback = indexName; | ||
addForeignKey: function() { | ||
addForeignKey: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
removeForeignKey: function() { | ||
removeForeignKey: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
normalizeColumnSpec: function(obj) { | ||
normalizeColumnSpec: function (obj) { | ||
if (typeof obj === 'string') { | ||
@@ -604,3 +642,35 @@ return { type: obj }; | ||
addMigrationRecord: function(name, callback) { | ||
_insertEntry: function (table, name) { | ||
return this.runSql( | ||
'INSERT INTO ' + | ||
this.escapeDDL(table) + | ||
' (' + | ||
this.escapeDDL('name') + | ||
', ' + | ||
this.escapeDDL('run_on') + | ||
') VALUES (?, ?)', | ||
[name, new Date()] | ||
); | ||
}, | ||
_insertKV: function (table, key, value) { | ||
return this.runSql( | ||
`INSERT INTO ${this.escapeDDL(table)} | ||
(${this.escapeDDL('key')}, ${this.escapeDDL('value')}, ${this.escapeDDL( | ||
'run_on' | ||
)}) VALUES (?, ?, ?)`, | ||
[key, value, new Date()] | ||
); | ||
}, | ||
_updateKV: function (table, key, value) { | ||
return this.runSql( | ||
`UPDATE ${this.escapeDDL(table)} SET ${this.escapeDDL('value')} = ?, | ||
${this.escapeDDL('run_on')} = ? | ||
WHERE key = ?`, | ||
[value, new Date(), key] | ||
); | ||
}, | ||
addMigrationRecord: function (name, callback) { | ||
this.runSql( | ||
@@ -619,3 +689,3 @@ 'INSERT INTO ' + | ||
addSeedRecord: function(name, callback) { | ||
addSeedRecord: function (name, callback) { | ||
this.runSql( | ||
@@ -634,6 +704,6 @@ 'INSERT INTO ' + | ||
startMigration: function(cb) { | ||
startMigration: function (cb) { | ||
return Promise.resolve().nodeify(cb); | ||
}, | ||
endMigration: function(cb) { | ||
endMigration: function (cb) { | ||
return Promise.resolve().nodeify(cb); | ||
@@ -643,6 +713,26 @@ }, | ||
// sql, callback | ||
runSql: function() { | ||
runSql: function () { | ||
throw new Error('not implemented'); | ||
}, | ||
_getList: function (table) { | ||
var sql = | ||
'SELECT * FROM ' + | ||
this._escapeDDL + | ||
table + | ||
this._escapeDDL + | ||
' ORDER BY run_on DESC, name DESC'; | ||
return this.allAsync(sql); | ||
}, | ||
_getKV: function (table, key) { | ||
var sql = | ||
'SELECT * FROM ' + | ||
this._escapeDDL + | ||
table + | ||
this._escapeDDL + | ||
' WHERE key = ?'; | ||
return this.allAsync(sql, [key]).then(([row]) => row); | ||
}, | ||
/** | ||
@@ -653,3 +743,3 @@ * Queries the migrations table | ||
*/ | ||
allLoadedMigrations: function(callback) { | ||
allLoadedMigrations: function (callback) { | ||
var sql = | ||
@@ -669,3 +759,3 @@ 'SELECT * FROM ' + | ||
*/ | ||
allLoadedSeeds: function(callback) { | ||
allLoadedSeeds: function (callback) { | ||
var sql = | ||
@@ -680,2 +770,22 @@ 'SELECT * FROM ' + | ||
_deleteEntry: function (table, entry) { | ||
var sql = | ||
'DELETE FROM ' + | ||
this._escapeDDL + | ||
table + | ||
this._escapeDDL + | ||
' WHERE name = ?'; | ||
return this.runSql(sql, [entry]); | ||
}, | ||
_deleteKV: function (table, key) { | ||
var sql = | ||
'DELETE FROM ' + | ||
this._escapeDDL + | ||
table + | ||
this._escapeDDL + | ||
' WHERE key = ?'; | ||
return this.runSql(sql, [key]); | ||
}, | ||
/** | ||
@@ -686,3 +796,3 @@ * Deletes a migration | ||
*/ | ||
deleteMigration: function(migrationName, callback) { | ||
deleteMigration: function (migrationName, callback) { | ||
var sql = | ||
@@ -718,3 +828,3 @@ 'DELETE FROM ' + | ||
*/ | ||
remove: function(table, ids, callback) { | ||
remove: function (table, ids, callback) { | ||
var sql = 'DELETE FROM ' + this._escapeDDL + table + +this._escapeDDL; | ||
@@ -746,6 +856,6 @@ var searchClause = ''; | ||
*/ | ||
buildWhereClause: function(ids) { | ||
buildWhereClause: function (ids) { | ||
var searchClause = ''; | ||
if (util.isArray(ids) && typeof ids[0] !== 'object' && ids.length > 1) { | ||
if (Array.isArray(ids) && typeof ids[0] !== 'object' && ids.length > 1) { | ||
searchClause += | ||
@@ -760,6 +870,6 @@ 'WHERE id IN (' + | ||
) { | ||
var id = util.isArray(ids) ? ids[0] : ids; | ||
var id = Array.isArray(ids) ? ids[0] : ids; | ||
searchClause += | ||
'WHERE id = ' + this._escapeString + id + this._escapeString; | ||
} else if (util.isArray(ids) && typeof ids[0] === 'object') { | ||
} else if (Array.isArray(ids) && typeof ids[0] === 'object') { | ||
var preLink = ''; | ||
@@ -783,4 +893,4 @@ searchClause = ' WHERE '; | ||
(column.name = column.name || 'id'), | ||
(column.operator = column.operator || '='), | ||
(column.link = column.link || 'AND'); | ||
(column.operator = column.operator || '='), | ||
(column.link = column.link || 'AND'); | ||
@@ -853,3 +963,3 @@ if (!column.value) { | ||
*/ | ||
deleteSeed: function(seedName, callback) { | ||
deleteSeed: function (seedName, callback) { | ||
var sql = | ||
@@ -864,7 +974,7 @@ 'DELETE FROM ' + | ||
all: function(sql, params, callback) { | ||
all: function (sql, params, callback) { | ||
throw new Error('not implemented'); | ||
}, | ||
escape: function(str) { | ||
escape: function (str) { | ||
if (this._escapeString === "'") return str.replace(/'/g, "''"); | ||
@@ -874,7 +984,7 @@ else return str.replace(/"/g, '"'); | ||
escapeString: function(str) { | ||
escapeString: function (str) { | ||
return this._escapeString + this.escape(str) + this._escapeString; | ||
}, | ||
escapeDDL: function(str) { | ||
escapeDDL: function (str) { | ||
return this._escapeDDL + str + this._escapeDDL; | ||
@@ -881,0 +991,0 @@ } |
{ | ||
"name": "db-migrate-base", | ||
"version": "1.6.3", | ||
"version": "2.0.0", | ||
"description": "db-migrate base driver", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
32409
877