'use strict';

exports.up = async function (db) {
    const AVATAR_NONE = Buffer.from('', 'utf8');
    const AVATAR_GRAVATAR = Buffer.from('gravatar', 'utf8');

    await db.runSql('ALTER TABLE users MODIFY avatar MEDIUMBLOB'); // remove NOT NULL
    const allUsers = await db.runSql('SELECT id, avatar FROM users', []);

    for (const user of allUsers) {
        if (AVATAR_GRAVATAR.equals(user.avatar) || AVATAR_NONE.equals(user.avatar)) {
            await db.runSql('UPDATE users SET avatar=? WHERE id=?', [ null, user.id ]); // drops avatar support. empty avatar is now null
        }
    }
};

exports.down = async function (db) {
    await db.runSql('ALTER TABLE users MODIFY avatar MEDIUMBLOB NOT NULL');
};
