<commit_before>
import { ObjectID } from 'mongodb';


function onCatch (err) {
    throw err;
}

function dummyPromiseCallback (resolve, reject, err) {
    if (err) {
        reject(err);
        throw err;
    }

    resolve();
}


export default class Migrate {
    constructor (db, CONST) {
        this._db = db;
        this._CONST = CONST;
    }

    start () {
        return this._mergeLayersInThemes()
        .then( this._mergePresetsInThemes.bind(this), onCatch);
    }

    _mergeLayersInThemes () {
        return new Promise((resolve, reject) => {
            let layerCollection = this._db.collection('poiLayer');
            let themeCollection = this._db.collection('theme');

            layerCollection.find().toArray((err, results) => {
                if(err) {
                    throw err;
                }

                if (results.length === 0) {
                    resolve();
                    return;
                }

                let layers = {};

                for (let layer of results) {
                    let themeId = layer.themeId;

                    if ( !layers[ themeId ] ) {
                        layers[ themeId ] = [];
                    }

                    layer.type = this._CONST.layerType.overpass;
                    layer.uniqid = layer._id.toString();

                    delete layer._id;
                    delete layer.themeId;

                    layers[ themeId ].push(layer);
                }

                for (let themeId in layers) {
                    themeCollection.updateOne({
                            '_id': new ObjectID(themeId)
                        },
                        {
                            '$set': {'layers': layers[ themeId ]}
                        },
                        dummyPromiseCallback.bind(this, resolve, reject)
                    );
                }

                layerCollection.rename('migrated_poiLayer');
            });
        });
    }

    _mergePresetsInThemes () {
        return new Promise((resolve, reject) => {
            let presetCollection = this._db.collection('preset');
            let themeCollection = this._db.collection('theme');

            presetCollection.find().toArray((err, results) => {
                if(err) {
                    throw err;
                }

                if (results.length === 0) {
                    resolve();
                    return;
                }

                let presets = {};

                for (let preset of results) {
                    let themeId = preset.themeId;

                    if ( !presets[ themeId ] ) {
                        presets[ themeId ] = [];
                    }

                    preset.uniqid = preset._id.toString();

                    delete preset._id;
                    delete preset.themeId;

                    presets[ themeId ].push(preset);
                }

                for (let themeId in presets) {
                    themeCollection.updateOne({
                            '_id': new ObjectID(themeId)
                        },
                        {
                            '$set': {'presets': presets[ themeId ]}
                        },
                        dummyPromiseCallback.bind(this, resolve, reject)
                    );
                }

                presetCollection.rename('migrated_preset');<commit_msg>Fix on the migrate script<commit_after>
import { ObjectID } from 'mongodb';


function onCatch (err) {
    throw err;
}

function dummyPromiseCallback (resolve, reject, err) {
    if (err) {
        reject(err);
        throw err;
    }

    resolve();
}


export default class Migrate {
    constructor (db, CONST) {
        this._db = db;
        this._CONST = CONST;
    }

    start () {
        return this._mergeLayersInThemes()
        .then( this._mergePresetsInThemes.bind(this), onCatch);
    }

    _mergeLayersInThemes () {
        return new Promise((resolve, reject) => {
            let layerCollection = this._db.collection('poiLayer');
            let themeCollection = this._db.collection('theme');

            layerCollection.find().toArray((err, results) => {
                if(err) {
                    throw err;
                }

                if (results.length === 0) {
                    resolve();
                    return;
                }

                let layers = {};

                for (let layer of results) {
                    let themeId = layer.themeId;

                    if (!themeId || themeId.length !== 24) {
                        continue;
                    }

                    if ( !layers[ themeId ] ) {
                        layers[ themeId ] = [];
                    }

                    layer.type = this._CONST.layerType.overpass;
                    layer.uniqid = layer._id.toString();

                    delete layer._id;
                    delete layer.themeId;

                    layers[ themeId ].push(layer);
                }

                for (let themeId in layers) {
                    themeCollection.updateOne({
                            '_id': new ObjectID(themeId)
                        },
                        {
                            '$set': {'layers': layers[ themeId ]}
                        },
                        dummyPromiseCallback.bind(this, resolve, reject)
                    );
                }

                layerCollection.rename('migrated_poiLayer');
            });
        });
    }

    _mergePresetsInThemes () {
        return new Promise((resolve, reject) => {
            let presetCollection = this._db.collection('preset');
            let themeCollection = this._db.collection('theme');

            presetCollection.find().toArray((err, results) => {
                if(err) {
                    throw err;
                }

                if (results.length === 0) {
                    resolve();
                    return;
                }

                let presets = {};

                for (let preset of results) {
                    let themeId = preset.themeId;

                    if (!themeId || themeId.length !== 24) {
                        continue;
                    }

                    if ( !presets[ themeId ] ) {
                        presets[ themeId ] = [];
                    }

                    preset.uniqid = preset._id.toString();

                    delete preset._id;
                    delete preset.themeId;

                    presets[ themeId ].push(preset);
                }

                for (let themeId in presets) {
                    themeCollection.updateOne({
                            '_id': new ObjectID(themeId)
                        },
                        {
                            '$set': {'presets': presets[ themeId ]}
                        },
                        dummyPromiseCallback.bind(this, resolve, reject)
                    );
                }

                presetCollection.rename('migrated_preset');
{'fraction_non_alphanumeric': 0.10036754311563471, 'fraction_numerical': 0.0011309018942606728, 'mean_word_length': 0.994643360586411, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4365516', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1614, 'n_words': 341}