<commit_before>'use strict';

const strategies = require('./default-strategies.json');
const async = require('async');

function insertStrategySQL (strategy) {
    return `
        INSERT INTO strategies (name, description, parameters, built_in)
        SELECT '${strategy.name}', '${strategy.description}', '${JSON.stringify(strategy.parameters)}', 1
        WHERE
            NOT EXISTS (
                SELECT name FROM strategies WHERE name = '${strategy.name}'
        );`;
}

function insertEventsSQL (strategy) {
    return `
        INSERT INTO events (type, created_by, data)
        SELECT 'strategy-created', 'migration', '${JSON.stringify(strategy)}'
        WHERE
            NOT EXISTS (
                SELECT name FROM strategies WHERE name = '${strategy.name}'
        );`;
}

exports.up = function (db, callback) {
    const insertStrategies = strategies.map((s) => (cb) => {
        db.runSql(insertEventsSQL(s), cb);
        db.runSql(insertStrategySQL(s), cb);
    });
    async.series(insertStrategies, callback);
};

exports.down = function (db, cb) {
    return cb();
};<commit_msg>migration:down should remove built_in strategies created in up<commit_after>'use strict';

const strategies = require('./default-strategies.json');
const async = require('async');

function insertStrategySQL (strategy) {
    return `
        INSERT INTO strategies (name, description, parameters, built_in)
        SELECT '${strategy.name}', '${strategy.description}', '${JSON.stringify(strategy.parameters)}', 1
        WHERE
            NOT EXISTS (
                SELECT name FROM strategies WHERE name = '${strategy.name}'
        );`;
}

function insertEventsSQL (strategy) {
    return `
        INSERT INTO events (type, created_by, data)
        SELECT 'strategy-created', 'migration', '${JSON.stringify(strategy)}'
        WHERE
            NOT EXISTS (
                SELECT name FROM strategies WHERE name = '${strategy.name}'
        );`;
}

function removeEventsSQL (strategy) {
    return `
        INSERT INTO events (type, created_by, data)
        SELECT 'strategy-deleted', 'migration', '${JSON.stringify(strategy)}'
        WHERE
            EXISTS (
                SELECT name FROM strategies WHERE name = '${strategy.name}' AND built_in = 1
        );`;
}

function removeStrategySQL (strategy) {
    return `
        DELETE FROM strategies
        WHERE name = '${strategy.name}' AND built_in = 1`;
}

exports.up = function (db, callback) {
    const insertStrategies = strategies.map((s) => (cb) => {
        db.runSql(insertEventsSQL(s), cb);
        db.runSql(insertStrategySQL(s), cb);
    });
    async.series(insertStrategies, callback);
};

exports.down = function (db, callback) {
    const removeStrategies = strategies
        .filter(s => s.name !== 'default')
        .map((s) => (cb) => {
            db.runSql(removeEventsSQL(s), cb);
            db.runSql(removeStrategySQL(s), cb);
        });

    async.series(removeStrategies, callback);
};
{'fraction_non_alphanumeric': 0.13674356402540958, 'fraction_numerical': 0.0013373453694416582, 'mean_word_length': 2.754077791718946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '6266038', 'n_tokens_mistral': 886, 'n_tokens_neox': 849, 'n_words': 214}