'use strict';

exports.up = async function(db) {
    const results = await db.runSql('SELECT * FROM settings WHERE name=?', ['autoupdate_pattern']);
    if (results.length === 0) return; // use defaults from box code

    let policy, schedule;

    if (results[0].value === 'never') {
        policy = 'never';
        schedule = '00 00 1,3,5,23 * * *';
    } else {
        policy = 'platform_and_apps';
        schedule = results[0].value;
    }

    await db.runSql('START TRANSACTION;');
    await db.runSql('INSERT settings (name, value) VALUES(?, ?)', ['autoupdate_schedule', schedule]);
    await db.runSql('INSERT settings (name, value) VALUES(?, ?)', ['autoupdate_policy', policy]);
    await db.runSql('DELETE FROM settings WHERE name=?', ['autoupdate_pattern']);
    await db.runSql('COMMIT');
};

exports.down = async function() {
};
