<commit_before>var semver = require('semver');
var massive = require('massive');
var Migration = require('massive-migrate');
var fs = require('fs');
var serialization = require('typewise-semver');
var parse = serialization.parse;
var stringify = serialization.stringify;
var bytewise = require('bytewise-core');


function orderMigrations(results) {
    var migrations = {};

    for (var i = 0; i < results.length; i++) {
        var splitted = results[i].split('.');
        for(var j=0;j<splitted.length;j++) {
            splitted[j] = parseInt(splitted[j])
        }
        migrations[results[i]] = splitted
    }

    var unorderedmigrations = Object.keys(migrations).slice();

    var sorted = [];

    unorderedmigrations.map(function (key) {
        return migrations[key]
    })
        .map(bytewise.encode)
        .sort(bytewise.compare)
        .map(bytewise.decode)
        .forEach(function (v, j) {
            sorted.push(stringify(v));
            var x = stringify(v);
        });
    return sorted;
}

module.exports.up = function (options, callback) {

    if (semver.valid(options.version)) {
        massive.connect({connectionString: options.connectionString, scripts: __dirname + '/db'}, function (err, db) {
            if (!err) {
                var migrationTableExist = false;
                for (var i = 0; i < db.tables.length; i++) {
                    if (db.tables[i].name === 'pgmigration') {
                        migrationTableExist = true;
                    }
                }


                fs.readdir(options.migrationsDirectory, function (err, results) {
                    var sorted = orderMigrations(results);

                    if (migrationTableExist) {
                        db.pgmigration.findOne({version: options.version}, function (err, migration) {
                            if (!err) {
                                console.log('migration', migration);
                                if (typeof migration === typeof undefined) {

                                    console.log(sorted.indexOf(options.version));

                                    // last migration?
                                    if(sorted.indexOf(options.version) === sorted.length-1) {

                                        var migration = new Migration(
                                            options.connectionString,
                                            options.migrationsDirectory,
                                            options.version, function () {
                                                migration.up(options.migrationScript);
                                            });
                                    }

                                } else {
                                    console.log('Migration ' + options.version + ' already applied')
                                }
                            }
                        })
                    }


                });

            }
        });

        if (callback) {
            callback()
        }
    }


};<commit_msg>Fix duplicate declaration.<commit_after>var semver = require('semver');
var massive = require('massive');
var Migration = require('massive-migrate');
var fs = require('fs');
var serialization = require('typewise-semver');
var parse = serialization.parse;
var stringify = serialization.stringify;
var bytewise = require('bytewise-core');


function orderMigrations(results) {
    var migrations = {};

    for (var i = 0; i < results.length; i++) {
        var splitted = results[i].split('.');
        for(var j=0;j<splitted.length;j++) {
            splitted[j] = parseInt(splitted[j])
        }
        migrations[results[i]] = splitted
    }

    var unorderedmigrations = Object.keys(migrations).slice();

    var sorted = [];

    unorderedmigrations.map(function (key) {
        return migrations[key]
    })
        .map(bytewise.encode)
        .sort(bytewise.compare)
        .map(bytewise.decode)
        .forEach(function (v, j) {
            sorted.push(stringify(v));
            var x = stringify(v);
        });
    return sorted;
}

module.exports.up = function (options, callback) {

    if (semver.valid(options.version)) {
        massive.connect({connectionString: options.connectionString, scripts: __dirname + '/db'}, function (err, db) {
            if (!err) {
                var migrationTableExist = false;
                for (var i = 0; i < db.tables.length; i++) {
                    if (db.tables[i].name === 'pgmigration') {
                        migrationTableExist = true;
                    }
                }


                fs.readdir(options.migrationsDirectory, function (err, results) {
                    var sorted = orderMigrations(results);

                    if (migrationTableExist) {
                        db.pgmigration.findOne({version: options.version}, function (err, exitstingMigration) {
                            if (!err) {
                                console.log('migration', exitstingMigration);
                                if (typeof exitstingMigration === typeof undefined) {

                                    console.log(sorted.indexOf(options.version));

                                    // last migration?
                                    if(sorted.indexOf(options.version) === sorted.length-1) {

                                        var migration = new Migration(
                                            options.connectionString,
                                            options.migrationsDirectory,
                                            options.version, function () {
                                                migration.up(options.migrationScript);
                                            });
                                    }

                                } else {
                                    console.log('Migration ' + options.version + ' already applied')
                                }
                            }
                        })
                    }


                });

            }
        });

        if (callback) {
            callback()
        }
    }


};
{'fraction_non_alphanumeric': 0.10866065679330328, 'fraction_numerical': 0.00128783000643915, 'mean_word_length': 1.2252865329512894, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549133', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1430, 'n_words': 284}