1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: migrate only users without any role

This commit is contained in:
Ivar Conradi Østhus 2021-04-28 12:56:19 +02:00
parent 6c57aeb232
commit 65fad95c6f
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09

View File

@ -2,8 +2,8 @@
const async = require('async');
function resolveRoleName(permissions = []) {
if (permissions.length === 0) {
function resolveRoleName(permissions) {
if (!permissions || permissions.length === 0) {
return 'Viewer';
}
if (permissions.includes('ADMIN')) {
@ -13,24 +13,27 @@ function resolveRoleName(permissions = []) {
}
exports.up = function(db, cb) {
db.runSql(`SELECT id, permissions from users;`, (err, results) => {
if (results.rowCount > 0) {
const users = results.rows;
const insertRootRole = users.map(u => {
const roleName = resolveRoleName(u.permissions);
return db.runSql.bind(
db,
`INSERT INTO role_user (role_id, user_id)
db.runSql(
`SELECT id, permissions from users WHERE id NOT IN (select user_id from role_user);`,
(err, results) => {
if (results.rowCount > 0) {
const users = results.rows;
const insertRootRole = users.map(u => {
const roleName = resolveRoleName(u.permissions);
return db.runSql.bind(
db,
`INSERT INTO role_user (role_id, user_id)
SELECT id, '${u.id}'
FROM roles
WHERE name = '${roleName}' AND type = 'root';`,
);
});
async.series(insertRootRole, cb);
} else {
cb();
}
});
);
});
async.series(insertRootRole, cb);
} else {
cb();
}
},
);
};
exports.down = function(db, cb) {