Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createMigrationsTable (db, options, callback)
{
if (_tableExists)
setImmediate(callback);
// check if migrations table already exists
var request = db.request();
request.input('tableName', MsSql.NVarChar('max'), migrationsTable);
request.query("select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = @tableName", function (error, rows)
{
if (rows.length > 0)
{
_tableExists = true;
callback();
return;
}
if (options.preview)
{
callback();
return;
}
// create the migrations table
function (cb, rows)
{
var request;
if (rows.length > 0)
{
// migration has already been applied
var r = rows[0];
if (r.Filename !== script.name)
{
result.message = 'Filename has changed in the database; updating ' + script.name;
if (!options.preview)
{
request = db.request();
request.input('script', MsSql.NVarChar(260), script.name);
request.input('hash', MsSql.NVarChar(40), script.hash);
request.query('update [' + migrationsTable + '] set Filename = @script where [Hash] = @hash', cb.break);
return;
}
}
cb.break();
return;
}
// check for previous migration by filename
if (!_tableExists && options.preview)
{
cb(null, []);
}
else
app.get('/bulk', function(req, res) {
var table = new sql.Table('AnotherUserTable');
table.create = true;
table.columns.add('name', sql.NVarChar(40), { nullable: true });
table.columns.add('email', sql.NVarChar(40), { nullable: true });
table.rows.add('Domitian', 'domitian@flavius.com');
table.rows.add('Nerva', 'nerva@nerva.com');
table.rows.add('Trajan ', 'trajan@nerva.com');
new sql.Request().bulk(table, function(err, results) {
if (err) {
log('Failed to execute bulk operation.', err);
return res.status(500).json(err);
}
res.json(results);
});
});
{
var request = tran.request();
var sql;
if (exists)
{
sql = 'UPDATE [' + migrationsTable + '] SET [Hash] = @hash, [ExecutionDate] = @execDate, [Duration] = @duration WHERE [Filename] = @script';
}
else
{
sql = 'INSERT [' + migrationsTable + '] ([Hash], [ExecutionDate], [Duration], [Filename]) VALUES(@hash, @execDate, @duration, @script)';
}
request.input('hash', MsSql.NVarChar(40), script.hash);
request.input('execDate', MsSql.DateTime, new Date());
request.input('duration', MsSql.Int, result.runtime);
request.input('script', MsSql.NVarChar(260), script.name);
request.query(sql, cb);
}
else
{
cb();
}
},
function (cb)
app.ensurePrimaryKey = function (table, schema) {
if (!table || !table.columns || !_.isArray(table.columns)) return table;
let hasPrimaryKey = false;
let primaryKey = '';
table.columns.forEach(function (column) {
if (column.primary == true) {
primaryKey = column.name;
return hasPrimaryKey = true;
}
});
if (!hasPrimaryKey) {
primaryKey = schema._id || '_id';
table.columns.add(primaryKey, sql.NVarChar(255), {nullable: false, primary: true});
}
return primaryKey;
};
getTableList: function (tables, ignoreList) {
var request = new sql.Request();
request.input('table_owner', sql.NVarChar(50), "dbo");
return request.execute('sp_tables').
then(function (res) {
var tableList = res[0];
for (var i = 0; i < tableList.length; i++) {
if (ignoreList.indexOf(tableList[i].TABLE_NAME) > -1) {
continue;
}
tables.push({ name: tableList[i].TABLE_NAME });
}
})
},
exportTable: function (tableName, outputDirectory) {