<commit_before>/*

	API:

    var postgrator = require('postgrator');

    postgrator.setConfig({
        driver: 'pg', // or pg.js, mysql, mssql, tedious
        migrationDirectory: '',
        logProgress: true,
        schemaTable: '', // default is 'schemaversion'
        host: '',
        database: '',
        username: '',
        password: ''
    });

    postgrator.migrate(version, function (err, migrations) {
        // handle the error, and if you want end the connection
        postgrator.endConnection();
    });


	NOTES:

	If the table specified by config.schemaTable is not present, it will be created automatically!
	If no migration version is supplied, no migration is performed

	THINGS TO IMPLEMENT SOMEDAY (MAYBE)

	postgrator.migrate('max', callback); 	// migrate to the latest migration available

================================================================= */

var fs = require('fs');
var crypto = require('crypto');
var Promise = require('knex/lib/promise');
var createCommonClient = require('./lib/create-common-client.js');

var commonClient;
var currentVersion;
var targetVersion;
var migrations = []; // array of objects like: {version: n, action: 'do', direction: 'up', filename: '0001.up.sql'}

var config = {};

exports.config = config;



/*  Set Config
================================================================= */
exports.setConfig = function (configuration) {
    config = configuration;
    config.schemaTable = config.schemaTable || 'schemaversion';

    commonClient = createCommonClient(config);
};



/*  Migration Sorting Functions
================================================================= */
var sortMigrationsAsc = function (a,b) {
	if (a.version < b.version)
		return -1;
	if (a.version > b.version)
		return 1;
	return 0;
};

var sortMigrationsDesc = function (a, b) {
	if (a.version < b.version)
		return 1;
	if (a.version > b.version)
		return -1;
	return 0;
};



/*
	getMigrations()

	Internal function
	Reads the migration directory for all the migration files.
	It is SYNC out of laziness and simplicity

================================================================= */
var getMigrations = function () {
	migrations = [];
	var migrationFiles = fs.readdirSync(config.migrationDirectory);
	migrationFiles.forEach(function(file) {
		var m = file.split('.');
		var name = m.length >= 3 ? m.slice(2, m.length - 1).join('.') : file;
		var ext = m[m.length - 1];
		if (ext === 'sql' || ext === 'js') {
			migrations.push({
				version: Number(m[0]),
				direction: m[1],
				action: m[1],
				filename: file,
				name: name,
				md5: fileChecksum(config.migrationDirectory + "/" + file, config.newline)
			});
		}
	});
};


/*  runQuery
    connects the database driver if it is not currently connected.
    Executes an arbitrary sql query using the common client
================================================================= */
function runQuery (query, cb) {
	if (commonClient.connected) {
		commonClient.runQuery(query, cb);
	} else {
		// connect common client
		commonClient.createConnection(function (err) {
		    if (err) cb(err);
		    else {
		        commonClient.connected = true;
		        commonClient.runQuery(query, cb);
		    }
		});
	}
}
exports.runQuery = runQuery;

/*  runMigrationQuery
    Execute the function to run the migration query
================================================================= */
function runMigrationQuery (useKnex, query, cb) {
	if(!useKnex) {
		runQuery(query, cb);
	} else if(query) {
		Promise.all(query(commonClient.knex)).then(function(){
			cb();
		});
	}
}<commit_msg>check if do/undo functions return an array - otherwise, just use chaining<commit_after>/*

	API:

    var postgrator = require('postgrator');

    postgrator.setConfig({
        driver: 'pg', // or pg.js, mysql, mssql, tedious
        migrationDirectory: '',
        logProgress: true,
        schemaTable: '', // default is 'schemaversion'
        host: '',
        database: '',
        username: '',
        password: ''
    });

    postgrator.migrate(version, function (err, migrations) {
        // handle the error, and if you want end the connection
        postgrator.endConnection();
    });


	NOTES:

	If the table specified by config.schemaTable is not present, it will be created automatically!
	If no migration version is supplied, no migration is performed

	THINGS TO IMPLEMENT SOMEDAY (MAYBE)

	postgrator.migrate('max', callback); 	// migrate to the latest migration available

================================================================= */

var _ = require('lodash');
var fs = require('fs');
var crypto = require('crypto');
var Promise = require('knex/lib/promise');
var createCommonClient = require('./lib/create-common-client.js');

var commonClient;
var currentVersion;
var targetVersion;
var migrations = []; // array of objects like: {version: n, action: 'do', direction: 'up', filename: '0001.up.sql'}

var config = {};

exports.config = config;



/*  Set Config
================================================================= */
exports.setConfig = function (configuration) {
    config = configuration;
    config.schemaTable = config.schemaTable || 'schemaversion';

    commonClient = createCommonClient(config);
};



/*  Migration Sorting Functions
================================================================= */
var sortMigrationsAsc = function (a,b) {
	if (a.version < b.version)
		return -1;
	if (a.version > b.version)
		return 1;
	return 0;
};

var sortMigrationsDesc = function (a, b) {
	if (a.version < b.version)
		return 1;
	if (a.version > b.version)
		return -1;
	return 0;
};



/*
	getMigrations()

	Internal function
	Reads the migration directory for all the migration files.
	It is SYNC out of laziness and simplicity

================================================================= */
var getMigrations = function () {
	migrations = [];
	var migrationFiles = fs.readdirSync(config.migrationDirectory);
	migrationFiles.forEach(function(file) {
		var m = file.split('.');
		var name = m.length >= 3 ? m.slice(2, m.length - 1).join('.') : file;
		var ext = m[m.length - 1];
		if (ext === 'sql' || ext === 'js') {
			migrations.push({
				version: Number(m[0]),
				direction: m[1],
				action: m[1],
				filename: file,
				name: name,
				md5: fileChecksum(config.migrationDirectory + "/" + file, config.newline)
			});
		}
	});
};


/*  runQuery
    connects the database driver if it is not currently connected.
    Executes an arbitrary sql query using the common client
================================================================= */
function runQuery (query, cb) {
	if (commonClient.connected) {
		commonClient.runQuery(query, cb);
	} else {
		// connect common client
		commonClient.createConnection(function (err) {
		    if (err) cb(err);
		    else {
		        commonClient.connected = true;
		        commonClient.runQuery(query, cb);
		    }
		});
	}
}
exports.runQuery = runQuery;

/*  runMigrationQuery
    Execute the function to run the migration query
================================================================= */
function runMigrationQuery (useKnex, query, cb) {
	var knexQueries;
	if(!useKnex) {
		runQuery(query, cb);
	} else if(query) {
		knexQueries = query(commonClient.knex);

		if(_.isArray(knexQueries)) {
			Promise.all(knexQueries).then(function(){
				cb();
			});
		} else {
			knexQueries.then(function(){
				cb();
			})
		}
	}
}
{'fraction_non_alphanumeric': 0.22573272385049745, 'fraction_numerical': 0.004840010755579457, 'mean_word_length': 3.7052498418722326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6365204', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2125, 'n_words': 598}