<commit_before>"use strict";
/* -------------------------------------------------------------------
 * Require Statements << Keep in alphabetical order >>
 * ---------------------------------------------------------------- */

var Crypto = require('crypto');
var dateFormat = require('dateformat');
var Fs = require('fs');
var ODBC = require('odbc');
var Path = require('path');

/* =============================================================================
 * 
 * Mayflower Class
 * 
 * All of the methods are synchronous for ease of development.
 *  
 * ========================================================================== */

module.exports = Mayflower;

function Mayflower(connectionString, migrateDirectory, migrationsTable) {

  /* -------------------------------------------------------------------
   * Private Members Declaration << no methods >>
   * ---------------------------------------------------------------- */

  var _this = this;

  var _tableExists = false;
  var _splitOnGo = /^\s*GO\s*$/gim;

  if (!migrationsTable)
    migrationsTable = 'Migrations';
  
  var MIGRATIONS_TABLE_SCHEMA =
    "CREATE TABLE [" + migrationsTable + "](\
      [Id] [int] IDENTITY(1,1) NOT NULL,\
      [Filename] [nvarchar](260) NULL,\
      [Hash] [varchar](40) NULL,\
      [ExecutionDate] [datetime] NULL,\
      [Duration] [int] NULL,\
      \
      PRIMARY KEY CLUSTERED \
      (\
        [Id] ASC\
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]\
    ) ON [PRIMARY]\
    ";

  /* -------------------------------------------------------------------
   * Public Methods << Keep in alphabetical order >>
   * ---------------------------------------------------------------- */

  this.getAllScripts = function () {
    var sqlExt = /.+\.sql$/;
    var names = [];
    var files = Fs.readdirSync(migrateDirectory);

    for (var i in files) {
      if (sqlExt.test(files[i]))
        names.push(files[i]);
    }

    names.sort();
    return names.map(_this.getScript);
  };

  this.getDbConnection = function () {
    var db = ODBC();
    db.openSync(connectionString);
    return db;
  };

  this.getScript = function (name) {
    var sql = Fs.readFileSync(Path.join(migrateDirectory, name), { encoding: 'utf8' });

    return {
      name: name,
      hash: getHash(sql),
      commands: sql.split(_splitOnGo)
    };
  };

  this.migrateAll = function (options) {
    try {
      //default options
      if (!options || typeof options !== 'object') {
        options = { output: true };
      }

      //open db and read sql script files
      var db = _this.getDbConnection();
      var scripts = _this.getAllScripts();

      //migrate each script
      var skipped = 0;
      var results = new Array(scripts.length);
      for (var i in scripts) {
        results[i] = _this.migrate(db, options, scripts[i]);

        if (results[i].skipped)
          skipped++;

        if (results[i].message && options.output) {
          console.log(results[i].message);
        }
      }

      if (options.output && skipped > 0) {
        console.log(skipped + ' previously applied migrations were skipped.');
      }

      return results;

    } catch (ex) {
      db.closeSync();
      return ex;
    }
  };

  this.migrate = function (db, options, script) {
    var ret = {
      skipped: true,<commit_msg>Improved options defaults and added script name to results.<commit_after>"use strict";
/* -------------------------------------------------------------------
 * Require Statements << Keep in alphabetical order >>
 * ---------------------------------------------------------------- */

var Crypto = require('crypto');
var dateFormat = require('dateformat');
var Fs = require('fs');
var ODBC = require('odbc');
var Path = require('path');

/* =============================================================================
 * 
 * Mayflower Class
 * 
 * All of the methods are synchronous for ease of development.
 *  
 * ========================================================================== */

module.exports = Mayflower;

function Mayflower(connectionString, migrateDirectory, migrationsTable) {

  /* -------------------------------------------------------------------
   * Private Members Declaration << no methods >>
   * ---------------------------------------------------------------- */

  var _this = this;

  var _tableExists = false;
  var _splitOnGo = /^\s*GO\s*$/gim;

  if (!migrationsTable)
    migrationsTable = 'Migrations';
  
  var MIGRATIONS_TABLE_SCHEMA =
    "CREATE TABLE [" + migrationsTable + "](\
      [Id] [int] IDENTITY(1,1) NOT NULL,\
      [Filename] [nvarchar](260) NULL,\
      [Hash] [varchar](40) NULL,\
      [ExecutionDate] [datetime] NULL,\
      [Duration] [int] NULL,\
      \
      PRIMARY KEY CLUSTERED \
      (\
        [Id] ASC\
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]\
    ) ON [PRIMARY]\
    ";

  /* -------------------------------------------------------------------
   * Public Methods << Keep in alphabetical order >>
   * ---------------------------------------------------------------- */

  this.getAllScripts = function () {
    var sqlExt = /.+\.sql$/;
    var names = [];
    var files = Fs.readdirSync(migrateDirectory);

    for (var i in files) {
      if (sqlExt.test(files[i]))
        names.push(files[i]);
    }

    names.sort();
    return names.map(_this.getScript);
  };

  this.getDbConnection = function () {
    var db = ODBC();
    db.openSync(connectionString);
    return db;
  };

  this.getScript = function (name) {
    var sql = Fs.readFileSync(Path.join(migrateDirectory, name), { encoding: 'utf8' });

    return {
      name: name,
      hash: getHash(sql),
      commands: sql.split(_splitOnGo)
    };
  };

  this.migrateAll = function (options) {
    try {
      //default options
      if (!options || typeof options !== 'object') {
        options = {};
      }
      
      if (options.output === undefined)
        options.output = true;

      //open db and read sql script files
      var db = _this.getDbConnection();
      var scripts = _this.getAllScripts();

      //migrate each script
      var skipped = 0;
      var results = new Array(scripts.length);
      for (var i in scripts) {
        results[i] = _this.migrate(db, options, scripts[i]);

        if (results[i].skipped)
          skipped++;

        if (results[i].message && options.output) {
          console.log(results[i].message);
        }
      }

      if (options.output && skipped > 0) {
        console.log(skipped + ' previously applied migrations were skipped.');
      }

      return results;

    } catch (ex) {
      db.closeSync();
      return ex;
    }
  };

  this.migrate = function (db, options, script) {
    var ret = {
      name: script.name,
      skipped: true,
{'fraction_non_alphanumeric': 0.2755351242637552, 'fraction_numerical': 0.0028731504094239332, 'mean_word_length': 1.7648927720413026, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5583846', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1901, 'n_words': 474}