'use strict';

exports.up = async function (db) {
    const tableNames = [
        'userGroups', 'users', 'domains', 'tasks', 'volumes', 'apps', 'backups', 'mail',

        'appAddonConfigs', 'appEnvVars', 'appMounts', 'appPasswords', 'appPortBindings', 'applinks', 'archives',
        'blobs', 'dockerRegistries', 'eventlog', 'groupMembers', 'locations', 'locks', 'mailboxes',
        'migrations', 'notifications', 'oidcClients', 'settings', 'tokens'
    ];

    await db.runSql('SET foreign_key_checks = 0');

    // this should already be the default
    console.log('Changing database charset');
    await db.runSql('ALTER DATABASE box CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin');

    for (const tableName of tableNames) {
        // this update all applicable columns (VARCHAR, TEXT, etc.) to use the new character set and collation and not just the table!
        console.log(`Changing charset of ${tableName}`);
        await db.runSql(`ALTER TABLE ${tableName} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);
    }

    await db.runSql('SET foreign_key_checks = 1');
};

exports.down = async function (db) {
    // no going back!
};
